From 7ccfb72c0bc101b57ac853dc10a19188894e9157 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Thu, 25 May 2023 14:32:00 +0200 Subject: [PATCH] neon: document AccountsBloc and disable logout with setActiveAccount --- .../neon/neon/lib/src/blocs/accounts.dart | 41 +++++++++++++++---- packages/neon/neon/lib/src/pages/home.dart | 2 +- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index 1741e52d..8951f3f6 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -2,15 +2,39 @@ part of 'blocs.dart'; const _keyAccounts = 'accounts'; -abstract class AccountsBlocEvents { +abstract interface class AccountsBlocEvents { + /// Logs in the given [account]. + /// + /// It will also call [setActiveAccount] when no other accounts are registered in [AccountsBlocStates.accounts]. void addAccount(final Account account); + + /// Logs out the given [account]. + /// + /// If [account] is the current [AccountsBlocStates.activeAccount] it will automatically activate the first one in [AccountsBlocStates.accounts]. + /// It is not defined whether listeners of [AccountsBlocStates.accounts] or [AccountsBlocStates.activeAccount] are informed first. void removeAccount(final Account account); + + /// Updates the given [account]. + /// + /// It triggers an event in both [AccountsBlocStates.accounts] and [AccountsBlocStates.activeAccount] to inform all listeners. void updateAccount(final Account account); - void setActiveAccount(final Account? account); + + /// Sets the active [account]. + /// + /// It triggers an event in [AccountsBlocStates.activeAccount] to inform all listeners. + void setActiveAccount(final Account account); } -abstract class AccountsBlocStates { +abstract interface class AccountsBlocStates { + /// All registered accounts. + /// + /// An empty value represents a state where no account is logged in. BehaviorSubject> get accounts; + + /// Currently active account. + /// + /// It should be ensured to only emit an event when it's value changes. + /// A null value represents a state where no user is logged in. BehaviorSubject get activeAccount; } @@ -111,7 +135,11 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState final as = accounts.value; final aa = activeAccount.valueOrNull; if (aa?.id == account.id) { - setActiveAccount(as.firstOrNull); + if (as.firstOrNull != null) { + setActiveAccount(as.first); + } else { + activeAccount.add(null); + } } unawaited(() async { @@ -125,9 +153,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState } @override - void setActiveAccount(final Account? account) { - final as = accounts.value; - activeAccount.add(account ?? as.firstOrNull); + void setActiveAccount(final Account account) { + activeAccount.add(account); } @override diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index c9d23b18..553f051d 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -300,7 +300,7 @@ class _HomePageState extends State { .toList(), onChanged: (final id) { if (id != null) { - _accountsBloc.setActiveAccount(accounts.find(id)); + _accountsBloc.setActiveAccount(accounts.find(id)!); } }, ),