feat: 头像
This commit is contained in:
parent
86ac98a465
commit
607405ba58
@ -114,10 +114,29 @@ public struct AvatarPicker: View {
|
|||||||
.sheet(isPresented: $showMediaPicker) {
|
.sheet(isPresented: $showMediaPicker) {
|
||||||
MediaPicker(
|
MediaPicker(
|
||||||
selectedMedia: Binding(
|
selectedMedia: Binding(
|
||||||
get: { uploadManager.selectedMedia },
|
get: {
|
||||||
|
print("🔄 Getting selected media: ", uploadManager.selectedMedia)
|
||||||
|
return uploadManager.selectedMedia
|
||||||
|
},
|
||||||
set: { newMedia in
|
set: { newMedia in
|
||||||
uploadManager.clearAllMedia()
|
print("🔄 Setting new media: ", newMedia)
|
||||||
uploadManager.addMedia(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,
|
imageSelectionLimit: 1,
|
||||||
@ -125,28 +144,61 @@ public struct AvatarPicker: View {
|
|||||||
allowedMediaTypes: .imagesOnly,
|
allowedMediaTypes: .imagesOnly,
|
||||||
selectionMode: .single,
|
selectionMode: .single,
|
||||||
onDismiss: {
|
onDismiss: {
|
||||||
showMediaPicker = false
|
print("🔄 Media picker dismissed")
|
||||||
if !uploadManager.selectedMedia.isEmpty {
|
// We'll handle the dismiss in the setter to ensure proper ordering
|
||||||
withAnimation {
|
|
||||||
isUploading = true
|
|
||||||
}
|
|
||||||
uploadManager.startUpload()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.onChange(of: uploadManager.uploadStatus) { _ in
|
.onChange(of: uploadManager.uploadStatus) { status in
|
||||||
if let firstMedia = uploadManager.selectedMedia.first,
|
print("🔄 Upload status changed: ", status)
|
||||||
case .image(let image) = firstMedia,
|
|
||||||
uploadManager.isAllUploaded {
|
// 检查是否有待处理的上传
|
||||||
withAnimation(.spring()) {
|
let pendingUploads = uploadManager.selectedMedia.filter { media in
|
||||||
selectedImage = image
|
guard let status = uploadManager.uploadStatus[media.id] else { return true }
|
||||||
isUploading = false
|
return !status.isCompleted && !status.isUploading
|
||||||
if let status = uploadManager.uploadStatus["0"],
|
}
|
||||||
case .completed(let fileId) = status {
|
|
||||||
uploadedFileId = fileId
|
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user