From 0d876a470f2b51d0f507bb5f9047c0d54966838e Mon Sep 17 00:00:00 2001 From: martyfuhry Date: Wed, 7 Feb 2024 22:54:54 -0500 Subject: [PATCH] feat(mobile): Adds WiFi only backup option to iOS (#6724) Adds WiFi only backup option to iOS Co-authored-by: Marty Fuhry Co-authored-by: Alex Tran --- .../BackgroundServicePlugin.swift | 19 +++++++++++++++++-- .../backup/views/backup_options_page.dart | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mobile/ios/Runner/BackgroundSync/BackgroundServicePlugin.swift b/mobile/ios/Runner/BackgroundSync/BackgroundServicePlugin.swift index 8e5db08b1e..c84b037daf 100644 --- a/mobile/ios/Runner/BackgroundSync/BackgroundServicePlugin.swift +++ b/mobile/ios/Runner/BackgroundSync/BackgroundServicePlugin.swift @@ -9,6 +9,7 @@ import Flutter import BackgroundTasks import path_provider_foundation import CryptoKit +import Network class BackgroundServicePlugin: NSObject, FlutterPlugin { @@ -335,8 +336,8 @@ class BackgroundServicePlugin: NSObject, FlutterPlugin { defaults.set(Date().timeIntervalSince1970, forKey: "last_background_fetch_run_time") // If we have required charging, we should check the charging status - let requireCharging = defaults.value(forKey: "require_charging") as? Bool - if (requireCharging ?? false) { + let requireCharging = defaults.value(forKey: "require_charging") as? Bool ?? false + if (requireCharging) { UIDevice.current.isBatteryMonitoringEnabled = true if (UIDevice.current.batteryState == .unplugged) { // The device is unplugged and we have required charging @@ -347,6 +348,20 @@ class BackgroundServicePlugin: NSObject, FlutterPlugin { } } + // If we have required Wi-Fi, we can check the isExpensive property + let requireWifi = defaults.value(forKey: "require_wifi") as? Bool ?? false + if (requireWifi) { + let wifiMonitor = NWPathMonitor(requiredInterfaceType: .wifi) + let isExpensive = wifiMonitor.currentPath.isExpensive + if (isExpensive) { + // The network is expensive and we have required Wi-Fi + // Therfore, we will simply complete the task without + // running it + task.setTaskCompleted(success: true) + return + } + } + // Schedule the next sync task so we can run this again later scheduleBackgroundFetch() diff --git a/mobile/lib/modules/backup/views/backup_options_page.dart b/mobile/lib/modules/backup/views/backup_options_page.dart index c3beb2f8c5..8144f1b8ed 100644 --- a/mobile/lib/modules/backup/views/backup_options_page.dart +++ b/mobile/lib/modules/backup/views/backup_options_page.dart @@ -264,7 +264,7 @@ class BackupOptionsPage extends HookConsumerWidget { "backup_controller_page_background_description", ).tr(), ), - if (isBackgroundEnabled && Platform.isAndroid) + if (isBackgroundEnabled) SwitchListTile.adaptive( title: const Text("backup_controller_page_background_wifi") .tr(),