- Show stable group health in a fixed 2x2 metric grid - Add link-chain state to fleet cards and parse hardware/control/SIM/cellular - Replace the wrapped icon with a full-bleed app icon - Bump the IPA build number
40 lines
1.2 KiB
Swift
40 lines
1.2 KiB
Swift
import Foundation
|
|
|
|
@MainActor
|
|
final class DeviceViewModel: ObservableObject {
|
|
@Published var snapshot: DeviceSnapshot?
|
|
@Published var isLoading = false
|
|
@Published var errorMessage: String?
|
|
@Published private(set) var connectionStatus: ConnectionStatus = .unknown
|
|
|
|
let instance: Instance
|
|
private let service: DeviceService
|
|
private let probe: ConnectionProbe?
|
|
|
|
init(instance: Instance, service: DeviceService, probe: ConnectionProbe? = nil) {
|
|
self.instance = instance
|
|
self.service = service
|
|
self.probe = probe
|
|
}
|
|
|
|
func refresh() async {
|
|
isLoading = true
|
|
errorMessage = nil
|
|
do {
|
|
snapshot = try await service.snapshot(instance)
|
|
} catch DeviceServiceError.unauthorized {
|
|
errorMessage = "需要实例密码登录后才能读取设备数据。"
|
|
} catch {
|
|
errorMessage = "无法加载设备数据。"
|
|
}
|
|
guard let probe else {
|
|
connectionStatus = .offline
|
|
isLoading = false
|
|
return
|
|
}
|
|
let result = try? await probe.test(instance)
|
|
connectionStatus = result?.status ?? .offline
|
|
isLoading = false
|
|
}
|
|
}
|