veilidchat/lib/account_manager/cubits/account_info_cubit.dart

41 lines
1.2 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(
2024-06-18 21:20:06 -04:00
{required AccountRepository accountRepository,
required TypedKey superIdentityRecordKey})
2023-12-26 20:26:54 -05:00
: _accountRepository = accountRepository,
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:
final acctInfo =
accountRepository.getAccountInfo(superIdentityRecordKey);
if (acctInfo != null) {
emit(acctInfo);
}
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;
}