Account state now survives reload before redirecting to login #40
@@ -27,7 +27,7 @@ class AccountProvider extends ChangeNotifier {
|
||||
Resource<Account?> get resource => _resource;
|
||||
late LocaleProvider _localeProvider;
|
||||
PendingLogin? _pendingLogin;
|
||||
bool _restoreAttempted = false;
|
||||
Future<void>? _restoreFuture;
|
||||
|
||||
Account? get account => _resource.data;
|
||||
PendingLogin? get pendingLogin => _pendingLogin;
|
||||
@@ -35,7 +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;
|
||||
Future<void>? get restoreFuture => _restoreFuture;
|
||||
|
||||
Account? currentUser() {
|
||||
final acc = account;
|
||||
@@ -69,7 +69,6 @@ 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(
|
||||
@@ -98,7 +97,6 @@ class AccountProvider extends ChangeNotifier {
|
||||
|
||||
void completePendingLogin(Account account) {
|
||||
_pendingLogin = null;
|
||||
_restoreAttempted = true;
|
||||
_setResource(Resource(data: account, isLoading: false, error: null));
|
||||
_pickupLocale(account.locale);
|
||||
}
|
||||
@@ -106,7 +104,6 @@ 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();
|
||||
@@ -145,7 +142,6 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
_restoreAttempted = true;
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
await AccountService.logout();
|
||||
@@ -227,16 +223,11 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
Future<void> restoreIfPossible() {
|
||||
return _restoreFuture ??= () async {
|
||||
final hasAuth = await AuthorizationService.isAuthorizationStored();
|
||||
if (!hasAuth) return;
|
||||
await restore();
|
||||
} catch (_) {}
|
||||
}();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,17 @@ class AccountLoader extends StatelessWidget {
|
||||
});
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user