70 lines
2.2 KiB
Dart
70 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:pshared/provider/account.dart';
|
|
|
|
import 'package:pweb/app/router/pages.dart';
|
|
import 'package:pweb/widgets/error/snackbar.dart';
|
|
|
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
|
|
|
|
class AccountLoader extends StatelessWidget {
|
|
final Widget child;
|
|
const AccountLoader({super.key, required this.child});
|
|
|
|
static final _logger = Logger('loader.account');
|
|
|
|
void _notifyErrorAndRedirect(BuildContext context, Object error) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!context.mounted) return;
|
|
postNotifyUserOfErrorX(
|
|
context: context,
|
|
errorSituation: AppLocalizations.of(context)!.errorLogin,
|
|
exception: error,
|
|
);
|
|
navigateAndReplace(context, Pages.login);
|
|
});
|
|
}
|
|
|
|
void _restoreAndHandleResult(BuildContext context, AccountProvider provider) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
try {
|
|
await provider.restoreIfPossible();
|
|
} catch (error, stack) {
|
|
_logger.warning('Account restore failed: $error', error, stack);
|
|
}
|
|
if (!context.mounted) return;
|
|
final latest = Provider.of<AccountProvider>(context, listen: false);
|
|
if (latest.account != null) return;
|
|
if (latest.error != null) {
|
|
_notifyErrorAndRedirect(context, latest.error!);
|
|
return;
|
|
}
|
|
if (!latest.isLoading) {
|
|
navigateAndReplace(context, Pages.login);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<AccountProvider>(builder: (context, provider, _) {
|
|
if (provider.account != null) return child;
|
|
if (provider.error != null) {
|
|
_notifyErrorAndRedirect(context, provider.error!);
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (!provider.isLoading) {
|
|
_restoreAndHandleResult(context, provider);
|
|
}
|
|
if (provider.isLoading) return const Center(child: CircularProgressIndicator());
|
|
// In case of error or missing account we show loader while side effects handle navigation.
|
|
return const Center(child: CircularProgressIndicator());
|
|
});
|
|
}
|
|
}
|