3 Commits

3 changed files with 39 additions and 9 deletions

3
.gitignore vendored
View File

@@ -8,4 +8,5 @@ devtools_options.yaml
untranslated.txt
generate_protos.sh
update_dep.sh
.vscode/
.vscode/
GeneratedPluginRegistrant.swift

View File

@@ -27,6 +27,7 @@ class AccountProvider extends ChangeNotifier {
Resource<Account?> get resource => _resource;
late LocaleProvider _localeProvider;
PendingLogin? _pendingLogin;
Future<void>? _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<void>? get restoreFuture => _restoreFuture;
Account? currentUser() {
final acc = account;
@@ -220,4 +222,12 @@ class AccountProvider extends ChangeNotifier {
rethrow;
}
}
Future<void> restoreIfPossible() {
return _restoreFuture ??= () async {
final hasAuth = await AuthorizationService.isAuthorizationStored();
if (!hasAuth) return;
await restore();
}();
}
}

View File

@@ -17,15 +17,34 @@ class AccountLoader extends StatelessWidget {
@override
Widget build(BuildContext context) => Consumer<AccountProvider>(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());