import SwiftUI import XuqmSDK @MainActor final class UpdateViewModel: ObservableObject { @Published var isChecking = false @Published var updateInfo: AppUpdateInfo? @Published var message: String? func checkUpdate() { isChecking = true message = nil Task { do { let info = try await UpdateSDK.shared.checkAppUpdate(currentVersionCode: 1) updateInfo = info if !info.needsUpdate { message = "已是最新版本" } isChecking = false } catch { message = "检查失败: \(error.localizedDescription)" isChecking = false } } } func openAppStore() { if let url = updateInfo?.appStoreUrl, !url.isEmpty { UpdateSDK.shared.openAppStore(url: url) } } } struct UpdateCheckView: View { @StateObject private var viewModel = UpdateViewModel() var body: some View { VStack(spacing: 20) { Spacer().frame(height: 40) Text("版本更新") .font(.title2) .fontWeight(.bold) if let info = viewModel.updateInfo, info.needsUpdate { VStack(alignment: .leading, spacing: 12) { Text("发现新版本: \(info.versionName ?? "")") .font(.headline) if let changeLog = info.changeLog, !changeLog.isEmpty { Text("更新说明: \(changeLog)") .font(.body) .foregroundStyle(.secondary) } if info.forceUpdate == true { Text("强制更新") .font(.caption) .foregroundStyle(.red) } Button { viewModel.openAppStore() } label: { Text("前往 App Store") .frame(maxWidth: .infinity) .padding() .background(Color.accentColor) .foregroundStyle(.white) .clipShape(RoundedRectangle(cornerRadius: 12)) } } .padding() .background(Color.gray.opacity(0.15)) .clipShape(RoundedRectangle(cornerRadius: 16)) .padding(.horizontal) } else { if let msg = viewModel.message { Text(msg) .foregroundStyle(.secondary) } Button { viewModel.checkUpdate() } label: { HStack { if viewModel.isChecking { ProgressView() } Text(viewModel.isChecking ? "检查中…" : "检查更新") } .frame(maxWidth: .infinity) .padding() .background(Color.accentColor) .foregroundStyle(.white) .clipShape(RoundedRectangle(cornerRadius: 12)) } .disabled(viewModel.isChecking) .padding(.horizontal) } Spacer() } .padding() .onAppear { if viewModel.updateInfo == nil && !viewModel.isChecking { viewModel.checkUpdate() } } } }