import Foundation #if canImport(UIKit) import UIKit #endif struct SettingsRuntime: Equatable { let appName: String let marketingVersion: String let buildVersion: String let platform: String let systemVersion: String } @MainActor final class SettingsViewModel: ObservableObject { @Published private(set) var runtime: SettingsRuntime? @Published private(set) var isLoading = false @Published var storageLabel = "—" @Published var notice: String? @Published var errorMessage: String? private let store: any LogStore init(store: any LogStore) { self.store = store } func refresh() async { isLoading = true errorMessage = nil runtime = SettingsRuntime( appName: AppMetadata.name, marketingVersion: AppMetadata.marketingVersion, buildVersion: AppMetadata.buildVersion, platform: "iOS", systemVersion: Self.systemVersion() ) await loadStorageLabel() isLoading = false } func clearRuntimeLogs() async -> Int { do { let removed = try await store.clearRuntimeLogs() notice = removed > 0 ? "已清除 \(removed) 条运行日志。" : "没有可清除的运行日志。" errorMessage = nil await loadStorageLabel() return removed } catch { notice = nil errorMessage = "清除运行日志失败。" return 0 } } private func loadStorageLabel() async { do { let runtimeCount = try await store.runtimeLogs().count let connectionCount = try await store.connectionLogs().count storageLabel = "\(runtimeCount) 条运行日志 · \(connectionCount) 条连接日志" } catch { storageLabel = "—" errorMessage = "无法读取本地数据状态。" } } /// 只报对用户有意义的系统版本,不暴露机器标识之类的原始信息。 private static func systemVersion() -> String { #if canImport(UIKit) let device = UIDevice.current return "\(device.systemName) \(device.systemVersion)" #else let version = ProcessInfo.processInfo.operatingSystemVersion return "iOS \(version.majorVersion).\(version.minorVersion)" #endif } }