diff --git a/.gitignore b/.gitignore index e663d96..ae95915 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ devtools_options.yaml untranslated.txt generate_protos.sh update_dep.sh -.vscode/ \ No newline at end of file +.vscode/ +GeneratedPluginRegistrant.swift \ No newline at end of file diff --git a/frontend/pshared/lib/provider/account.dart b/frontend/pshared/lib/provider/account.dart index 9413913..644907c 100644 --- a/frontend/pshared/lib/provider/account.dart +++ b/frontend/pshared/lib/provider/account.dart @@ -27,6 +27,7 @@ class AccountProvider extends ChangeNotifier { Resource get resource => _resource; late LocaleProvider _localeProvider; PendingLogin? _pendingLogin; + Future? _restoreFuture; Account? get account => _resource.data; PendingLogin? get pendingLogin => _pendingLogin; @@ -34,6 +35,7 @@ class AccountProvider extends ChangeNotifier { bool get isLoading => _resource.isLoading; Object? get error => _resource.error; bool get isReady => (!isLoading) && (account != null); + Future? get restoreFuture => _restoreFuture; Account? currentUser() { final acc = account; @@ -220,4 +222,12 @@ class AccountProvider extends ChangeNotifier { rethrow; } } + + Future restoreIfPossible() { + return _restoreFuture ??= () async { + final hasAuth = await AuthorizationService.isAuthorizationStored(); + if (!hasAuth) return; + await restore(); + }(); + } } diff --git a/frontend/pweb/lib/pages/loaders/account.dart b/frontend/pweb/lib/pages/loaders/account.dart index d891e1c..14200d9 100644 --- a/frontend/pweb/lib/pages/loaders/account.dart +++ b/frontend/pweb/lib/pages/loaders/account.dart @@ -17,15 +17,34 @@ class AccountLoader extends StatelessWidget { @override Widget build(BuildContext context) => Consumer(builder: (context, provider, _) { - if (provider.isLoading) return const Center(child: CircularProgressIndicator()); - if (provider.error != null) { - postNotifyUserOfErrorX( - context: context, - errorSituation: AppLocalizations.of(context)!.errorLogin, - exception: provider.error!, - ); - navigateAndReplace(context, Pages.login); + if (provider.account != null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + }); + return child; } + + if (provider.error != null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + postNotifyUserOfErrorX( + context: context, + errorSituation: AppLocalizations.of(context)!.errorLogin, + exception: provider.error!, + ); + navigateAndReplace(context, Pages.login); + }); + return const Center(child: CircularProgressIndicator()); + } + + if (provider.restoreFuture == null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + provider.restoreIfPossible().catchError((error, stack) { + debugPrint('Account restore failed: $error'); + }); + }); + return const Center(child: CircularProgressIndicator()); + } + + if (provider.isLoading) return const Center(child: CircularProgressIndicator()); if (provider.account == null) { WidgetsBinding.instance.addPostFrameCallback((_) => navigateAndReplace(context, Pages.login)); return const Center(child: CircularProgressIndicator());