refactor(HomePage): 优化在线状态轮询逻辑并新增配置比对刷新

1. 新增应用前后台状态监听,仅前台执行轮询
2. 调整轮询间隔为3秒,增加轮询空值校验
3. 新增设备配置变更检测,自动刷新主页UI
This commit is contained in:
“虞渠成” 2026-09-09 14:08:36 +08:00
parent ff3ae505df
commit 52cf570f6c

View File

@ -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;