From 6ad75244ea9f24823b781cb91d8bec19914d5824 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 12 Jun 2023 11:10:55 +0200 Subject: [PATCH] neon: cache appBloc in the AppImplementation also gives us caches per account. --- packages/neon/neon/lib/src/blocs/apps.dart | 19 +++++++------------ .../lib/src/utils/app_implementation.dart | 13 +++++++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index fd06e320..9b269778 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -82,8 +82,6 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates final Account _account; final List _allAppImplementations; - final Map _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(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(final AppImplementation appImplementation) => appImplementation.getBloc(_account) as T; List get appBlocProviders => - _allAppImplementations.map((final appImplementation) => appImplementation.blocProvider(_account.client)).toList(); + _allAppImplementations.map((final appImplementation) => appImplementation.blocProvider).toList(); } diff --git a/packages/neon/neon/lib/src/utils/app_implementation.dart b/packages/neon/neon/lib/src/utils/app_implementation.dart index 61a75194..3e61f94c 100644 --- a/packages/neon/neon/lib/src/utils/app_implementation.dart +++ b/packages/neon/neon/lib/src/utils/app_implementation.dart @@ -22,10 +22,19 @@ abstract class AppImplementation blocs = {}; + + T getBloc(final Account account) => blocs[account.id] ??= buildBloc(account.client); + T buildBloc(final NextcloudClient client); - Provider blocProvider(final NextcloudClient client) => Provider( - create: (final _) => buildBloc(client), + Provider get blocProvider => Provider( + create: (final context) { + final accountsBloc = Provider.of(context, listen: false); + final account = accountsBloc.activeAccount.value!; + + return getBloc(account); + }, ); BehaviorSubject? getUnreadCounter(final AppsBloc appsBloc);