Browse Source

neon: cache appBloc in the AppImplementation

also gives us caches per account.
pull/371/head
Nikolas Rimikis 2 years ago
parent
commit
6ad75244ea
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 19
      packages/neon/neon/lib/src/blocs/apps.dart
  2. 13
      packages/neon/neon/lib/src/utils/app_implementation.dart

19
packages/neon/neon/lib/src/blocs/apps.dart

@ -82,8 +82,6 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
final Account _account;
final List<AppImplementation> _allAppImplementations;
final Map<String, Bloc> _blocs = {};
@override
void dispose() {
unawaited(apps.close());
@ -91,8 +89,11 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
unawaited(notificationsAppImplementation.close());
unawaited(activeAppID.close());
unawaited(openNotifications.close());
for (final key in _blocs.keys) {
_blocs[key]!.dispose();
for (final app in _allAppImplementations) {
for (final bloc in app.blocs.values) {
bloc.dispose();
}
}
}
@ -137,14 +138,8 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
}
}
T getAppBloc<T extends Bloc>(final AppImplementation appImplementation) {
if (_blocs[appImplementation.id] != null) {
return _blocs[appImplementation.id]! as T;
}
return _blocs[appImplementation.id] = appImplementation.buildBloc(_account.client) as T;
}
T getAppBloc<T extends Bloc>(final AppImplementation appImplementation) => appImplementation.getBloc(_account) as T;
List<Provider> get appBlocProviders =>
_allAppImplementations.map((final appImplementation) => appImplementation.blocProvider(_account.client)).toList();
_allAppImplementations.map((final appImplementation) => appImplementation.blocProvider).toList();
}

13
packages/neon/neon/lib/src/utils/app_implementation.dart

@ -22,10 +22,19 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppSpecificO
late final R options;
R buildOptions(final AppStorage storage);
final Map<String, T> blocs = {};
T getBloc(final Account account) => blocs[account.id] ??= buildBloc(account.client);
T buildBloc(final NextcloudClient client);
Provider<T> blocProvider(final NextcloudClient client) => Provider<T>(
create: (final _) => buildBloc(client),
Provider<T> get blocProvider => Provider<T>(
create: (final context) {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
final account = accountsBloc.activeAccount.value!;
return getBloc(account);
},
);
BehaviorSubject<int>? getUnreadCounter(final AppsBloc appsBloc);

Loading…
Cancel
Save