Browse Source

neon: add convenience methods for the active account to AccountsBloc

Co-authored-by: Kate <26026535+provokateurin@users.noreply.github.com>
pull/341/head
Nikolas Rimikis 1 year ago
parent
commit
fbd4f6bade
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 8
      packages/neon/neon/lib/src/app.dart
  2. 72
      packages/neon/neon/lib/src/blocs/accounts.dart
  3. 2
      packages/neon/neon/lib/src/blocs/apps.dart
  4. 4
      packages/neon/neon/lib/src/pages/account_settings.dart
  5. 4
      packages/neon/neon/lib/src/pages/home.dart
  6. 4
      packages/neon/neon/lib/src/pages/settings.dart
  7. 2
      packages/neon/neon/lib/src/widgets/account_tile.dart
  8. 2
      packages/neon/neon/lib/src/widgets/user_avatar.dart
  9. 2
      packages/neon/neon_notifications/lib/pages/main.dart

8
packages/neon/neon/lib/src/app.dart

@ -124,7 +124,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
}
final app = Provider.of<List<AppImplementation>>(context, listen: false).find('notifications');
if (app != null) {
await _accountsBloc.getAppsBloc(account).getAppBloc<NotificationsBlocInterface>(app).refresh();
await _accountsBloc.getAppsBlocFor(account).getAppBloc<NotificationsBlocInterface>(app).refresh();
}
};
Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async {
@ -144,7 +144,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
if (app != null) {
if (app.id != 'notifications') {
_accountsBloc
.getAppsBloc(account)
.getAppsBlocFor(account)
.getAppBloc<NotificationsBlocInterface>(app)
.deleteNotification(pushNotificationWithAccountID.subject.nid!);
}
@ -211,7 +211,7 @@ class _NeonAppState extends State<NeonApp> 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<NeonApp> with WidgetsBindingObserver, tray.Tra
FlutterNativeSplash.remove();
return ResultBuilder<Capabilities?>(
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;

72
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<Account> loadAccounts(final AppStorage storage) {
if (storage.containsKey(_keyAccounts)) {
return storage

2
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) {

4
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

4
packages/neon/neon/lib/src/pages/home.dart

@ -28,8 +28,8 @@ class _HomePageState extends State<HomePage> {
_globalOptions = Provider.of<GlobalOptions>(context, listen: false);
_accountsBloc = Provider.of<AccountsBloc>(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;

4
packages/neon/neon/lib/src/pages/settings.dart

@ -31,7 +31,7 @@ class _SettingsPageState extends State<SettingsPage> {
}
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<SettingsPage> {
accountSpecificOptions: {
if (accountsSnapshot.hasData) ...{
for (final account in accountsSnapshot.data!) ...{
account: accountsBloc.getOptions(account).options,
account: accountsBloc.getOptionsFor(account).options,
},
},
},

2
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<AccountsBloc>(context, listen: false).getUserDetailsBloc(account);
final userDetailsBloc = Provider.of<AccountsBloc>(context, listen: false).getUserDetailsBlocFor(account);
return ListTile(
textColor: textColor,

2
packages/neon/neon/lib/src/widgets/user_avatar.dart

@ -24,7 +24,7 @@ class NeonUserAvatar extends StatefulWidget {
}
class _UserAvatarState extends State<NeonUserAvatar> {
late final _userStatusBloc = Provider.of<AccountsBloc>(context, listen: false).getUserStatusesBloc(widget.account);
late final _userStatusBloc = Provider.of<AccountsBloc>(context, listen: false).getUserStatusesBlocFor(widget.account);
late double size;
@override

2
packages/neon/neon_notifications/lib/pages/main.dart

@ -91,7 +91,7 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
}
if (app != null) {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
await accountsBloc.getAppsBloc(accountsBloc.activeAccount.value!).setActiveApp(app.id);
await accountsBloc.activeAppsBloc.setActiveApp(app.id);
} else {
await showDialog(
context: context,

Loading…
Cancel
Save