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
@@ -216,8 +216,10 @@ public final class WebContainerViewController: UIViewController {
)
}
/// 4 app_*.js battery / network
/// msext NewRootVC.initJSdata + changebattery + changenetstate
/// 4 app_*.js battery / network / appservice
/// 1 .js reload
/// 2`bridge.call` H5§3.2 234
/// msext NewRootVC.initJSdata + changebattery + changenetstate + appservice
private func writeAppDataFiles(resolved: ResolvedVersion) throws {
let writer = AppDataWriter(
bundleConfig: .shared,
@@ -225,28 +227,43 @@ public final class WebContainerViewController: UIViewController {
containerRole: .lobby
)
// NetworkMonitor started
// 3 monitor started
NetworkMonitor.shared.start()
UIDevice.current.isBatteryMonitoringEnabled = true
BatteryMonitor.shared.start()
AppLifecycleObserver.shared.start()
// 4
try writer.writeInitial()
try writer.writeBattery(UIDevice.current.batteryLevel)
try writer.writeBattery(BatteryMonitor.shared.currentLevel)
try writer.writeNetwork(NetworkMonitor.shared.currentCode)
// battery / network
// H5 reload
// §3.2 23 callback Phase 2
NotificationCenter.default.addObserver(
forName: UIDevice.batteryLevelDidChangeNotification,
object: nil,
queue: .main
) { _ in
try? writer.writeBattery(UIDevice.current.batteryLevel)
let bridge = bridgedWebView.bridge
// §3.2 2 getBattery UIDevice
// data: "%.2f"
BatteryMonitor.shared.onChange = { level in
try? writer.writeBattery(level)
bridge.call("getBattery",
data: .string(String(format: "%.2f", level)),
callback: nil)
}
// §3.2 3 getnetwork NWPath
// data: "1"= / "2"=WiFi / "3"=
NetworkMonitor.shared.onChange = { code in
try? writer.writeNetwork(code)
bridge.call("getnetwork",
data: .string("\(code)"),
callback: nil)
}
// §3.2 4 appservice App 沿
// data: "1"= / "2"=
AppLifecycleObserver.shared.onBackground = {
bridge.call("appservice", data: .string("1"), callback: nil)
}
AppLifecycleObserver.shared.onForeground = {
bridge.call("appservice", data: .string("2"), callback: nil)
}
}