Phase 2.B:3 项反向 callback 落地(getBattery / getnetwork / appservice)

完成大厅业务期实时状态推送给 H5 的事件驱动链路。每次变化时同时(1)
重写对应 app_*.js 文件供下次 reload,(2)bridge.call 反向推送给当前
页 H5 — 双轨与原 msext 行为等价。

新增 Source/Resource/BatteryMonitor.swift:
  - @MainActor public final class BatteryMonitor,shared 单例 + 幂等 start()
  - 监听 UIDevice.batteryLevelDidChangeNotification + 启动期读首次值
  - currentLevel 同步快照(max(level, 0) 兜底模拟器 -1)+ onChange 回调
  - notification block 内 MainActor.assumeIsolated 安全跨到 main actor

新增 Source/Resource/AppLifecycleObserver.swift:
  - @MainActor public final class AppLifecycleObserver,shared 单例 + 幂等 start()
  - 监听 didEnterBackgroundNotification + willEnterForegroundNotification
  - onBackground / onForeground 双钩子;assumeIsolated 同上

WebContainerViewController.writeAppDataFiles 接入(替换 inline addObserver):
  - 3 个 monitor 启动统一聚合(NetworkMonitor + BatteryMonitor + AppLifecycle)
  - BatteryMonitor.onChange:writeBattery + bridge.call("getBattery", "%.2f")
  - NetworkMonitor.onChange:writeNetwork + bridge.call("getnetwork", "1"/"2"/"3")
  - AppLifecycle.onBackground/Foreground:bridge.call("appservice", "1"/"2")
  - 首次值改读 BatteryMonitor.currentLevel 而非 UIDevice 原始值

至此 Phase 2.B 全部完成(5 项反向 callback:getphoneinfo / getBattery /
getnetwork / appservice / shakeEnd 全部接好)。

剩 Phase 2.C ExternalSubscriptions 生命周期管理(防双发,栈深 ≥ 2 时
下层不发桥事件)。

BuildProject 通过

Plan §5 Phase 2.B 全部勾选

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
joywayer
2026-06-22 18:45:03 +08:00
co-authored by Claude Opus 4.7
parent d38ff0f8c4
commit 23b3a1fdad
4 changed files with 139 additions and 24 deletions
+10 -10
View File
@@ -359,16 +359,16 @@ Contract Design Plan(本文档)
##### 2.B Native→H5 反向 callback
- [ ] **2.8** `Source/Device/DeviceInfo.swift`6 字段 snapshot
- `getphoneInfo` 收到调用后`bridge.call("getphoneinfo", data: ...)` 反向
- [ ] **2.9** `Source/Device/BatteryMonitor.swift`:监听 `UIDeviceBatteryLevelDidChangeNotification`
- 触发 → `bridge.call("getBattery", data: .string("%.2f"))`
- [ ] **2.10** `Source/Device/NetworkMonitor.swift``NWPathMonitor` 包装
- 状态变化 → `bridge.call("getnetwork", data: .string("1"/"2"/"3"))`
- [ ] **2.11** `Source/Device/AppLifecycleObserver.swift`:监听 SceneDelegate 前后台通知
-`bridge.call("appservice", data: .string("1"=后台 / "2"=前台))`
- [ ] **2.12** `Source/WebView/ShakeDetector.swift`:在 `WebContainerViewController` 重写 `motionEnded:`
-`bridge.call("shakeEnd", data: nil)`(仅 `canshake=YES` 时触发)
- [x] **2.8** `Source/Bridge/Handlers/DeviceInfoHandler.swift`DeviceInfoSnapshot 6 字段(Phase 2.5 一并完成)
- `getphoneInfo` 收到 → `bridge.call("getphoneinfo", data: 表 A 6 字段)` 反向
- [x] **2.9** `Source/Resource/BatteryMonitor.swift`:监听 `UIDevice.batteryLevelDidChangeNotification`
- 触发 → 重写 `app_battery.js` + `bridge.call("getBattery", data: .string("%.2f", level))`
- [x] **2.10** `Source/Resource/NetworkMonitor.swift`NWPathMonitor 包装Phase 1 已建)
- 状态变化 → 重写 `app_network.js` + `bridge.call("getnetwork", data: .string("1"/"2"/"3"))`
- [x] **2.11** `Source/Resource/AppLifecycleObserver.swift`:监听 `UIApplication.didEnterBackgroundNotification` / `willEnterForegroundNotification`
- 后台`bridge.call("appservice", data: .string("1"))`;前台 → `"2"`
- [x] **2.12** ShakeDetector 集成在 `Source/Bridge/Handlers/ShakeHandler.swift`Phase 2.3 一并完成)
- `WebContainerViewController.motionEnded(_:with:)` 转发`bridge.call("shakeEnd", data: nil)`(仅 `canShake=YES` 时触发)
##### 2.C 外部订阅生命周期