PostHog last fixes hopefully
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
@@ -12,24 +14,56 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
class AccountLoader extends StatelessWidget {
|
||||
final Widget child;
|
||||
|
||||
const AccountLoader({super.key, required this.child});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Consumer<AccountProvider>(builder: (context, provider, _) {
|
||||
if (provider.isLoading) return const Center(child: CircularProgressIndicator());
|
||||
if (provider.error != null) {
|
||||
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: provider.error!,
|
||||
context: context,
|
||||
errorSituation: AppLocalizations.of(context)!.errorLogin,
|
||||
exception: error,
|
||||
);
|
||||
navigateAndReplace(context, Pages.login);
|
||||
}
|
||||
if (provider.account == null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => 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());
|
||||
}
|
||||
return child;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user