42 lines
1.2 KiB
Dart
42 lines
1.2 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'account_event.dart';
|
|
import 'account_state.dart';
|
|
|
|
class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|
AccountBloc({
|
|
AccountStatus initialStatus = AccountStatus.undefined,
|
|
String? accountId,
|
|
}) : super(AccountState(status: initialStatus, accountId: accountId)) {
|
|
on<AccountStatusChanged>(
|
|
(event, emit) => emit(
|
|
AccountState(
|
|
status: event.status,
|
|
accountId: event.status == AccountStatus.loggedOut
|
|
? null
|
|
: state.accountId,
|
|
),
|
|
),
|
|
);
|
|
on<AccountActivated>(
|
|
(event, emit) => emit(
|
|
AccountState(
|
|
status: AccountStatus.loggedIn,
|
|
accountId: event.accountId,
|
|
freshLogin: event.freshLogin,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void setStatus(AccountStatus status) => add(AccountStatusChanged(status));
|
|
|
|
/// [accountId] became the active account (login, switch or takeover after a
|
|
/// sign-out); null means no account is left.
|
|
void activated(String? accountId, {bool freshLogin = false}) => add(
|
|
accountId == null
|
|
? const AccountStatusChanged(AccountStatus.loggedOut)
|
|
: AccountActivated(accountId, freshLogin: freshLogin),
|
|
);
|
|
}
|