From fbd4f6bade18809ea08f538556400bcb6082fc0e Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 5 Jun 2023 15:08:37 +0200 Subject: [PATCH] neon: add convenience methods for the active account to AccountsBloc Co-authored-by: Kate <26026535+provokateurin@users.noreply.github.com> --- packages/neon/neon/lib/src/app.dart | 8 +-- .../neon/neon/lib/src/blocs/accounts.dart | 72 +++++++++++++++++-- packages/neon/neon/lib/src/blocs/apps.dart | 2 +- .../neon/lib/src/pages/account_settings.dart | 4 +- packages/neon/neon/lib/src/pages/home.dart | 4 +- .../neon/neon/lib/src/pages/settings.dart | 4 +- .../neon/lib/src/widgets/account_tile.dart | 2 +- .../neon/lib/src/widgets/user_avatar.dart | 2 +- .../neon_notifications/lib/pages/main.dart | 2 +- 9 files changed, 79 insertions(+), 21 deletions(-) diff --git a/packages/neon/neon/lib/src/app.dart b/packages/neon/neon/lib/src/app.dart index b4687d30..da3ad4ae 100644 --- a/packages/neon/neon/lib/src/app.dart +++ b/packages/neon/neon/lib/src/app.dart @@ -124,7 +124,7 @@ class _NeonAppState extends State with WidgetsBindingObserver, tray.Tra } final app = Provider.of>(context, listen: false).find('notifications'); if (app != null) { - await _accountsBloc.getAppsBloc(account).getAppBloc(app).refresh(); + await _accountsBloc.getAppsBlocFor(account).getAppBloc(app).refresh(); } }; Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async { @@ -144,7 +144,7 @@ class _NeonAppState extends State with WidgetsBindingObserver, tray.Tra if (app != null) { if (app.id != 'notifications') { _accountsBloc - .getAppsBloc(account) + .getAppsBlocFor(account) .getAppBloc(app) .deleteNotification(pushNotificationWithAccountID.subject.nid!); } @@ -211,7 +211,7 @@ class _NeonAppState extends State with WidgetsBindingObserver, tray.Tra } Future _openAppFromExternal(final Account account, final String id) async { - await _accountsBloc.getAppsBloc(account).setActiveApp(id); + await _accountsBloc.getAppsBlocFor(account).setActiveApp(id); _navigatorKey.currentState!.popUntil((final route) => route.settings.name == 'home'); await _showAndRestoreWindow(); } @@ -268,7 +268,7 @@ class _NeonAppState extends State with WidgetsBindingObserver, tray.Tra FlutterNativeSplash.remove(); return ResultBuilder( stream: activeAccountSnapshot.hasData - ? widget.accountsBloc.getCapabilitiesBloc(activeAccountSnapshot.data!).capabilities + ? widget.accountsBloc.getCapabilitiesBlocFor(activeAccountSnapshot.data!).capabilities : null, builder: (final context, final capabilitiesSnapshot) { final nextcloudTheme = capabilitiesSnapshot.data?.capabilities.theming; diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index 8951f3f6..c667c10c 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -176,26 +176,65 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState setActiveAccount(account); } - AccountSpecificOptions getOptions(final Account account) => _accountsOptions[account.id] ??= AccountSpecificOptions( + /// The currently active account. + /// + /// Equivalent to activeAccount.value but throws a [StateError] when no user is logged in. + @visibleForTesting + Account get aa { + final aa = activeAccount.value; + + if (aa == null) { + throw StateError('No user is logged in.'); + } + + return aa; + } + + /// The options for the [activeAccount]. + /// + /// Convenience method for [getOptionsFor] with the currently active account. + AccountSpecificOptions get activeOptions => getOptionsFor(aa); + + /// The options for the specified [account]. + /// + /// Use [activeOptions] to get them for the [activeAccount]. + AccountSpecificOptions getOptionsFor(final Account account) => + _accountsOptions[account.id] ??= AccountSpecificOptions( AppStorage('accounts-${account.id}', _sharedPreferences), - getAppsBloc(account), + getAppsBlocFor(account), ); - AppsBloc getAppsBloc(final Account account) { + /// The appsBloc for the [activeAccount]. + /// + /// Convenience method for [getAppsBlocFor] with the currently active account. + AppsBloc get activeAppsBloc => getAppsBlocFor(aa); + + /// The appsBloc for the specified [account]. + /// + /// Use [activeAppsBloc] to get them for the [activeAccount]. + AppsBloc getAppsBlocFor(final Account account) { if (_appsBlocs[account.id] != null) { return _appsBlocs[account.id]!; } return _appsBlocs[account.id] = AppsBloc( _requestManager, - getCapabilitiesBloc(account), + getCapabilitiesBlocFor(account), this, account, _allAppImplementations, ); } - CapabilitiesBloc getCapabilitiesBloc(final Account account) { + /// The capabilitiesBloc for the [activeAccount]. + /// + /// Convenience method for [getCapabilitiesBlocFor] with the currently active account. + CapabilitiesBloc get activeCapabilitiesBloc => getCapabilitiesBlocFor(aa); + + /// The capabilitiesBloc for the specified [account]. + /// + /// Use [activeCapabilitiesBloc] to get them for the [activeAccount]. + CapabilitiesBloc getCapabilitiesBlocFor(final Account account) { if (_capabilitiesBlocs[account.id] != null) { return _capabilitiesBlocs[account.id]!; } @@ -206,7 +245,15 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState ); } - UserDetailsBloc getUserDetailsBloc(final Account account) { + /// The userDetailsBloc for the [activeAccount]. + /// + /// Convenience method for [getUserDetailsBlocFor] with the currently active account. + UserDetailsBloc get activeUerDetailsBloc => getUserDetailsBlocFor(aa); + + /// The userDetailsBloc for the specified [account]. + /// + /// Use [activeUerDetailsBloc] to get them for the [activeAccount]. + UserDetailsBloc getUserDetailsBlocFor(final Account account) { if (_userDetailsBlocs[account.id] != null) { return _userDetailsBlocs[account.id]!; } @@ -217,7 +264,15 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState ); } - UserStatusesBloc getUserStatusesBloc(final Account account) { + /// The userStatusBloc for the [activeAccount]. + /// + /// Convenience method for [getUserStatusesBlocFor] with the currently active account. + UserStatusesBloc get activeUserStatusesBloc => getUserStatusesBlocFor(aa); + + /// The userStatusBloc for the specified [account]. + /// + /// Use [activeUserStatusesBloc] to get them for the [activeAccount]. + UserStatusesBloc getUserStatusesBlocFor(final Account account) { if (_userStatusesBlocs[account.id] != null) { return _userStatusesBlocs[account.id]!; } @@ -229,6 +284,9 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState } } +/// Get a list of logged in accounts from [storage]. +/// +/// It is not checked whether the stored information is still valid. List loadAccounts(final AppStorage storage) { if (storage.containsKey(_keyAccounts)) { return storage diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index a2c799cc..5dceb6d8 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -33,7 +33,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates appImplementations.listen((final result) { if (result.data != null) { - final options = _accountsBloc.getOptions(_account); + final options = _accountsBloc.getOptionsFor(_account); unawaited( options.initialApp.stream.first.then((var initialApp) async { if (initialApp == null) { diff --git a/packages/neon/neon/lib/src/pages/account_settings.dart b/packages/neon/neon/lib/src/pages/account_settings.dart index 0b95a9f6..ea91a5ef 100644 --- a/packages/neon/neon/lib/src/pages/account_settings.dart +++ b/packages/neon/neon/lib/src/pages/account_settings.dart @@ -10,8 +10,8 @@ class AccountSettingsPage extends StatelessWidget { final AccountsBloc bloc; final Account account; - late final _options = bloc.getOptions(account); - late final _userDetailsBloc = bloc.getUserDetailsBloc(account); + late final _options = bloc.getOptionsFor(account); + late final _userDetailsBloc = bloc.getUserDetailsBlocFor(account); late final _name = account.client.humanReadableID; @override diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 553f051d..620e6b7d 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -28,8 +28,8 @@ class _HomePageState extends State { _globalOptions = Provider.of(context, listen: false); _accountsBloc = Provider.of(context, listen: false); _account = _accountsBloc.activeAccount.value!; - _appsBloc = _accountsBloc.getAppsBloc(_account); - _capabilitiesBloc = _accountsBloc.getCapabilitiesBloc(_account); + _appsBloc = _accountsBloc.activeAppsBloc; + _capabilitiesBloc = _accountsBloc.activeCapabilitiesBloc; _appsBloc.openNotifications.listen((final _) async { final notificationsAppImplementation = _appsBloc.notificationsAppImplementation.valueOrNull; diff --git a/packages/neon/neon/lib/src/pages/settings.dart b/packages/neon/neon/lib/src/pages/settings.dart index 145d740b..efebfb80 100644 --- a/packages/neon/neon/lib/src/pages/settings.dart +++ b/packages/neon/neon/lib/src/pages/settings.dart @@ -31,7 +31,7 @@ class _SettingsPageState extends State { } for (final account in accountsBloc.accounts.value) { - await accountsBloc.getOptions(account).reset(); + await accountsBloc.getOptionsFor(account).reset(); } } }, @@ -52,7 +52,7 @@ class _SettingsPageState extends State { accountSpecificOptions: { if (accountsSnapshot.hasData) ...{ for (final account in accountsSnapshot.data!) ...{ - account: accountsBloc.getOptions(account).options, + account: accountsBloc.getOptionsFor(account).options, }, }, }, diff --git a/packages/neon/neon/lib/src/widgets/account_tile.dart b/packages/neon/neon/lib/src/widgets/account_tile.dart index 54eb5b15..5afa02c8 100644 --- a/packages/neon/neon/lib/src/widgets/account_tile.dart +++ b/packages/neon/neon/lib/src/widgets/account_tile.dart @@ -20,7 +20,7 @@ class NeonAccountTile extends StatelessWidget { @override Widget build(final BuildContext context) { - final userDetailsBloc = Provider.of(context, listen: false).getUserDetailsBloc(account); + final userDetailsBloc = Provider.of(context, listen: false).getUserDetailsBlocFor(account); return ListTile( textColor: textColor, diff --git a/packages/neon/neon/lib/src/widgets/user_avatar.dart b/packages/neon/neon/lib/src/widgets/user_avatar.dart index ce23ce3b..80ff76ee 100644 --- a/packages/neon/neon/lib/src/widgets/user_avatar.dart +++ b/packages/neon/neon/lib/src/widgets/user_avatar.dart @@ -24,7 +24,7 @@ class NeonUserAvatar extends StatefulWidget { } class _UserAvatarState extends State { - late final _userStatusBloc = Provider.of(context, listen: false).getUserStatusesBloc(widget.account); + late final _userStatusBloc = Provider.of(context, listen: false).getUserStatusesBlocFor(widget.account); late double size; @override diff --git a/packages/neon/neon_notifications/lib/pages/main.dart b/packages/neon/neon_notifications/lib/pages/main.dart index 6412354b..f647a860 100644 --- a/packages/neon/neon_notifications/lib/pages/main.dart +++ b/packages/neon/neon_notifications/lib/pages/main.dart @@ -91,7 +91,7 @@ class _NotificationsMainPageState extends State { } if (app != null) { final accountsBloc = Provider.of(context, listen: false); - await accountsBloc.getAppsBloc(accountsBloc.activeAccount.value!).setActiveApp(app.id); + await accountsBloc.activeAppsBloc.setActiveApp(app.id); } else { await showDialog( context: context,