Browse Source

neon: cleanup account handling

pull/337/head
Nikolas Rimikis 2 years ago
parent
commit
11066bfe32
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 28
      packages/neon/neon/lib/src/blocs/accounts.dart
  2. 2
      packages/neon/neon/lib/src/utils/global_options.dart

28
packages/neon/neon/lib/src/blocs/accounts.dart

@ -28,6 +28,13 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
_globalOptions.updateAccounts(as); _globalOptions.updateAccounts(as);
await _storage.setStringList(_keyAccounts, as.map((final a) => json.encode(a.toJson())).toList()); 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; final as = accounts.value;
if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) { if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) {
@ -76,7 +83,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
BehaviorSubject<List<Account>> accounts = BehaviorSubject<List<Account>>.seeded([]); BehaviorSubject<List<Account>> accounts = BehaviorSubject<List<Account>>.seeded([]);
@override @override
BehaviorSubject<Account?> activeAccount = BehaviorSubject<Account?>.seeded(null); BehaviorSubject<Account?> activeAccount = BehaviorSubject<Account?>.seeded(null)
..distinct((final current, final next) => current?.id != next?.id);
@override @override
void addAccount(final Account account) { void addAccount(final Account account) {
@ -92,8 +100,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
final as = accounts.value; final as = accounts.value;
final aa = activeAccount.valueOrNull; final aa = activeAccount.valueOrNull;
if (aa != null && aa.id == account.id) { if (aa?.id == account.id) {
setActiveAccount(as.isNotEmpty ? as[0] : null); setActiveAccount(as.firstOrNull);
} }
unawaited(() async { unawaited(() async {
@ -108,20 +116,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
@override @override
void setActiveAccount(final Account? account) { 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; final as = accounts.value;
if (as.isNotEmpty) { activeAccount.add(account ?? as.firstOrNull);
setActiveAccount(as[0]);
} else {
unawaited(_storage.remove(_keyLastUsedAccount));
activeAccount.add(null);
}
}
} }
@override @override

2
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, // Only override the initial account if there already has been a value,
// which means it's not the initial emit from rememberLastUsedAccount // which means it's not the initial emit from rememberLastUsedAccount
if (initialAccount.hasValue) { if (initialAccount.hasValue) {
await initialAccount.set((await initialAccount.values.first).keys.toList()[0]); await initialAccount.set((await initialAccount.values.first).keys.first);
} }
} }
}); });

Loading…
Cancel
Save