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? @Binding var showUsername: Bool public init(selectedImage: Binding, showUsername: Binding) { self._selectedImage = selectedImage self._showUsername = showUsername } 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) } } } if !showUsername { // 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(maxWidth: .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() } } } }