import SwiftUI import os.log /// 媒体上传示例视图 struct ExampleView: View { /// 是否显示媒体选择器 @State private var showMediaPicker = false /// 已选媒体文件 @State private var selectedMedia: [MediaType] = [] /// 上传状态 @State private var uploadStatus: [String: UploadStatus] = [:] /// 上传管理器 private let uploader = ImageUploadService() /// 上传状态 private enum UploadStatus: Equatable { case pending case uploading(progress: Double) case completed(fileId: String) case failed(Error) static func == (lhs: UploadStatus, rhs: UploadStatus) -> Bool { switch (lhs, rhs) { case (.pending, .pending): return true case (.uploading(let lhsProgress), .uploading(let rhsProgress)): return lhsProgress == rhsProgress case (.completed(let lhsId), .completed(let rhsId)): return lhsId == rhsId default: return false } } var description: String { switch self { case .pending: return "等待上传" case .uploading(let progress): return "上传中 \(Int(progress * 100))%" case .completed(let fileId): return "上传完成 (ID: \(fileId.prefix(8))...)" case .failed(let error): return "上传失败: \(error.localizedDescription)" } } } var body: some View { NavigationView { VStack(spacing: 20) { // 选择媒体按钮 Button(action: { showMediaPicker = true }) { Label("选择媒体", systemImage: "photo.on.rectangle") .font(.headline) .frame(maxWidth: .infinity) .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } .padding(.horizontal) // 显示已选媒体数量 if !selectedMedia.isEmpty { VStack(spacing: 10) { Text("已选择 \(selectedMedia.count) 个媒体文件") .font(.headline) // 显示媒体缩略图和上传状态 List { ForEach(0.. Color { switch status { case .pending: return .secondary case .uploading: return .blue case .completed: return .green case .failed: return .red } } } /// 媒体缩略图视图 private struct MediaThumbnailView: View { let media: MediaType let onDelete: (() -> Void)? var body: some View { ZStack(alignment: .topTrailing) { if let thumbnail = media.thumbnail { Image(uiImage: thumbnail) .resizable() .scaledToFill() .frame(width: 80, height: 80) .cornerRadius(8) .clipped() if media.isVideo { Image(systemName: "video.fill") .foregroundColor(.white) .padding(4) .background(Color.black.opacity(0.6)) .clipShape(Circle()) .padding(4) } } } } } #Preview { ExampleView() .environmentObject(AuthState.shared) }