damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

EditProfilePictureControl.swift (4144B)


      1 //
      2 //  ProfilePictureEditView.swift
      3 //  damus
      4 //
      5 //  Created by Joel Klabo on 3/30/23.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct EditProfilePictureControl: View {
     11     let uploader: MediaUploader
     12     let pubkey: String
     13     @Binding var profile_image: URL?
     14     @ObservedObject var viewModel: ProfileUploadingViewModel
     15     let callback: (URL?) -> Void
     16     
     17     @StateObject var image_upload: ImageUploadModel = ImageUploadModel()
     18     
     19     @State private var show_camera = false
     20     @State private var show_library = false
     21     @State var image_upload_confirm: Bool = false
     22 
     23     @State var mediaToUpload: MediaUpload? = nil
     24 
     25     var body: some View {
     26         Menu {
     27             Button(action: {
     28                 self.show_library = true
     29             }) {
     30                 Text("Choose from Library", comment: "Option to select photo from library")
     31             }
     32             
     33             Button(action: {
     34                 self.show_camera = true
     35             }) {
     36                 Text("Take Photo", comment: "Option to take a photo with the camera")
     37             }
     38         } label: {
     39             if viewModel.isLoading {
     40                 ProgressView()
     41             } else {
     42                 Image("camera")
     43                     .resizable()
     44                     .scaledToFit()
     45                     .frame(width: 25, height: 25)
     46                     .foregroundColor(DamusColors.white)
     47             }
     48         }
     49         .sheet(isPresented: $show_camera) {
     50             
     51             ImagePicker(uploader: uploader, sourceType: .camera, pubkey: pubkey, image_upload_confirm: $image_upload_confirm, imagesOnly: true) { img in
     52                 self.mediaToUpload = .image(img)
     53             } onVideoPicked: { url in
     54                 print("Cannot upload videos as profile image")
     55             }
     56             .alert(NSLocalizedString("Are you sure you want to upload this image?", comment: "Alert message asking if the user wants to upload an image."), isPresented: $image_upload_confirm) {
     57                 Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
     58                     if let mediaToUpload {
     59                         self.handle_upload(media: mediaToUpload)
     60                         self.show_camera = false
     61                     }
     62                 }
     63                 Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
     64             }
     65         }
     66         .sheet(isPresented: $show_library) {
     67             ImagePicker(uploader: uploader, sourceType: .photoLibrary, pubkey: pubkey, image_upload_confirm: $image_upload_confirm, imagesOnly: true) { img in
     68                 self.mediaToUpload = .image(img)
     69 
     70             } onVideoPicked: { url in
     71                 print("Cannot upload videos as profile image")
     72             }
     73             .alert(NSLocalizedString("Are you sure you want to upload this image?", comment: "Alert message asking if the user wants to upload an image."), isPresented: $image_upload_confirm) {
     74                 Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
     75                     if let mediaToUpload {
     76                         self.handle_upload(media: mediaToUpload)
     77                         self.show_library = false
     78                     }
     79                 }
     80                 Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
     81             }
     82         }
     83     }
     84     
     85     private func handle_upload(media: MediaUpload) {
     86         viewModel.isLoading = true
     87         Task {
     88             let res = await image_upload.start(media: media, uploader: uploader)
     89             
     90             switch res {
     91             case .success(let urlString):
     92                 let url = URL(string: urlString)
     93                 profile_image = url
     94                 callback(url)
     95             case .failed(let error):
     96                 if let error {
     97                     print("Error uploading profile image \(error.localizedDescription)")
     98                 } else {
     99                     print("Error uploading image :(")
    100                 }
    101                 callback(nil)
    102             }
    103             viewModel.isLoading = false
    104         }
    105     }
    106 }