From 9f1b9548c338b229a20e8d29decff05b71624906 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 11 Sep 2024 09:57:45 -0700 Subject: [PATCH] save --- lib/entities/background_tasks.dart | 85 ++++++++++++++++++------------ lib/entities/preferences_key.dart | 1 + lib/src/screens/root/root.dart | 8 +-- lib/store/settings_store.dart | 11 ++++ 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index b7b764ea6..6cb3ba079 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -98,6 +98,7 @@ Future onStart(ServiceInstance service) async { await currentWallet?.stopSync(); final walletLoadingService = getIt.get(); + final settingsStore = getIt.get(); final typeRaw = getIt.get().getInt(PreferencesKey.currentWalletType); bool syncAll = true; @@ -171,10 +172,10 @@ Future onStart(ServiceInstance service) async { return; } - final wallet = syncingWallets.first!; - final syncProgress = ((wallet.syncStatus.progress() ?? 0) * 100).toStringAsPrecision(5); - + // final wallet = syncingWallets.first!; + // final syncProgress = ((wallet.syncStatus.progress() ?? 0) * 100).toStringAsPrecision(5); // String title = "${wallet!.name} ${syncProgress}% Synced"; + String title = ""; for (int i = 0; i < syncingWallets.length; i++) { @@ -208,36 +209,38 @@ Future onIosBackground(ServiceInstance service) async { return true; } -Future initializeService(FlutterBackgroundService bgService) async { - const AndroidNotificationChannel channel = AndroidNotificationChannel( - notificationChannelId, - notificationChannelName, - description: notificationChannelDescription, - importance: Importance.low, - ); - - final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = - FlutterLocalNotificationsPlugin(); - - if (Platform.isIOS || Platform.isAndroid) { - await flutterLocalNotificationsPlugin.initialize( - const InitializationSettings( - iOS: DarwinInitializationSettings(), - android: AndroidInitializationSettings('ic_bg_service_small'), - ), +Future initializeService(FlutterBackgroundService bgService, bool useNotifications) async { + if (useNotifications) { + const AndroidNotificationChannel channel = AndroidNotificationChannel( + notificationChannelId, + notificationChannelName, + description: notificationChannelDescription, + importance: Importance.low, ); + + final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = + FlutterLocalNotificationsPlugin(); + + if (Platform.isIOS || Platform.isAndroid) { + await flutterLocalNotificationsPlugin.initialize( + const InitializationSettings( + iOS: DarwinInitializationSettings(), + android: AndroidInitializationSettings('ic_bg_service_small'), + ), + ); + } + + await flutterLocalNotificationsPlugin + .resolvePlatformSpecificImplementation() + ?.createNotificationChannel(channel); + + flutterLocalNotificationsPlugin + .resolvePlatformSpecificImplementation() + ?.requestNotificationsPermission(); + + setNotificationStandby(flutterLocalNotificationsPlugin); } - await flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation() - ?.createNotificationChannel(channel); - - flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation() - ?.requestNotificationsPermission(); - - setNotificationStandby(flutterLocalNotificationsPlugin); - // notify the service that we are in the foreground: bgService.invoke("setForeground"); @@ -271,10 +274,10 @@ Future initializeService(FlutterBackgroundService bgService) async { class BackgroundTasks { FlutterBackgroundService bgService = FlutterBackgroundService(); - void updateServiceState(bool foreground) { + void updateServiceState(bool foreground, bool useNotifications) async { if (foreground) { bgService.invoke('stopService'); - initializeService(bgService); + initializeService(bgService, useNotifications); } else { bgService.invoke("setBackground"); } @@ -292,12 +295,24 @@ class BackgroundTasks { .wallets .any((element) => element.type == WalletType.litecoin); + bool hasBitcoin = getIt + .get() + .wallets + .any((element) => element.type == WalletType.bitcoin); + + final settingsStore = getIt.get(); + if (!settingsStore.silentPaymentsAlwaysScan) { + hasBitcoin = false; + } + if (!settingsStore.mwebAlwaysScan) { + hasLitecoin = false; + } + /// if its not android nor ios, or the user has no monero wallets; exit - if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin)) { + if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin && !hasBitcoin)) { return; } - final settingsStore = getIt.get(); final SyncMode syncMode = settingsStore.currentSyncMode; final bool syncAll = settingsStore.currentSyncAll; @@ -309,7 +324,7 @@ class BackgroundTasks { bgService.invoke('stopService'); - await initializeService(bgService); + await initializeService(bgService, settingsStore.showSyncNotification); } catch (error, stackTrace) { print(error); print(stackTrace); diff --git a/lib/entities/preferences_key.dart b/lib/entities/preferences_key.dart index 57b74bf26..6158e07a6 100644 --- a/lib/entities/preferences_key.dart +++ b/lib/entities/preferences_key.dart @@ -58,6 +58,7 @@ class PreferencesKey { static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1'; static const syncModeKey = 'sync_mode'; static const syncAllKey = 'sync_all'; + static const showSyncNotificationKey = 'show_sync_notification'; static const lastPopupDate = 'last_popup_date'; static const lastAppReviewDate = 'last_app_review_date'; static const sortBalanceBy = 'sort_balance_by'; diff --git a/lib/src/screens/root/root.dart b/lib/src/screens/root/root.dart index ab052d0ea..9c45a9842 100644 --- a/lib/src/screens/root/root.dart +++ b/lib/src/screens/root/root.dart @@ -4,6 +4,7 @@ import 'package:cake_wallet/core/auth_service.dart'; import 'package:cake_wallet/core/totp_request_details.dart'; import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/entities/background_tasks.dart'; +import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/utils/device_info.dart'; import 'package:cake_wallet/view_model/link_view_model.dart'; import 'package:cw_core/wallet_base.dart'; @@ -161,10 +162,11 @@ class RootState extends State with WidgetsBindingObserver { } // background service handling: + bool showNotifications = getIt.get().showSyncNotification; switch (state) { case AppLifecycleState.resumed: // restart the background service if it was running before: - getIt.get().updateServiceState(true); + getIt.get().updateServiceState(true, showNotifications); _stateTimer?.cancel(); if (!wasInBackground) { return; @@ -178,7 +180,7 @@ class RootState extends State with WidgetsBindingObserver { } break; case AppLifecycleState.paused: - getIt.get().updateServiceState(false); + getIt.get().updateServiceState(false, showNotifications); case AppLifecycleState.inactive: case AppLifecycleState.detached: default: @@ -187,7 +189,7 @@ class RootState extends State with WidgetsBindingObserver { _stateTimer?.cancel(); _stateTimer = Timer(const Duration(seconds: 10), () async { wasInBackground = true; - getIt.get().updateServiceState(false); + getIt.get().updateServiceState(false, showNotifications); }); break; } diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart index 1598664ee..b2e5a9fd2 100644 --- a/lib/store/settings_store.dart +++ b/lib/store/settings_store.dart @@ -75,6 +75,7 @@ abstract class SettingsStoreBase with Store { required String initialLanguageCode, required SyncMode initialSyncMode, required bool initialSyncAll, + required bool initialShowSyncNotification, // required String initialCurrentLocale, required this.appVersion, required this.deviceName, @@ -169,6 +170,7 @@ abstract class SettingsStoreBase with Store { initialShouldRequireTOTP2FAForAllSecurityAndBackupSettings, currentSyncMode = initialSyncMode, currentSyncAll = initialSyncAll, + showSyncNotification = initialShowSyncNotification, priority = ObservableMap(), defaultBuyProviders = ObservableMap(), defaultSellProviders = ObservableMap() { @@ -389,7 +391,11 @@ abstract class SettingsStoreBase with Store { reaction((_) => currentSyncAll, (bool syncAll) { sharedPreferences.setBool(PreferencesKey.syncAllKey, syncAll); + _backgroundTasks.registerBackgroundService(); + }); + reaction((_) => showSyncNotification, (bool value) { + sharedPreferences.setBool(PreferencesKey.showSyncNotificationKey, value); _backgroundTasks.registerBackgroundService(); }); @@ -766,6 +772,9 @@ abstract class SettingsStoreBase with Store { @observable bool currentSyncAll; + @observable + bool showSyncNotification; + String appVersion; String deviceName; @@ -1068,6 +1077,7 @@ abstract class SettingsStoreBase with Store { return element.type.index == (sharedPreferences.getInt(PreferencesKey.syncModeKey) ?? 0); }); final savedSyncAll = sharedPreferences.getBool(PreferencesKey.syncAllKey) ?? true; + final savedShowSyncNotification = sharedPreferences.getBool(PreferencesKey.showSyncNotificationKey) ?? false; // migrated to secure: final timeOutDuration = await SecureKey.getInt( @@ -1241,6 +1251,7 @@ abstract class SettingsStoreBase with Store { backgroundTasks: backgroundTasks, initialSyncMode: savedSyncMode, initialSyncAll: savedSyncAll, + initialShowSyncNotification: savedShowSyncNotification, shouldShowYatPopup: shouldShowYatPopup, shouldShowRepWarning: shouldShowRepWarning, );