Fix routing to home after initial account creation

This commit is contained in:
Brandon Vandegrift 2025-04-03 14:27:56 -04:00
parent f4407e5284
commit e1f081105a
3 changed files with 27 additions and 16 deletions

View file

@ -63,10 +63,13 @@ class _NewAccountPageState extends WindowSetupState<NewAccountPage> {
return false; return false;
} }
final isFirstAccount =
AccountRepository.instance.getLocalAccounts().isEmpty;
final writableSuperIdentity = await AccountRepository.instance final writableSuperIdentity = await AccountRepository.instance
.createWithNewSuperIdentity(accountSpec); .createWithNewSuperIdentity(accountSpec);
GoRouterHelper(context).pushReplacement('/new_account/recovery_key', GoRouterHelper(context).pushReplacement('/new_account/recovery_key',
extra: [writableSuperIdentity, accountSpec.name]); extra: [writableSuperIdentity, accountSpec.name, isFirstAccount]);
return true; return true;
} finally { } finally {
@ -92,11 +95,15 @@ class _NewAccountPageState extends WindowSetupState<NewAccountPage> {
return StyledScaffold( return StyledScaffold(
appBar: DefaultAppBar( appBar: DefaultAppBar(
title: Text(translate('new_account_page.titlebar')), title: Text(translate('new_account_page.titlebar')),
leading: Navigator.canPop(context) leading: GoRouterHelper(context).canPop()
? IconButton( ? IconButton(
icon: const Icon(Icons.arrow_back), icon: const Icon(Icons.arrow_back),
onPressed: () { onPressed: () {
Navigator.pop(context); if (GoRouterHelper(context).canPop()) {
GoRouterHelper(context).pop();
} else {
GoRouterHelper(context).go('/');
}
}, },
) )
: null, : null,

View file

@ -25,15 +25,18 @@ class ShowRecoveryKeyPage extends StatefulWidget {
const ShowRecoveryKeyPage( const ShowRecoveryKeyPage(
{required WritableSuperIdentity writableSuperIdentity, {required WritableSuperIdentity writableSuperIdentity,
required String name, required String name,
required bool isFirstAccount,
super.key}) super.key})
: _writableSuperIdentity = writableSuperIdentity, : _writableSuperIdentity = writableSuperIdentity,
_name = name; _name = name,
_isFirstAccount = isFirstAccount;
@override @override
State<ShowRecoveryKeyPage> createState() => _ShowRecoveryKeyPageState(); State<ShowRecoveryKeyPage> createState() => _ShowRecoveryKeyPageState();
final WritableSuperIdentity _writableSuperIdentity; final WritableSuperIdentity _writableSuperIdentity;
final String _name; final String _name;
final bool _isFirstAccount;
} }
class _ShowRecoveryKeyPageState extends WindowSetupState<ShowRecoveryKeyPage> { class _ShowRecoveryKeyPageState extends WindowSetupState<ShowRecoveryKeyPage> {
@ -248,10 +251,14 @@ class _ShowRecoveryKeyPageState extends WindowSetupState<ShowRecoveryKeyPage> {
child: ElevatedButton( child: ElevatedButton(
onPressed: () { onPressed: () {
if (context.mounted) { if (context.mounted) {
Navigator.canPop(context) if (widget._isFirstAccount) {
GoRouterHelper(context).go('/');
} else {
GoRouterHelper(context).canPop()
? GoRouterHelper(context).pop() ? GoRouterHelper(context).pop()
: GoRouterHelper(context).go('/'); : GoRouterHelper(context).go('/');
} }
}
}, },
child: Text(translate('button.finish')).paddingAll(8)) child: Text(translate('button.finish')).paddingAll(8))
.paddingAll(12)) .paddingAll(12))

View file

@ -96,7 +96,8 @@ class RouterCubit extends Cubit<RouterState> {
if (extra == null || if (extra == null ||
extra is! List<Object> || extra is! List<Object> ||
extra[0] is! WritableSuperIdentity || extra[0] is! WritableSuperIdentity ||
extra[1] is! String) { extra[1] is! String ||
extra[2] is! bool) {
return '/'; return '/';
} }
return null; return null;
@ -107,7 +108,8 @@ class RouterCubit extends Cubit<RouterState> {
return ShowRecoveryKeyPage( return ShowRecoveryKeyPage(
writableSuperIdentity: writableSuperIdentity:
extra[0] as WritableSuperIdentity, extra[0] as WritableSuperIdentity,
name: extra[1] as String); name: extra[1] as String,
isFirstAccount: extra[2] as bool);
}), }),
]), ]),
GoRoute( GoRoute(
@ -123,14 +125,8 @@ class RouterCubit extends Cubit<RouterState> {
/// Redirects when our state changes /// Redirects when our state changes
String? redirect(BuildContext context, GoRouterState goRouterState) { String? redirect(BuildContext context, GoRouterState goRouterState) {
// No matter where we are, if there's not
switch (goRouterState.matchedLocation) { switch (goRouterState.matchedLocation) {
case '/': // We can go to any of these routes without an account.
if (!state.hasAnyAccount) {
return '/new_account';
}
return null;
case '/new_account': case '/new_account':
return null; return null;
case '/new_account/recovery_key': case '/new_account/recovery_key':
@ -139,6 +135,7 @@ class RouterCubit extends Cubit<RouterState> {
return null; return null;
case '/developer': case '/developer':
return null; return null;
// Otherwise, if there's no account, we need to go to the new account page.
default: default:
return state.hasAnyAccount ? null : '/new_account'; return state.hasAnyAccount ? null : '/new_account';
} }