From 9955e482a41e92e662856b24759533e51dc2951b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 11 Aug 2025 09:08:38 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20network=20detection=20logic=20to=20proper?= =?UTF-8?q?ly=20handle=20WiFi=20=E2=86=94=20Mobile=20transitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: monkeyWie <13160176+monkeyWie@users.noreply.github.com> --- ui/flutter/lib/util/network_monitor.dart | 41 ++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/ui/flutter/lib/util/network_monitor.dart b/ui/flutter/lib/util/network_monitor.dart index 0ebebc0d..fe476868 100644 --- a/ui/flutter/lib/util/network_monitor.dart +++ b/ui/flutter/lib/util/network_monitor.dart @@ -8,6 +8,8 @@ import '../api/api.dart'; import '../database/database.dart'; import '../util/log_util.dart'; +enum NetworkType { wifi, mobile, other, none } + class NetworkMonitor extends GetxService { static NetworkMonitor get to => Get.find(); @@ -17,7 +19,7 @@ class NetworkMonitor extends GetxService { final _isWiFiConnected = false.obs; bool get isWiFiConnected => _isWiFiConnected.value; - bool _wasWiFiConnected = false; + NetworkType _previousNetworkType = NetworkType.none; bool _isInitialized = false; @override @@ -33,8 +35,8 @@ class NetworkMonitor extends GetxService { // Check initial connectivity state final initialResult = await _connectivity.checkConnectivity(); - _updateWiFiStatus(initialResult); - _wasWiFiConnected = _isWiFiConnected.value; + _updateNetworkStatus(initialResult); + _previousNetworkType = _getNetworkType(initialResult); _isInitialized = true; // Listen to connectivity changes @@ -54,7 +56,20 @@ class NetworkMonitor extends GetxService { super.onClose(); } - void _updateWiFiStatus(List result) { + NetworkType _getNetworkType(List result) { + if (result.contains(ConnectivityResult.wifi)) { + return NetworkType.wifi; + } else if (result.contains(ConnectivityResult.mobile)) { + return NetworkType.mobile; + } else if (result.contains(ConnectivityResult.none)) { + return NetworkType.none; + } else { + // Ethernet, Bluetooth, VPN, or other connection types + return NetworkType.other; + } + } + + void _updateNetworkStatus(List result) { final hasWiFi = result.contains(ConnectivityResult.wifi); _isWiFiConnected.value = hasWiFi; } @@ -68,23 +83,23 @@ class NetworkMonitor extends GetxService { return; } - _updateWiFiStatus(result); - final currentlyWiFi = _isWiFiConnected.value; + _updateNetworkStatus(result); + final currentNetworkType = _getNetworkType(result); - logInfo('Network changed: wasWiFi=$_wasWiFiConnected, currentlyWiFi=$currentlyWiFi'); + logInfo('Network changed: previous=$_previousNetworkType, current=$currentNetworkType'); - // WiFi to Mobile Data: pause all tasks - if (_wasWiFiConnected && !currentlyWiFi) { + // Only handle WiFi ↔ Mobile transitions, ignore other connection types + if (_previousNetworkType == NetworkType.wifi && currentNetworkType == NetworkType.mobile) { _pauseAllDownloads(); logInfo('Switched from WiFi to mobile data - pausing all downloads'); - } - // Mobile Data to WiFi: resume all tasks - else if (!_wasWiFiConnected && currentlyWiFi) { + } else if (_previousNetworkType == NetworkType.mobile && currentNetworkType == NetworkType.wifi) { _resumeAllDownloads(); logInfo('Switched from mobile data to WiFi - resuming all downloads'); + } else { + logInfo('Network change ignored: not a WiFi ↔ Mobile transition'); } - _wasWiFiConnected = currentlyWiFi; + _previousNetworkType = currentNetworkType; } Future _pauseAllDownloads() async {