Skip to content

Commit

Permalink
refactor: extract "selects" out from widgets
Browse files Browse the repository at this point in the history
Moving to separate providers keeps stuff DRY.
It could potentially also help perf, which is a win.
  • Loading branch information
lishaduck committed Oct 16, 2023
1 parent dc8c94a commit 53f1f85
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions lib/features/auth/application/auth_service.dart
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -47,3 +50,33 @@ PirateUser? user(UserRef ref) => ref.watch(

/// The email address used in case things go wrong.
const redactedEmail = "[email protected]";

/// 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));
}
2 changes: 1 addition & 1 deletion lib/features/auth/data/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 8 additions & 5 deletions lib/features/dashboard/application/dashboard_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Applet> _applets(_AppletsRef ref) {
List<Applet> get _applets {
// Add more buttons here
// Ideas:
//
Expand Down Expand Up @@ -54,3 +51,9 @@ List<Applet> _applets(_AppletsRef ref) {
// ),
];
}

/// Get the list of applets.
@riverpod
List<Applet> applets(AppletsRef ref) => ref.watch(
dashboardServiceProvider.select((value) => value.applets),
);
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 53f1f85

Please sign in to comment.