veilidchat/lib/account_manager/cubits/active_local_account_cubit.dart

36 lines
1.1 KiB
Dart
Raw Normal View History

2023-12-26 20:26:54 -05:00
import 'dart:async';
import 'package:bloc/bloc.dart';
2023-12-27 22:56:24 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-12-26 20:26:54 -05:00
2024-02-13 22:03:26 -05:00
import '../repository/account_repository/account_repository.dart';
2023-12-26 20:26:54 -05:00
2024-02-13 22:03:26 -05:00
class ActiveLocalAccountCubit extends Cubit<TypedKey?> {
ActiveLocalAccountCubit(AccountRepository accountRepository)
2023-12-26 20:26:54 -05:00
: _accountRepository = accountRepository,
super(accountRepository.getActiveLocalAccount()) {
2023-12-26 20:26:54 -05:00
// Subscribe to streams
2024-01-08 21:37:08 -05:00
_accountRepositorySubscription = _accountRepository.stream.listen((change) {
2023-12-26 20:26:54 -05:00
switch (change) {
2024-02-13 22:03:26 -05:00
case AccountRepositoryChange.activeLocalAccount:
emit(_accountRepository.getActiveLocalAccount());
2023-12-26 20:26:54 -05:00
break;
// Ignore these
case AccountRepositoryChange.localAccounts:
case AccountRepositoryChange.userLogins:
break;
}
});
}
@override
Future<void> close() async {
await super.close();
await _accountRepositorySubscription.cancel();
}
final AccountRepository _accountRepository;
late final StreamSubscription<AccountRepositoryChange>
_accountRepositorySubscription;
}