damus

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

EditPictureControl.swift (4264B)


      1 //
      2 //  EditPictureControl.swift
      3 //  damus
      4 //
      5 //  Created by Joel Klabo on 3/30/23.
      6 //
      7 
      8 import SwiftUI
      9 
     10 class ImageUploadingObserver: ObservableObject {
     11     @Published var isLoading: Bool = false
     12 }
     13 
     14 struct EditPictureControl: View {
     15     let uploader: MediaUploader
     16     let pubkey: Pubkey
     17     @Binding var image_url: URL?
     18     @ObservedObject var uploadObserver: ImageUploadingObserver
     19     let callback: (URL?) -> Void
     20     
     21     @StateObject var image_upload: ImageUploadModel = ImageUploadModel()
     22     
     23     @State private var show_camera = false
     24     @State private var show_library = false
     25     @State var image_upload_confirm: Bool = false
     26 
     27     @State var preUploadedMedia: PreUploadedMedia? = nil
     28 
     29     var body: some View {
     30         Menu {
     31             Button(action: {
     32                 self.show_library = true
     33             }) {
     34                 Text("Choose from Library", comment: "Option to select photo from library")
     35             }
     36             
     37             Button(action: {
     38                 self.show_camera = true
     39             }) {
     40                 Text("Take Photo", comment: "Option to take a photo with the camera")
     41             }
     42         } label: {
     43             if uploadObserver.isLoading {
     44                 ProgressView()
     45                     .progressViewStyle(CircularProgressViewStyle(tint: DamusColors.purple))
     46                     .padding(10)
     47                     .background(DamusColors.white.opacity(0.7))
     48                     .clipShape(Circle())
     49                     .shadow(color: DamusColors.purple, radius: 15, x: 0, y: 0)
     50             } else {
     51                 Image("camera")
     52                     .resizable()
     53                     .scaledToFit()
     54                     .frame(width: 25, height: 25)
     55                     .foregroundColor(DamusColors.purple)
     56                     .padding(10)
     57                     .background(DamusColors.white.opacity(0.7))
     58                     .clipShape(Circle())
     59                     .shadow(color: DamusColors.purple, radius: 15, x: 0, y: 0)
     60             }
     61         }
     62         .sheet(isPresented: $show_camera) {
     63             CameraController(uploader: uploader) {
     64                 self.show_camera = false
     65                 self.show_library = true
     66             }
     67         }
     68         .sheet(isPresented: $show_library) {
     69             MediaPicker(image_upload_confirm: $image_upload_confirm, imagesOnly: true) { media in
     70                 self.preUploadedMedia = media
     71             }
     72             .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) {
     73                 Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
     74                     if let mediaToUpload = generateMediaUpload(preUploadedMedia) {
     75                         self.handle_upload(media: mediaToUpload)
     76                         self.show_library = false
     77                     }
     78                 }
     79                 Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
     80             }
     81         }
     82     }
     83     
     84     private func handle_upload(media: MediaUpload) {
     85         uploadObserver.isLoading = true
     86         Task {
     87             let res = await image_upload.start(media: media, uploader: uploader)
     88             
     89             switch res {
     90             case .success(let urlString):
     91                 let url = URL(string: urlString)
     92                 image_url = url
     93                 callback(url)
     94             case .failed(let error):
     95                 if let error {
     96                     print("Error uploading profile image \(error.localizedDescription)")
     97                 } else {
     98                     print("Error uploading image :(")
     99                 }
    100                 callback(nil)
    101             }
    102             uploadObserver.isLoading = false
    103         }
    104     }
    105 }
    106 
    107 struct EditPictureControl_Previews: PreviewProvider {
    108     static var previews: some View {
    109         let url = Binding<URL?>.constant(URL(string: "https://damus.io")!)
    110         let observer = ImageUploadingObserver()
    111         ZStack {
    112             Color.gray
    113             EditPictureControl(uploader: .nostrBuild, pubkey: test_pubkey, image_url: url, uploadObserver: observer) { _ in
    114                 //
    115             }
    116         }
    117     }
    118 }