how to show an alert view inside if-condition statement in swiftui?

Viewed 491

I am coming to a problem where I am trying to implement an alert view to show when the image is wrong or something and when the submit button is pressed to show an alert view. As you see in my code below that I am trying to implement an alert view inside my if condition? is it possible thanks for the help.

here is my code:

 @State private var showsAlert = false
     func uploadImage(image:UIImage){
        
        if let imageData = image.jpegData(compressionQuality: 1){
            
            let storage = Storage.storage()
            
           f let imageName = "image1"
            let folderName = "folder1"
            let path = "\(folderName)/\(imageName)"
            
            
            storage.reference(withPath: path).putData(imageData, metadata: nil){
                (_, err) in
                if let err = err {
                  print("an error has occurred - \(err.localizedDescription)")
                    
//                    .alert(isPresented: $showsAlert) {
//                                Alert(title: Text("Photo Upload Error"), message: Text("an error has occurred - \(err.localizedDescription)"), dismissButton: .default(Text("OK")))
//                            }
               
                } else {
                    
                   
                    print("Your photos have been successfully uploaded. Keep a lookout for your photos in our social media posts!")
                    //  .alert(isPresented: $showsAlert) {
                    //
                    //  Alert(title: Text("Success!"), message: Text("Your photos have been successfully uploaded. Keep a lookout for your photos in our social media posts!"), dismissButton: .default(Text("OK")))
                    //
                    //                }
                }
                
            }
            
        } else {
            print("coldn't unwrap/case image to data")
        }
        
        
    } 
1 Answers

You need to only set @State variables in your function and display the alert in the body of the view:

struct ContentView: View {
    @State private var showsAlert = false
    @State private var errorDescription: String?

    var alertTitle: String {
        errorDescription != nil ? "Photo Upload Error" : "Photo Uploaded correctly"
    }

    var alertMessage: String {
        if let error = errorDescription {
            return error
        }
        return "Photo uploaded"
    }

    var body: some View {
        Text("view")
            .alert(isPresented: $showsAlert) {
                Alert(
                    title: Text(alertTitle),
                    message: Text(alertMessage),
                    dismissButton: .default(Text("OK"))
                )
            }
    }

    func uploadImage(image: UIImage) {
        if let imageData = image.jpegData(compressionQuality: 1) {
            ...

            storage.reference(withPath: path).putData(imageData, metadata: nil) {
                _, err in
                if let err = err {
                    print("an error has occurred - \(err.localizedDescription)")
                    // set @State variables
                    DispatchQueue.main.async {
                        self.errorDescription = err.localizedDescription
                    }
                }
                DispatchQueue.main.async {
                    self.showsAlert = true
                }
            }
        }
    }
}
Related