From ac847d8b934a5dae571f3925d9a4e57943fb27e4 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 18 Dec 2024 16:36:39 -0500 Subject: [PATCH] on battery and on data sync options --- lib/entities/background_tasks.dart | 27 ++++++++++++++++++++++----- pubspec_base.yaml | 1 + 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/entities/background_tasks.dart b/lib/entities/background_tasks.dart index 1ccedf007..d88cb7412 100644 --- a/lib/entities/background_tasks.dart +++ b/lib/entities/background_tasks.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:ui'; +import 'package:battery_plus/battery_plus.dart'; import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart'; import 'package:cake_wallet/store/settings_store.dart'; @@ -10,6 +11,7 @@ import 'package:cake_wallet/utils/feature_flag.dart'; import 'package:cake_wallet/view_model/settings/sync_mode.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:cw_bitcoin/electrum_wallet.dart'; import 'package:cw_core/sync_status.dart'; import 'package:cw_core/utils/print_verbose.dart'; @@ -39,6 +41,8 @@ const spNodeNotificationMessage = "Currently configured Bitcoin node does not support Silent Payments. skipping wallet"; const SYNC_THRESHOLD = 0.98; Duration REFRESH_QUEUE_DURATION = Duration(hours: 1); +bool syncOnBattery = false; +bool syncOnData = false; void setMainNotification( FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, { @@ -247,8 +251,6 @@ Future onStart(ServiceInstance service) async { final progressPercent = (progress * 100).toStringAsPrecision(5) + "%"; bool shouldSync = i == 0; - - if (progress > 0.999 && shouldSync) { syncedTicks++; if (syncedTicks > 10) { @@ -338,6 +340,21 @@ Future onStart(ServiceInstance service) async { _queueTimer?.cancel(); // add a timer that checks all wallets and adds them to the queue if they are less than SYNC_THRESHOLD synced: _queueTimer = Timer.periodic(REFRESH_QUEUE_DURATION, (timer) async { + final batteryState = await Battery().batteryState; + bool onBattery = batteryState == BatteryState.connectedNotCharging || + batteryState == BatteryState.discharging; + + ConnectivityResult connectivityResult = await Connectivity().checkConnectivity(); + bool onData = connectivityResult == ConnectivityResult.mobile; + + if (onBattery && !syncOnBattery) { + return; + } + + if (onData && !syncOnData) { + return; + } + for (int i = 0; i < standbyWallets.length; i++) { final wallet = standbyWallets[i]; final syncStatus = wallet.syncStatus; @@ -348,7 +365,7 @@ Future onStart(ServiceInstance service) async { await wallet.startSync(); } - // wait a few seconds before checking progress: + // wait a while before checking progress: await Future.delayed(const Duration(seconds: 20)); if (syncStatus.progress() < SYNC_THRESHOLD) { @@ -500,8 +517,8 @@ class BackgroundTasks { final SyncMode syncMode = settingsStore.currentSyncMode; final bool useNotifications = settingsStore.showSyncNotification; final bool syncEnabled = settingsStore.backgroundSyncEnabled; - final bool syncOnBattery = settingsStore.backgroundSyncOnBattery; - final bool syncOnData = settingsStore.backgroundSyncOnData; + syncOnBattery = settingsStore.backgroundSyncOnBattery; + syncOnData = settingsStore.backgroundSyncOnData; if (useNotifications) { flutterLocalNotificationsPlugin diff --git a/pubspec_base.yaml b/pubspec_base.yaml index 6e79e3cc7..5325b42bf 100644 --- a/pubspec_base.yaml +++ b/pubspec_base.yaml @@ -71,6 +71,7 @@ dependencies: workmanager: ^0.5.2 flutter_background_service: ^5.0.9 flutter_local_notifications: ^17.2.2 + battery_plus: ^6.2.1 wakelock_plus: ^1.2.5 flutter_mailer: git: