wake-ios/wake/View/Owner/UserInfo/AvatarPicker.swift
2025-08-21 19:40:20 +08:00

84 lines
3.0 KiB
Swift

import SwiftUI
public struct AvatarPicker: View {
@StateObject private var uploadManager = MediaUploadManager()
@State private var showMediaPicker = false
@State private var isUploading = false
@Binding var selectedImage: UIImage?
public init(selectedImage: Binding<UIImage?>) {
self._selectedImage = selectedImage
}
public var body: some View {
VStack(spacing: 20) {
// Avatar Image
Button(action: {
showMediaPicker = true
}) {
ZStack {
if let selectedImage = selectedImage {
Image(uiImage: selectedImage)
.resizable()
.scaledToFill()
.frame(width: 225, height: 225)
.clipShape(RoundedRectangle(cornerRadius: 20))
} else {
// Default SVG avatar
SVGImage(svgName: "Avatar")
.frame(width: 225, height: 225)
.contentShape(Rectangle())
}
if isUploading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.scaleEffect(1.5)
}
}
}
// Upload button
Button(action: {
showMediaPicker = true
}) {
Text("Upload from Gallery")
.font(Typography.font(for: .subtitle, family: .inter))
.fontWeight(.regular)
.frame(maxWidth: .infinity)
.padding()
.foregroundColor(.black)
.background(
RoundedRectangle(cornerRadius: 16)
.fill(Color.themePrimaryLight)
)
}
.frame(width: .infinity)
}
.sheet(isPresented: $showMediaPicker) {
MediaPicker(
selectedMedia: $uploadManager.selectedMedia,
imageSelectionLimit: 1,
videoSelectionLimit: 0,
allowedMediaTypes: .imagesOnly,
selectionMode: .single,
onDismiss: {
showMediaPicker = false
if !uploadManager.selectedMedia.isEmpty {
isUploading = true
uploadManager.startUpload()
}
}
)
}
.onChange(of: uploadManager.uploadStatus) { _ in
if let firstMedia = uploadManager.selectedMedia.first,
case .image(let image) = firstMedia,
uploadManager.isAllUploaded {
selectedImage = image
isUploading = false
uploadManager.clearAllMedia()
}
}
}
}