From 11066bfe32d02a5fff4bb0e948277ca3bb940921 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 22 May 2023 12:31:01 +0200 Subject: [PATCH] neon: cleanup account handling --- .../neon/neon/lib/src/blocs/accounts.dart | 30 ++++++++----------- .../neon/lib/src/utils/global_options.dart | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index b4922b8c..4d79f044 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -28,6 +28,13 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState _globalOptions.updateAccounts(as); await _storage.setStringList(_keyAccounts, as.map((final a) => json.encode(a.toJson())).toList()); }); + activeAccount.listen((final aa) async { + if (aa != null) { + await _storage.setString(_keyLastUsedAccount, aa.id); + } else { + await _storage.remove(_keyLastUsedAccount); + } + }); final as = accounts.value; if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { @@ -76,7 +83,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState BehaviorSubject> accounts = BehaviorSubject>.seeded([]); @override - BehaviorSubject activeAccount = BehaviorSubject.seeded(null); + BehaviorSubject activeAccount = BehaviorSubject.seeded(null) + ..distinct((final current, final next) => current?.id != next?.id); @override void addAccount(final Account account) { @@ -92,8 +100,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState final as = accounts.value; final aa = activeAccount.valueOrNull; - if (aa != null && aa.id == account.id) { - setActiveAccount(as.isNotEmpty ? as[0] : null); + if (aa?.id == account.id) { + setActiveAccount(as.firstOrNull); } unawaited(() async { @@ -108,20 +116,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState @override void setActiveAccount(final Account? account) { - if (account != null) { - if (activeAccount.value != account) { - unawaited(_storage.setString(_keyLastUsedAccount, account.id)); - activeAccount.add(account); - } - } else { - final as = accounts.value; - if (as.isNotEmpty) { - setActiveAccount(as[0]); - } else { - unawaited(_storage.remove(_keyLastUsedAccount)); - activeAccount.add(null); - } - } + final as = accounts.value; + activeAccount.add(account ?? as.firstOrNull); } @override diff --git a/packages/neon/neon/lib/src/utils/global_options.dart b/packages/neon/neon/lib/src/utils/global_options.dart index 926fad3b..462dfcce 100644 --- a/packages/neon/neon/lib/src/utils/global_options.dart +++ b/packages/neon/neon/lib/src/utils/global_options.dart @@ -41,7 +41,7 @@ class GlobalOptions { // Only override the initial account if there already has been a value, // which means it's not the initial emit from rememberLastUsedAccount if (initialAccount.hasValue) { - await initialAccount.set((await initialAccount.values.first).keys.toList()[0]); + await initialAccount.set((await initialAccount.values.first).keys.first); } } });