diff --git a/build.yaml b/build.yaml index 061b37ce..7445bf16 100644 --- a/build.yaml +++ b/build.yaml @@ -36,7 +36,7 @@ targets: generate_for: - lib/utils/*.dart - lib/features/*/data/*_repository.dart - - lib/features/*/application/*_service.dart + - lib/features/*/application/*.dart flutter_gen_runner: # configs for the flutter.assets generator generate_for: diff --git a/lib/features/auth/application/auth_service.dart b/lib/features/auth/application/auth_service.dart index fcf47579..f0c8de68 100644 --- a/lib/features/auth/application/auth_service.dart +++ b/lib/features/auth/application/auth_service.dart @@ -1,9 +1,12 @@ /// This library contains the authentication feature's business logic. library; +import "dart:typed_data"; + import "package:riverpod_annotation/riverpod_annotation.dart"; import "../data/auth_repository.dart"; +import "../domain/account_type.dart"; import "../domain/pirate_auth_model.dart"; import "../domain/pirate_user.dart"; @@ -47,3 +50,33 @@ PirateUser? user(UserRef ref) => ref.watch( /// The email address used in case things go wrong. const redactedEmail = "redacted@example.com"; + +/// Get the current user's name. +@riverpod +String? name(NameRef ref) { + return ref.watch(userProvider.select((value) => value?.name)); +} + +/// Get the current user's email address. +@riverpod +String? email(EmailRef ref) { + return ref.watch(userProvider.select((value) => value?.email)); +} + +/// Get the current user's avatar. +@riverpod +Uint8List? avatar(AvatarRef ref) { + return ref.watch(userProvider.select((value) => value?.avatar)); +} + +/// Get the current user's account type. +@riverpod +AccountType? accountType(AccountTypeRef ref) { + return ref.watch(userProvider.select((value) => value?.accountType)); +} + +/// Get the current user's ID. +@riverpod +int? id(IdRef ref) { + return ref.watch(userProvider.select((value) => value?.id)); +} diff --git a/lib/features/auth/data/auth_repository.dart b/lib/features/auth/data/auth_repository.dart index 5a4f5d8d..d4c07d5d 100644 --- a/lib/features/auth/data/auth_repository.dart +++ b/lib/features/auth/data/auth_repository.dart @@ -100,7 +100,7 @@ base class _AppwriteAuthRepository implements AuthRepository { AuthRepository auth(AuthRef ref) { final account = ref.watch(accountsProvider); final platform = ref.watch(currentPlatformProvider); - final avatar = ref.watch(avatarProvider.select((value) => value)); + final avatar = ref.watch(avatarProvider); return _AppwriteAuthRepository(account, platform, avatar); } diff --git a/lib/features/dashboard/application/dashboard_service.dart b/lib/features/dashboard/application/dashboard_service.dart index a056a104..19dcffc7 100644 --- a/lib/features/dashboard/application/dashboard_service.dart +++ b/lib/features/dashboard/application/dashboard_service.dart @@ -15,15 +15,12 @@ part "dashboard_service.g.dart"; class DashboardService extends _$DashboardService { @override DashboardModel build() { - final applets = ref.watch(_appletsProvider); - - return DashboardModel(applets: applets); + return DashboardModel(applets: _applets); } } /// Get the list of applets. -@riverpod -List _applets(_AppletsRef ref) { +List get _applets { // Add more buttons here // Ideas: // @@ -54,3 +51,9 @@ List _applets(_AppletsRef ref) { // ), ]; } + +/// Get the list of applets. +@riverpod +List applets(AppletsRef ref) => ref.watch( + dashboardServiceProvider.select((value) => value.applets), + ); diff --git a/lib/features/dashboard/presentation/dashboard_page/dashboard_page.dart b/lib/features/dashboard/presentation/dashboard_page/dashboard_page.dart index c883a501..56226a97 100644 --- a/lib/features/dashboard/presentation/dashboard_page/dashboard_page.dart +++ b/lib/features/dashboard/presentation/dashboard_page/dashboard_page.dart @@ -38,9 +38,7 @@ class _Applets extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final buttonsData = ref.watch( - dashboardServiceProvider.select((value) => value.applets), - ); + final buttonsData = ref.watch(appletsProvider); return Expanded( child: Container( diff --git a/lib/features/dashboard/presentation/wrapper_page/wrapper_page.dart b/lib/features/dashboard/presentation/wrapper_page/wrapper_page.dart index a7883dec..a9ed74e1 100644 --- a/lib/features/dashboard/presentation/wrapper_page/wrapper_page.dart +++ b/lib/features/dashboard/presentation/wrapper_page/wrapper_page.dart @@ -65,10 +65,9 @@ class _ExpandedWrapper extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final theme = Theme.of(context); - final (name, email, avatar) = ref.watch( - userProvider - .select((value) => (value?.name, value?.email, value?.avatar)), - ); + final name = ref.watch(nameProvider); + final email = ref.watch(emailProvider); + final avatar = ref.watch(avatarProvider); return Scaffold( appBar: AppBar( diff --git a/lib/features/pirate_coins/presentation/pirate_coins_page/pirate_coins_page.dart b/lib/features/pirate_coins/presentation/pirate_coins_page/pirate_coins_page.dart index 75578008..b6dc28cb 100644 --- a/lib/features/pirate_coins/presentation/pirate_coins_page/pirate_coins_page.dart +++ b/lib/features/pirate_coins/presentation/pirate_coins_page/pirate_coins_page.dart @@ -24,9 +24,8 @@ class PirateCoinsPage extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final (accountType, id) = ref.watch( - userProvider.select((value) => (value?.accountType, value?.id)), - ); + final accountType = ref.watch(accountTypeProvider); + final id = ref.watch(idProvider); final child = accountType == AccountType.student ? _StudentView(id: id ?? 0) diff --git a/lib/features/pirate_coins/presentation/stats_page/stats_page.dart b/lib/features/pirate_coins/presentation/stats_page/stats_page.dart index bac22394..04c96792 100644 --- a/lib/features/pirate_coins/presentation/stats_page/stats_page.dart +++ b/lib/features/pirate_coins/presentation/stats_page/stats_page.dart @@ -7,7 +7,6 @@ import "package:hooks_riverpod/hooks_riverpod.dart"; import "../../../../l10n/l10n.dart"; import "../../../../widgets/big_card/big_card.dart"; -import "../../../auth/application/auth_service.dart"; import "../../application/coins_service.dart"; /// The page at `/pirate-coins/stats`. @@ -18,8 +17,7 @@ class StatsPage extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final id = ref.watch(userProvider.select((value) => value?.id)); - final data = id != null ? ref.watch(coinsServiceProvider(id)) : null; + final data = ref.watch(currentUserCoinsProvider); final l10n = context.l10n; return Center( @@ -29,8 +27,8 @@ class StatsPage extends ConsumerWidget { Padding( padding: const EdgeInsets.all(8), child: switch (data) { - AsyncData(:final value) => value.coins.coins > 0 - ? BigCard(l10n.howManyCoins(value.coins.coins)) + AsyncData(:final value) => (value?.coins.coins ?? 0) > 0 + ? BigCard(l10n.howManyCoins(value?.coins.coins ?? 0)) : BigCard(l10n.emptyReport), AsyncError(:final error) => BigCard(l10n.error("$error")), AsyncLoading() => Column(