refactor cubits to keep them alive, wip

This commit is contained in:
Christien Rioux 2024-06-17 23:38:30 -04:00
parent 360ba436f8
commit 3edf2ebb46
13 changed files with 550 additions and 158 deletions

View file

@ -164,44 +164,41 @@ class HomeScreenState extends State<HomeScreen> {
], child: Builder(builder: _buildAccountReadyDeviceSpecific)));
}
Widget _buildAccount(BuildContext context) {
// Get active account info status
final (
accountInfoStatus,
accountInfoActive,
superIdentityRecordKey
) = context
.select<AccountInfoCubit, (AccountInfoStatus, bool, TypedKey?)>((c) => (
c.state.status,
c.state.active,
c.state.unlockedAccountInfo?.superIdentityRecordKey
));
Widget _buildAccount(BuildContext context, TypedKey superIdentityRecordKey) =>
BlocProvider<AccountInfoCubit>(
key: ValueKey(superIdentityRecordKey),
create: (context) => AccountInfoCubit(
AccountRepository.instance, superIdentityRecordKey),
child: Builder(builder: (context) {
// Get active account info status
final accountInfoStatus =
context.select<AccountInfoCubit, AccountInfoStatus>(
(c) => c.state.status);
switch (accountInfoStatus) {
case AccountInfoStatus.noAccount:
return const HomeAccountMissing();
case AccountInfoStatus.accountInvalid:
return const HomeAccountInvalid();
case AccountInfoStatus.accountLocked:
return const HomeAccountLocked();
case AccountInfoStatus.accountUnlocked:
switch (accountInfoStatus) {
case AccountInfoStatus.noAccount:
return const HomeAccountMissing();
case AccountInfoStatus.accountInvalid:
return const HomeAccountInvalid();
case AccountInfoStatus.accountLocked:
return const HomeAccountLocked();
case AccountInfoStatus.accountUnlocked:
// Get the current active account record cubit
final activeAccountRecordCubit =
context.select<AccountRecordsBlocMapCubit, AccountRecordCubit?>(
(c) => superIdentityRecordKey == null
? null
: c.tryOperate(superIdentityRecordKey, closure: (x) => x));
if (activeAccountRecordCubit == null) {
return waitingPage();
}
// Get the current active account record cubit
final activeAccountRecordCubit = context
.select<AccountRecordsBlocMapCubit, AccountRecordCubit?>(
(c) => c.tryOperate(superIdentityRecordKey,
closure: (x) => x));
if (activeAccountRecordCubit == null) {
return waitingPage();
}
return MultiBlocProvider(providers: [
BlocProvider<AccountRecordCubit>.value(
value: activeAccountRecordCubit),
], child: Builder(builder: _buildUnlockedAccount));
}
}
return MultiBlocProvider(providers: [
BlocProvider<AccountRecordCubit>.value(
value: activeAccountRecordCubit),
], child: Builder(builder: _buildUnlockedAccount));
}
}));
Widget _buildAccountPageView(BuildContext context) {
final localAccounts = context.watch<LocalAccountsCubit>().state;
@ -221,8 +218,7 @@ class HomeScreenState extends State<HomeScreen> {
value.dispose();
},
child: Builder(
builder: (context) => PageView.builder(
itemCount: localAccounts.length,
builder: (context) => PageView.custom(
onPageChanged: (idx) {
singleFuture(this, () async {
await AccountRepository.instance.switchToAccount(
@ -232,15 +228,10 @@ class HomeScreenState extends State<HomeScreen> {
controller: context
.read<ActiveAccountPageControllerWrapper>()
.pageController,
itemBuilder: (context, index) {
final localAccount = localAccounts[index];
return BlocProvider<AccountInfoCubit>(
key: ValueKey(localAccount.superIdentity.recordKey),
create: (context) => AccountInfoCubit(
AccountRepository.instance,
localAccount.superIdentity.recordKey),
child: Builder(builder: _buildAccount));
})));
childrenDelegate: SliverChildListDelegate(localAccounts
.map((la) =>
_buildAccount(context, la.superIdentity.recordKey))
.toList()))));
}
@override