diff --git a/wake/View/Owner/UserInfo/AvatarPicker.swift b/wake/View/Owner/UserInfo/AvatarPicker.swift index bdc0df4..011d7fa 100644 --- a/wake/View/Owner/UserInfo/AvatarPicker.swift +++ b/wake/View/Owner/UserInfo/AvatarPicker.swift @@ -114,10 +114,29 @@ public struct AvatarPicker: View { .sheet(isPresented: $showMediaPicker) { MediaPicker( selectedMedia: Binding( - get: { uploadManager.selectedMedia }, + get: { + print("🔄 Getting selected media: ", uploadManager.selectedMedia) + return uploadManager.selectedMedia + }, set: { newMedia in - uploadManager.clearAllMedia() - uploadManager.addMedia(newMedia) + print("🔄 Setting new media: ", newMedia) + + // Only update if we have new media + if !newMedia.isEmpty { + uploadManager.clearAllMedia() + uploadManager.addMedia(newMedia) + + // Start upload immediately after setting new media + print("🔄 Starting upload for ", newMedia.count, " items") + withAnimation { + isUploading = true + } + uploadManager.startUpload() + print("🔄 Upload started") + } + + // Dismiss the picker after processing + showMediaPicker = false } ), imageSelectionLimit: 1, @@ -125,28 +144,61 @@ public struct AvatarPicker: View { allowedMediaTypes: .imagesOnly, selectionMode: .single, onDismiss: { - showMediaPicker = false - if !uploadManager.selectedMedia.isEmpty { - withAnimation { - isUploading = true - } - uploadManager.startUpload() - } + print("🔄 Media picker dismissed") + // We'll handle the dismiss in the setter to ensure proper ordering } ) } - .onChange(of: uploadManager.uploadStatus) { _ in - if let firstMedia = uploadManager.selectedMedia.first, - case .image(let image) = firstMedia, - uploadManager.isAllUploaded { - withAnimation(.spring()) { - selectedImage = image - isUploading = false - if let status = uploadManager.uploadStatus["0"], - case .completed(let fileId) = status { - uploadedFileId = fileId + .onChange(of: uploadManager.uploadStatus) { status in + print("🔄 Upload status changed: ", status) + + // 检查是否有待处理的上传 + let pendingUploads = uploadManager.selectedMedia.filter { media in + guard let status = uploadManager.uploadStatus[media.id] else { return true } + return !status.isCompleted && !status.isUploading + } + + if !pendingUploads.isEmpty { + print("🔄 Found \(pendingUploads.count) pending uploads, starting upload...") + uploadManager.startUpload() + } + + // 检查是否有已完成的上传 + for (mediaId, status) in status { + if case .completed(let fileId) = status { + print("✅ Found completed upload with fileId: ", fileId) + + // 查找对应的媒体项 + if let media = uploadManager.selectedMedia.first(where: { $0.id == mediaId }), + case .image(let image) = media { + + print("🖼️ Updating selected image") + DispatchQueue.main.async { + withAnimation(.spring()) { + self.selectedImage = image + self.uploadedFileId = fileId + self.isUploading = false + } + // 成功更新后清除上传状态 + self.uploadManager.clearAllMedia() + } + return + } + } + } + + // 检查是否有失败的上传 + let hasFailures = status.values.contains { + if case .failed = $0 { return true } + return false + } + + if hasFailures { + print("❌ Some uploads failed") + DispatchQueue.main.async { + withAnimation { + self.isUploading = false } - uploadManager.clearAllMedia() } } }