import SwiftUI import UIKit struct RuntimeLogsView: View { @ObservedObject var setup: SetupCoordinator @ObservedObject var actions: LocationActionCoordinator let testFavorite: FavoriteLocation @Environment(\.dismiss) private var dismiss @ObservedObject private var proxy = ProxyManager.shared @State private var entries: [RuntimeLogEntry] = [] @State private var isTesting = false @State private var testResult = "" @State private var testMessage = "" @State private var showClearConfirm = false @State private var copiedEntryID: UUID? @State private var copyLogsConfirmed = false @State private var testLogCopied = false var body: some View { VStack(spacing: 0) { testPanel Divider() if entries.isEmpty { VStack(spacing: 10) { Image(systemName: "doc.text.magnifyingglass").font(.largeTitle).foregroundStyle(.secondary) Text("暂无运行日志").foregroundStyle(.secondary) }.frame(maxWidth: .infinity, maxHeight: .infinity) } else { ScrollView { LazyVStack(alignment: .leading, spacing: 10) { ForEach(entries.reversed()) { entry in logRow(entry) } }.padding(12) } } } .navigationTitle("运行日志").navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .navigationBarLeading) { Button("关闭") { dismiss() } } ToolbarItem(placement: .navigationBarTrailing) { Button { UIPasteboard.general.string = entries.map(\.renderedText).joined(separator: "\n") copyLogsConfirmed = true DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { copyLogsConfirmed = false } } label: { Image(systemName: copyLogsConfirmed ? "checkmark" : "doc.on.doc") } .disabled(entries.isEmpty) .accessibilityLabel("复制全部日志") } ToolbarItem(placement: .navigationBarTrailing) { Button(role: .destructive) { showClearConfirm = true } label: { Image(systemName: "trash") } .disabled(entries.isEmpty) .accessibilityLabel("清空全部日志") } } .confirmationDialog("清空所有运行日志?", isPresented: $showClearConfirm, titleVisibility: .visible) { Button("清空日志", role: .destructive) { RuntimeLogStore.clearAll() entries = [] } Button("取消", role: .cancel) {} } .task { while !Task.isCancelled { refresh(); try? await Task.sleep(nanoseconds: 750_000_000) } } } private var testPanel: some View { VStack(alignment: .leading, spacing: 10) { Button { isTesting = true; testResult = ""; testLogCopied = false Task { let result = await setup.runVerificationTest(testLat: testFavorite.latitude, testLon: testFavorite.longitude) testResult = result.isSuccess ? "环境检测通过" : "环境检测失败: \(result.id)" if !result.isSuccess { testResult += ",查看下方日志" } testMessage = setup.testLog isTesting = false; refresh() } } label: { HStack(spacing: 8) { if isTesting { ProgressView().tint(.white).controlSize(.small) } else { Image(systemName: "play.fill").font(.system(size: 13, weight: .bold)) } Text(isTesting ? "正在测试…" : "虚拟定位测试").font(.subheadline.weight(.semibold)) Spacer() Image(systemName: "chevron.right").font(.system(size: 12, weight: .semibold)).opacity(0.5) } .foregroundStyle(.white) .frame(maxWidth: .infinity) .padding(.horizontal, 14) .padding(.vertical, 12) } .buttonStyle(.plain) .background( RoundedRectangle(cornerRadius: 12, style: .continuous) .fill(isTesting ? Color.gray : Color.blue) ) .disabled(isTesting || actions.state.isBusy) Text("依次执行:代理 → 证书 → WiFi 代理 → 坐标写入 → 数据改写验证。") .font(.caption).foregroundStyle(.secondary) if !testMessage.isEmpty { VStack(alignment: .leading, spacing: 6) { HStack { Text("测试日志").font(.caption.weight(.semibold)).foregroundStyle(.secondary) Spacer() Button { UIPasteboard.general.string = testMessage testLogCopied = true DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { testLogCopied = false } } label: { HStack(spacing: 4) { if testLogCopied { Image(systemName: "checkmark").font(.system(size: 11, weight: .bold)) Text("已复制").font(.system(size: 11)) } else { Image(systemName: "doc.on.doc").font(.system(size: 11)) } } .foregroundStyle(testLogCopied ? .green : .secondary) .padding(.horizontal, 8) .padding(.vertical, 4) .background(testLogCopied ? Color.green.opacity(0.1) : Color.secondary.opacity(0.08), in: Capsule()) } .buttonStyle(.plain) Button { withAnimation(.easeInOut(duration: 0.2)) { testMessage = "" } } label: { Image(systemName: "xmark.circle.fill") .font(.system(size: 15)) .foregroundStyle(.secondary) } .buttonStyle(.plain) } ScrollView { Text(testMessage) .font(.caption.monospaced()) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) .padding(10) .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 8)) }.frame(maxHeight: 180) } } HStack(spacing: 14) { Label(proxy.isRunning ? "代理运行中" : "代理未运行", systemImage: proxy.isRunning ? "play.circle" : "stop.circle") Label(setup.canModify ? "可修改" : "不可修改", systemImage: setup.canModify ? "checkmark.shield.fill" : "xmark.shield") }.font(.caption).foregroundStyle(.secondary) if !testResult.isEmpty { Text(testResult).font(.footnote.weight(.medium)) .foregroundStyle(testResult.contains("通过") ? .green : .red) } }.padding(14).background(Color(.secondarySystemBackground)) } private func logRow(_ entry: RuntimeLogEntry) -> some View { HStack(alignment: .top, spacing: 9) { Image(systemName: entry.level == .error ? "xmark.octagon.fill" : entry.level == .warning ? "exclamationmark.triangle" : "info.circle") .foregroundStyle(entry.level == .error ? .red : entry.level == .warning ? .orange : .blue).frame(width: 18) VStack(alignment: .leading, spacing: 4) { HStack { Text("\(entry.source) \(entry.category)").font(.caption.weight(.semibold)) Spacer() Button { UIPasteboard.general.string = entry.renderedText copiedEntryID = entry.id DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { if copiedEntryID == entry.id { copiedEntryID = nil } } } label: { HStack(spacing: 4) { if copiedEntryID == entry.id { Image(systemName: "checkmark").font(.system(size: 11, weight: .bold)) Text("已复制").font(.system(size: 11)) } else { Image(systemName: "doc.on.doc").font(.system(size: 11)) } } .foregroundStyle(copiedEntryID == entry.id ? .green : .secondary) .padding(.horizontal, 8) .padding(.vertical, 4) .background(copiedEntryID == entry.id ? Color.green.opacity(0.1) : Color.secondary.opacity(0.08), in: Capsule()) } .buttonStyle(.plain) } Text(entry.message).font(.caption.monospaced()).textSelection(.enabled) if !entry.details.isEmpty { Text(entry.details.sorted(by: { $0.key < $1.key }).map { "\($0.key): \($0.value)" }.joined(separator: "\n")) .font(.caption2.monospaced()).foregroundStyle(.secondary).textSelection(.enabled) } } } .frame(maxWidth: .infinity, alignment: .leading) .padding(10) .background(Color(.secondarySystemBackground), in: RoundedRectangle(cornerRadius: 10)) } private func refresh() { entries = RuntimeLogStore.loadAll() } }