From a2e0dbabe37242fc5b9cec6a8e40d0b10c45c821 Mon Sep 17 00:00:00 2001
From: Ming Ming <nkming2@gmail.com>
Date: Sun, 2 Jan 2022 04:59:02 +0800
Subject: [PATCH] Refine upgrade flow in splash screen

---
 lib/widget/splash.dart | 65 +++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 30 deletions(-)

diff --git a/lib/widget/splash.dart b/lib/widget/splash.dart
index 522c685c..f811a9de 100644
--- a/lib/widget/splash.dart
+++ b/lib/widget/splash.dart
@@ -28,15 +28,18 @@ class _SplashState extends State<Splash> {
   @override
   initState() {
     super.initState();
-    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
-      if (_shouldUpgrade()) {
-        _handleUpgrade();
-      } else {
-        _initTimedExit();
-      }
+    WidgetsBinding.instance!.addPostFrameCallback((_) {
+      _doWork();
     });
   }
 
+  Future<void> _doWork() async {
+    if (_shouldUpgrade()) {
+      await _handleUpgrade();
+    }
+    _initTimedExit();
+  }
+
   @override
   build(BuildContext context) {
     return AppTheme(
@@ -90,7 +93,7 @@ class _SplashState extends State<Splash> {
     return lastVersion < k.version;
   }
 
-  void _handleUpgrade() async {
+  Future<void> _handleUpgrade() async {
     try {
       final lastVersion = Pref().getLastVersionOr(k.version);
       await _upgrade(lastVersion);
@@ -115,41 +118,43 @@ class _SplashState extends State<Splash> {
           ),
         );
       }
+    } catch (e, stackTrace) {
+      _log.shout("[_handleUpgrade] Failed while upgrade", e, stackTrace);
     } finally {
-      _initTimedExit();
-      Pref().setLastVersion(k.version);
+      await Pref().setLastVersion(k.version);
     }
   }
 
   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) {
+      showUpdateDialog();
       await _upgrade29(lastVersion);
     }
+    if (isShowDialog) {
+      Navigator.of(context).pop();
+    }
   }
 
   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 {
-      await fn();
-    } finally {
-      Navigator.of(context).pop();
+      _log.info("[_upgrade29] clearDefaultCache");
+      await CompatV29.clearDefaultCache();
+    } catch (e, stackTrace) {
+      _log.shout("[_upgrade29] Failed while clearDefaultCache", e, stackTrace);
+      // just leave the cache then
     }
   }