소스 검색

refactor and reuse code for both system notification and dialog notification for app updates

ashilkn 1 년 전
부모
커밋
f465e616c4
2개의 변경된 파일33개의 추가작업 그리고 41개의 파일을 삭제
  1. 19 10
      lib/services/update_service.dart
  2. 14 31
      lib/ui/tabs/home_widget.dart

+ 19 - 10
lib/services/update_service.dart

@@ -73,30 +73,39 @@ class UpdateService {
     return _latestVersion;
     return _latestVersion;
   }
   }
 
 
-  Future<void> showUpdateNotification() async {
-    if (!isIndependent()) {
-      return;
-    }
+  Future<bool> canShowUpdateNoification() async {
     final shouldUpdate = await this.shouldUpdate();
     final shouldUpdate = await this.shouldUpdate();
+
     final lastNotificationShownTime =
     final lastNotificationShownTime =
         _prefs.getInt(kUpdateAvailableShownTimeKey) ?? 0;
         _prefs.getInt(kUpdateAvailableShownTimeKey) ?? 0;
     final now = DateTime.now().microsecondsSinceEpoch;
     final now = DateTime.now().microsecondsSinceEpoch;
-    final hasBeen3DaysSinceLastNotification =
-        (now - lastNotificationShownTime) > (3 * microSecondsInDay);
-    if (shouldUpdate &&
-        hasBeen3DaysSinceLastNotification &&
-        _latestVersion!.shouldNotify) {
+    final hasBeenThresholdDaysSinceLastNotification =
+        (now - lastNotificationShownTime) >
+            ((_latestVersion!.shouldNotify ? 1 : 3) * microSecondsInDay);
+
+    return shouldUpdate && hasBeenThresholdDaysSinceLastNotification;
+  }
+
+  Future<void> showUpdateNotification() async {
+    if (await canShowUpdateNoification()) {
       // ignore: unawaited_futures
       // ignore: unawaited_futures
       NotificationService.instance.showNotification(
       NotificationService.instance.showNotification(
         "Update available",
         "Update available",
         "Click to install our best version yet",
         "Click to install our best version yet",
       );
       );
-      await _prefs.setInt(kUpdateAvailableShownTimeKey, now);
+      await resetUpdateAvailableShownTime();
     } else {
     } else {
       _logger.info("Debouncing notification");
       _logger.info("Debouncing notification");
     }
     }
   }
   }
 
 
+  Future<void> resetUpdateAvailableShownTime() {
+    return _prefs.setInt(
+      kUpdateAvailableShownTimeKey,
+      DateTime.now().microsecondsSinceEpoch,
+    );
+  }
+
   Future<LatestVersionInfo> _getLatestVersionInfo() async {
   Future<LatestVersionInfo> _getLatestVersionInfo() async {
     final response = await NetworkClient.instance
     final response = await NetworkClient.instance
         .getDio()
         .getDio()

+ 14 - 31
lib/ui/tabs/home_widget.dart

@@ -11,7 +11,6 @@ import 'package:media_extension/media_extension_action_types.dart';
 import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
 import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
 import 'package:move_to_background/move_to_background.dart';
 import 'package:move_to_background/move_to_background.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/configuration.dart';
-import "package:photos/core/constants.dart";
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/ente_theme_data.dart';
 import 'package:photos/ente_theme_data.dart';
 import 'package:photos/events/account_configured_event.dart';
 import 'package:photos/events/account_configured_event.dart';
@@ -104,8 +103,6 @@ class _HomeWidgetState extends State<HomeWidget> {
   late StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
   late StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
   late StreamSubscription<AccountConfiguredEvent> _accountConfiguredEvent;
   late StreamSubscription<AccountConfiguredEvent> _accountConfiguredEvent;
   late StreamSubscription<CollectionUpdatedEvent> _collectionUpdatedEvent;
   late StreamSubscription<CollectionUpdatedEvent> _collectionUpdatedEvent;
-  static const kUpdateAvailableDialogShownTimeKey =
-      "update_available_dialog_shown_time_key";
 
 
   @override
   @override
   void initState() {
   void initState() {
@@ -197,37 +194,23 @@ class _HomeWidgetState extends State<HomeWidget> {
       },
       },
     );
     );
     _initDeepLinks();
     _initDeepLinks();
-    UpdateService.instance.shouldUpdate().then((shouldUpdate) {
-      if (shouldUpdate) {
-        Future.delayed(Duration.zero, () {
-          SharedPreferences.getInstance().then(
-            (value) {
-              final lastDialogShownTime =
-                  value.getInt(kUpdateAvailableDialogShownTimeKey) ?? 0;
-              final now = DateTime.now().microsecondsSinceEpoch;
-              final hasBeen3DaysSinceLastShown =
-                  (now - lastDialogShownTime) > (3 * microSecondsInDay);
-
-              if (hasBeen3DaysSinceLastShown) {
-                value.setInt(
-                  kUpdateAvailableDialogShownTimeKey,
-                  DateTime.now().microsecondsSinceEpoch,
-                );
-                showDialog(
-                  context: context,
-                  builder: (BuildContext context) {
-                    return AppUpdateDialog(
-                      UpdateService.instance.getLatestVersionInfo(),
-                    );
-                  },
-                  barrierColor: Colors.black.withOpacity(0.85),
-                );
-              }
+    UpdateService.instance.canShowUpdateNoification().then((value) {
+      Future.delayed(Duration.zero, () {
+        if (value) {
+          showDialog(
+            context: context,
+            builder: (BuildContext context) {
+              return AppUpdateDialog(
+                UpdateService.instance.getLatestVersionInfo(),
+              );
             },
             },
+            barrierColor: Colors.black.withOpacity(0.85),
           );
           );
-        });
-      }
+          UpdateService.instance.resetUpdateAvailableShownTime();
+        }
+      });
     });
     });
+
     // For sharing images coming from outside the app
     // For sharing images coming from outside the app
     _initMediaShareSubscription();
     _initMediaShareSubscription();
     WidgetsBinding.instance.addPostFrameCallback(
     WidgetsBinding.instance.addPostFrameCallback(