Refine upgrade flow in splash screen

This commit is contained in:
Ming Ming 2022-01-02 04:59:02 +08:00
parent 877d50d2a2
commit a2e0dbabe3

View file

@ -28,15 +28,18 @@ class _SplashState extends State<Splash> {
@override @override
initState() { initState() {
super.initState(); super.initState();
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) { WidgetsBinding.instance!.addPostFrameCallback((_) {
if (_shouldUpgrade()) { _doWork();
_handleUpgrade();
} else {
_initTimedExit();
}
}); });
} }
Future<void> _doWork() async {
if (_shouldUpgrade()) {
await _handleUpgrade();
}
_initTimedExit();
}
@override @override
build(BuildContext context) { build(BuildContext context) {
return AppTheme( return AppTheme(
@ -90,7 +93,7 @@ class _SplashState extends State<Splash> {
return lastVersion < k.version; return lastVersion < k.version;
} }
void _handleUpgrade() async { Future<void> _handleUpgrade() async {
try { try {
final lastVersion = Pref().getLastVersionOr(k.version); final lastVersion = Pref().getLastVersionOr(k.version);
await _upgrade(lastVersion); await _upgrade(lastVersion);
@ -115,41 +118,43 @@ class _SplashState extends State<Splash> {
), ),
); );
} }
} catch (e, stackTrace) {
_log.shout("[_handleUpgrade] Failed while upgrade", e, stackTrace);
} finally { } finally {
_initTimedExit(); await Pref().setLastVersion(k.version);
Pref().setLastVersion(k.version);
} }
} }
Future<void> _upgrade(int lastVersion) async { Future<void> _upgrade(int lastVersion) async {
bool isShowDialog = false;
void showUpdateDialog() {
if (!isShowDialog) {
isShowDialog = true;
showDialog(
context: context,
builder: (_) => ProcessingDialog(
text: L10n.global().genericProcessingDialogContent,
),
);
}
}
if (lastVersion < 290) { if (lastVersion < 290) {
showUpdateDialog();
await _upgrade29(lastVersion); await _upgrade29(lastVersion);
} }
if (isShowDialog) {
Navigator.of(context).pop();
}
} }
Future<void> _upgrade29(int lastVersion) async { Future<void> _upgrade29(int lastVersion) async {
await _peformUpgrade(() async {
try {
_log.info("[_upgrade29] clearDefaultCache");
await CompatV29.clearDefaultCache();
} catch (e, stackTrace) {
_log.shout(
"[_upgrade29] Failed while clearDefaultCache", e, stackTrace);
// just leave the cache then
}
});
}
Future<void> _peformUpgrade(FutureOr<void> Function() fn) async {
showDialog(
context: context,
builder: (_) => ProcessingDialog(
text: L10n.global().genericProcessingDialogContent,
));
try { try {
await fn(); _log.info("[_upgrade29] clearDefaultCache");
} finally { await CompatV29.clearDefaultCache();
Navigator.of(context).pop(); } catch (e, stackTrace) {
_log.shout("[_upgrade29] Failed while clearDefaultCache", e, stackTrace);
// just leave the cache then
} }
} }