Account state now survives reload before redirecting to login

This commit is contained in:
Arseni
2025-12-08 19:50:02 +03:00
parent f478219990
commit 336687eccf
2 changed files with 35 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ class AccountProvider extends ChangeNotifier {
Resource<Account?> get resource => _resource;
late LocaleProvider _localeProvider;
PendingLogin? _pendingLogin;
bool _restoreAttempted = false;
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);
bool get restoreAttempted => _restoreAttempted;
Account? currentUser() {
final acc = account;
@@ -67,6 +69,7 @@ class AccountProvider extends ChangeNotifier {
required String password,
required String locale,
}) async {
_restoreAttempted = true;
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final outcome = await AccountService.login(LoginData.build(
@@ -95,6 +98,7 @@ class AccountProvider extends ChangeNotifier {
void completePendingLogin(Account account) {
_pendingLogin = null;
_restoreAttempted = true;
_setResource(Resource(data: account, isLoading: false, error: null));
_pickupLocale(account.locale);
}
@@ -102,6 +106,7 @@ class AccountProvider extends ChangeNotifier {
Future<bool> isAuthorizationStored() async => AuthorizationService.isAuthorizationStored();
Future<Account?> restore() async {
_restoreAttempted = true;
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final acc = await AccountService.restore();
@@ -140,6 +145,7 @@ class AccountProvider extends ChangeNotifier {
}
Future<void> logout() async {
_restoreAttempted = true;
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
await AccountService.logout();
@@ -220,4 +226,17 @@ class AccountProvider extends ChangeNotifier {
rethrow;
}
}
Future<void> restoreIfPossible() async {
if (_restoreAttempted) return;
_restoreAttempted = true;
final hasAuth = await AuthorizationService.isAuthorizationStored();
if (!hasAuth) {
_setResource(Resource(data: null, isLoading: false, error: null));
return;
}
try {
await restore();
} catch (_) {}
}
}

View File

@@ -17,14 +17,24 @@ class AccountLoader extends StatelessWidget {
@override
Widget build(BuildContext context) => Consumer<AccountProvider>(builder: (context, provider, _) {
if (provider.account != null) return child;
if (!provider.restoreAttempted) {
WidgetsBinding.instance.addPostFrameCallback((_) => provider.restoreIfPossible());
return const Center(child: CircularProgressIndicator());
}
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);
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.account == null) {
WidgetsBinding.instance.addPostFrameCallback((_) => navigateAndReplace(context, Pages.login));