Account state now survives reload before redirecting to login
This commit is contained in:
@@ -27,6 +27,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
Resource<Account?> get resource => _resource;
|
Resource<Account?> get resource => _resource;
|
||||||
late LocaleProvider _localeProvider;
|
late LocaleProvider _localeProvider;
|
||||||
PendingLogin? _pendingLogin;
|
PendingLogin? _pendingLogin;
|
||||||
|
bool _restoreAttempted = false;
|
||||||
|
|
||||||
Account? get account => _resource.data;
|
Account? get account => _resource.data;
|
||||||
PendingLogin? get pendingLogin => _pendingLogin;
|
PendingLogin? get pendingLogin => _pendingLogin;
|
||||||
@@ -34,6 +35,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
bool get isLoading => _resource.isLoading;
|
bool get isLoading => _resource.isLoading;
|
||||||
Object? get error => _resource.error;
|
Object? get error => _resource.error;
|
||||||
bool get isReady => (!isLoading) && (account != null);
|
bool get isReady => (!isLoading) && (account != null);
|
||||||
|
bool get restoreAttempted => _restoreAttempted;
|
||||||
|
|
||||||
Account? currentUser() {
|
Account? currentUser() {
|
||||||
final acc = account;
|
final acc = account;
|
||||||
@@ -67,6 +69,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
required String password,
|
required String password,
|
||||||
required String locale,
|
required String locale,
|
||||||
}) async {
|
}) async {
|
||||||
|
_restoreAttempted = true;
|
||||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||||
try {
|
try {
|
||||||
final outcome = await AccountService.login(LoginData.build(
|
final outcome = await AccountService.login(LoginData.build(
|
||||||
@@ -95,6 +98,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
|
|
||||||
void completePendingLogin(Account account) {
|
void completePendingLogin(Account account) {
|
||||||
_pendingLogin = null;
|
_pendingLogin = null;
|
||||||
|
_restoreAttempted = true;
|
||||||
_setResource(Resource(data: account, isLoading: false, error: null));
|
_setResource(Resource(data: account, isLoading: false, error: null));
|
||||||
_pickupLocale(account.locale);
|
_pickupLocale(account.locale);
|
||||||
}
|
}
|
||||||
@@ -102,6 +106,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
Future<bool> isAuthorizationStored() async => AuthorizationService.isAuthorizationStored();
|
Future<bool> isAuthorizationStored() async => AuthorizationService.isAuthorizationStored();
|
||||||
|
|
||||||
Future<Account?> restore() async {
|
Future<Account?> restore() async {
|
||||||
|
_restoreAttempted = true;
|
||||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||||
try {
|
try {
|
||||||
final acc = await AccountService.restore();
|
final acc = await AccountService.restore();
|
||||||
@@ -140,6 +145,7 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> logout() async {
|
Future<void> logout() async {
|
||||||
|
_restoreAttempted = true;
|
||||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||||
try {
|
try {
|
||||||
await AccountService.logout();
|
await AccountService.logout();
|
||||||
@@ -220,4 +226,17 @@ class AccountProvider extends ChangeNotifier {
|
|||||||
rethrow;
|
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 (_) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,24 @@ class AccountLoader extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => Consumer<AccountProvider>(builder: (context, provider, _) {
|
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.isLoading) return const Center(child: CircularProgressIndicator());
|
||||||
if (provider.error != null) {
|
if (provider.error != null) {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
postNotifyUserOfErrorX(
|
postNotifyUserOfErrorX(
|
||||||
context: context,
|
context: context,
|
||||||
errorSituation: AppLocalizations.of(context)!.errorLogin,
|
errorSituation: AppLocalizations.of(context)!.errorLogin,
|
||||||
exception: provider.error!,
|
exception: provider.error!,
|
||||||
);
|
);
|
||||||
navigateAndReplace(context, Pages.login);
|
navigateAndReplace(context, Pages.login);
|
||||||
|
});
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
}
|
}
|
||||||
if (provider.account == null) {
|
if (provider.account == null) {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) => navigateAndReplace(context, Pages.login));
|
WidgetsBinding.instance.addPostFrameCallback((_) => navigateAndReplace(context, Pages.login));
|
||||||
|
|||||||
Reference in New Issue
Block a user