From 52cf570f6cc0d1b204e444f8704db586617777eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=99=9E=E6=B8=A0=E6=88=90=E2=80=9D?= <“yuqucheng2006@qq.com”> Date: Wed, 9 Sep 2026 14:08:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(HomePage):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=8A=B6=E6=80=81=E8=BD=AE=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E6=AF=94?= =?UTF-8?q?=E5=AF=B9=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增应用前后台状态监听,仅前台执行轮询 2. 调整轮询间隔为3秒,增加轮询空值校验 3. 新增设备配置变更检测,自动刷新主页UI --- .../Scripts/UI/Pages/HomePage/HomePageCtrl.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs index 614860c..2837542 100644 --- a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs +++ b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs @@ -41,6 +41,7 @@ namespace Kill.UI.Pages bool hasWifi = false; bool isPollingOnline = false; bool stopPolling = false; + bool isAppForeground = true; // 是否处于前台:仅前台进行在线状态轮询 // UI 组件引用 public HomePageDeviceState deviceState; @@ -117,8 +118,13 @@ namespace Kill.UI.Pages } void OnApplicationPause(bool pause) { + isAppForeground = !pause; LockButtonPlane.SetActive(true); } + void OnApplicationFocus(bool hasFocus) + { + isAppForeground = hasFocus; + } void OnDestroy() { StopDeviceOnlineStatusPolling(); @@ -2021,8 +2027,15 @@ namespace Kill.UI.Pages while (!stopPolling && selectedDevice != null) { - // 每 5s 检测一次设备 WiFi 在线状态 - await System.Threading.Tasks.Task.Delay(5000); + // 每 3s 检测一次设备 WiFi 在线状态 + await System.Threading.Tasks.Task.Delay(3000); + + if (stopPolling || selectedDevice == null) + break; + + // 仅前台进行轮询:后台时挂起等待,切回前台后立即执行一次 + while (!stopPolling && !isAppForeground) + await System.Threading.Tasks.Task.Delay(500); if (stopPolling || selectedDevice == null) break; @@ -2084,6 +2097,23 @@ namespace Kill.UI.Pages deviceState?.UpdateDeviceState(hasBluetooth, hasWifi); }); } + + // 在线状态处理后拉取一次设备配置,与本地快照比对,有变化则刷新主页UI + DeviceConfig configBefore = DataManager.Instance.deviceConfig?.Clone(); + await DataManager.Instance.GetDeviceConfig(selectedDevice.ble_mac); + if (stopPolling) break; + + var changedFields = DataManager.Instance.deviceConfig?.GetChangedFields(configBefore); + if (changedFields != null && changedFields.Count > 0) + { + Debug.Log($"[HomePageCtrl] 检测到设备配置变化: {string.Join(",", changedFields.Keys)},刷新主页UI"); + pendingUIUpdates.Add(() => + { + // await 期间蓝牙可能已连接,避免用 WiFi 数据覆盖 BLE 数据 + if (hasBluetooth) return; + InitDeviceUIFromConfig(); + }); + } } isPollingOnline = false;