Browse Source

neon: document AccountsBloc and disable logout with setActiveAccount

pull/341/head
Nikolas Rimikis 2 years ago
parent
commit
7ccfb72c0b
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 41
      packages/neon/neon/lib/src/blocs/accounts.dart
  2. 2
      packages/neon/neon/lib/src/pages/home.dart

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

@ -2,15 +2,39 @@ part of 'blocs.dart';
const _keyAccounts = 'accounts'; 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); 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); 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 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<List<Account>> get accounts; BehaviorSubject<List<Account>> 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<Account?> get activeAccount; BehaviorSubject<Account?> get activeAccount;
} }
@ -111,7 +135,11 @@ 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?.id == account.id) { if (aa?.id == account.id) {
setActiveAccount(as.firstOrNull); if (as.firstOrNull != null) {
setActiveAccount(as.first);
} else {
activeAccount.add(null);
}
} }
unawaited(() async { unawaited(() async {
@ -125,9 +153,8 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
} }
@override @override
void setActiveAccount(final Account? account) { void setActiveAccount(final Account account) {
final as = accounts.value; activeAccount.add(account);
activeAccount.add(account ?? as.firstOrNull);
} }
@override @override

2
packages/neon/neon/lib/src/pages/home.dart

@ -300,7 +300,7 @@ class _HomePageState extends State<HomePage> {
.toList(), .toList(),
onChanged: (final id) { onChanged: (final id) {
if (id != null) { if (id != null) {
_accountsBloc.setActiveAccount(accounts.find(id)); _accountsBloc.setActiveAccount(accounts.find(id)!);
} }
}, },
), ),

Loading…
Cancel
Save