veilidchat/lib/account_manager/cubits/account_info_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';
2024-06-16 22:12:24 -04:00
import 'package:veilid_support/veilid_support.dart';
2023-12-26 20:26:54 -05:00
import '../models/models.dart';
2024-06-11 21:27:20 -04:00
import '../repository/account_repository.dart';
2023-12-26 20:26:54 -05:00
2024-06-16 22:12:24 -04:00
class AccountInfoCubit extends Cubit<AccountInfo> {
AccountInfoCubit(
AccountRepository accountRepository, TypedKey superIdentityRecordKey)
2023-12-26 20:26:54 -05:00
: _accountRepository = accountRepository,
2024-06-16 22:12:24 -04:00
super(accountRepository.getAccountInfo(superIdentityRecordKey)) {
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:
2023-12-26 20:26:54 -05:00
case AccountRepositoryChange.localAccounts:
case AccountRepositoryChange.userLogins:
2024-06-16 22:12:24 -04:00
emit(accountRepository.getAccountInfo(superIdentityRecordKey));
2023-12-26 20:26:54 -05:00
break;
}
});
}
@override
Future<void> close() async {
await super.close();
await _accountRepositorySubscription.cancel();
}
final AccountRepository _accountRepository;
late final StreamSubscription<AccountRepositoryChange>
_accountRepositorySubscription;
}