veilidchat/lib/account_manager/cubits/active_local_account_cubit.dart

47 lines
1.3 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-14 22:25:02 -05:00
import '../../init.dart';
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(null) {
// Subscribe to streams
_initAccountRepositorySubscription();
2024-02-14 22:25:02 -05:00
// Initialize when we can
Future.delayed(Duration.zero, () async {
await eventualInitialized.future;
emit(_accountRepository.getActiveLocalAccount());
});
2023-12-26 20:26:54 -05:00
}
void _initAccountRepositorySubscription() {
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;
}