From d5f7d8d962f1f9ea1b5452e7f74cc231f2664c05 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 29 Aug 2023 17:13:34 +0200 Subject: [PATCH] refactor(neon): refactor account storage Signed-off-by: Nikolas Rimikis --- .../neon/neon/lib/src/blocs/accounts.dart | 19 ++++++++++++++----- .../neon/neon/lib/src/utils/push_utils.dart | 3 +-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index ed251c51..02b4a6d3 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -61,10 +61,10 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState const lastUsedStorage = SingleValueStorage('last-used-account'); accounts - ..add(loadAccounts(_storage)) + ..add(loadAccounts()) ..listen((final as) async { _globalOptions.updateAccounts(as); - await _storage.setStringList(_keyAccounts, as.map((final a) => json.encode(a.toJson())).toList()); + await saveAccounts(as); }); activeAccount.listen((final aa) async { if (aa != null) { @@ -96,7 +96,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState } } - late final AppStorage _storage = const AppStorage('accounts'); final GlobalOptions _globalOptions; final Iterable _allAppImplementations; @@ -278,10 +277,12 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState ); } -/// Get a list of logged in accounts from [storage]. +/// Gets a list of logged in accounts from storage. /// /// It is not checked whether the stored information is still valid. -List loadAccounts(final AppStorage storage) { +List loadAccounts() { + const storage = AppStorage('accounts'); + if (storage.containsKey(_keyAccounts)) { return storage .getStringList(_keyAccounts)! @@ -290,3 +291,11 @@ List loadAccounts(final AppStorage storage) { } return []; } + +/// Saves the given [accounts] to the storage. +Future saveAccounts(final List accounts) async { + const storage = AppStorage('accounts'); + final values = accounts.map((final a) => json.encode(a.toJson())).toList(); + + await storage.setStringList(_keyAccounts, values); +} diff --git a/packages/neon/neon/lib/src/utils/push_utils.dart b/packages/neon/neon/lib/src/utils/push_utils.dart index d119e5c2..1ee1121e 100644 --- a/packages/neon/neon/lib/src/utils/push_utils.dart +++ b/packages/neon/neon/lib/src/utils/push_utils.dart @@ -93,12 +93,11 @@ class PushUtils { } else { final localizations = await appLocalizationsFromSystem(); - var accounts = []; + final accounts = loadAccounts(); Account? account; NotificationsNotification? notification; AndroidBitmap? largeIconBitmap; try { - accounts = loadAccounts(const AppStorage('accounts')); account = accounts.tryFind(instance); if (account != null) { notification =