relatica/lib/screens/interactions_viewer_screen....

111 wiersze
3.8 KiB
Dart

2023-01-31 03:22:08 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../controls/async_value_widget.dart';
import '../controls/error_message_widget.dart';
import '../controls/image_control.dart';
import '../controls/responsive_max_width.dart';
import '../controls/standard_appbar.dart';
import '../controls/status_and_refresh_button.dart';
import '../models/auth/profile.dart';
import '../models/interaction_type_enum.dart';
import '../riverpod_controllers/account_services.dart';
import '../riverpod_controllers/connection_manager_services.dart';
import '../riverpod_controllers/interactions_details_services.dart';
import '../routes.dart';
2023-01-31 03:22:08 +00:00
class InteractionsViewerScreen extends ConsumerWidget {
final String statusId;
2023-01-31 03:22:08 +00:00
final InteractionType type;
const InteractionsViewerScreen({
super.key,
required this.statusId,
2023-01-31 03:22:08 +00:00
required this.type,
});
void refreshInteractors(WidgetRef ref, Profile profile) async {
switch (type) {
case InteractionType.like:
return ref.invalidate(likesForStatusProvider(profile, statusId));
case InteractionType.reshare:
return ref.invalidate(resharesForStatusProvider(profile, statusId));
}
}
2023-01-31 03:22:08 +00:00
@override
Widget build(BuildContext context, WidgetRef ref) {
final profile = ref.watch(activeProfileProvider);
final connectionsAsyncValue = switch (type) {
InteractionType.like =>
ref.watch(likesForStatusProvider(profile, statusId)),
InteractionType.reshare =>
ref.watch(resharesForStatusProvider(profile, statusId)),
};
final loading = switch (connectionsAsyncValue) {
AsyncData() => false,
_ => true,
};
2023-01-31 03:22:08 +00:00
return Scaffold(
appBar: StandardAppBar.build(context, buildTitle(), actions: [
StatusAndRefreshButton(
executing: loading,
refreshFunction: () async => refreshInteractors(ref, profile),
2024-05-19 15:07:15 +00:00
busyColor: Theme.of(context).colorScheme.surface,
)
]),
2023-01-31 03:22:08 +00:00
body: Center(
child: ResponsiveMaxWidth(
child: AsyncValueWidget(connectionsAsyncValue,
valueBuilder: (context, ref, connectionsResult) {
if (connectionsResult.isFailure) {
return ErrorMessageWidget(message: connectionsResult.error.message);
}
final connections = connectionsResult.value;
return ListView.separated(
itemCount: connections.length,
itemBuilder: (context, index) {
final connection = connections[index];
return ListTile(
onTap: () async {
await ref
.read(connectionByIdProvider(profile, connection.id))
.withErrorAsync((_) => ref
.read(connectionModifierProvider(profile, connection)
.notifier)
.fullRefresh());
if (context.mounted) {
context.pushNamed(ScreenPaths.userProfile,
pathParameters: {'id': connection.id});
}
},
leading: ImageControl(
imageUrl: connection.avatarUrl.toString(),
iconOverride: const Icon(Icons.person),
width: 32.0,
onTap: () => context.pushNamed(ScreenPaths.userProfile,
pathParameters: {'id': connection.id}),
),
title: Text('${connection.name} (${connection.handle})'),
);
},
separatorBuilder: (_, __) => const Divider(),
);
})),
),
2023-01-31 03:22:08 +00:00
);
}
String buildTitle() {
switch (type) {
case InteractionType.like:
return 'Likes';
case InteractionType.reshare:
return 'Reshares';
}
}
}