-1

I am a little confused, I try to display a pop-up in my View when my function is true otherwise continue but I do not understand how to do it I am lost between the different ways of doing. I tried several things but something escapes me.

Edit: The project is based on SwiftUI and the popup as to be displayed in a SwiftUI View, the function is in a class returning an ObservableObject. The function is called from a SwiftUI View from a button.

get_deliveries.swift -> into a class delivViewModel:ObservableObject

    func cancelDeliv(delivID: String) {
        let delivRef = db.collection("av_deliveries").document(delivID)
        let isDemain = Date().dayAfter.toString()
        delivRef.getDocument { (document, error) in
            if let document = document, document.exists {
                let property = document.get("when")
                if isDemain == property as! String {
                      // Print a pop-up error
                } else {
                delivRef.updateData([
                    "taken": false,
                    "taken_by": ""
                ]) { err in
                    if let err = err {
                        print("Error updating document: \(err)")
                    } else {
                        print("Document successfully updated")
                    }
                }
            }
            }
        }
    }
2
  • It is not clear from your question where this function is or how it relates to your view. You also have it tagged both SwiftUI and UIKit, so it's not clear which type of alert you're trying to display. Commented Mar 28, 2021 at 18:39
  • The code you have shown is a view model, so it is not responsible for, directly, showing an alert. That is the responsibility of your view. You set a state/property in your view model that indicates there is an error and your view reacts to that to actually display an alert or a sheet or whatever. Commented Mar 28, 2021 at 20:10

1 Answer 1

1

Here is a basic demo of activating an alert with a Button in a View body from an ObservableObject class.

struct ContentView: View {
// However you've initialized your class, it may be different than this.
@StateObject var progress = DeliveriesViewModel()

var body: some View {
    VStack  {
    // Other views in your View body

        // SwiftUI button to activate the Bool in ObservableObject class.
        Button {
            progress.isDisplayingAlert.toggle()
        } label: {
            Text("Toggle Alert")
        }
    }
    .alert(isPresented: $progress.isDisplayingAlert) { () -> Alert in
        Alert(title: Text("Alert Title"), message: Text("Body of Alert."), dismissButton: .cancel())
    }
  }
}

class DeliveriesViewModel: ObservableObject {

@Published var isDisplayingAlert = false

func displayAlert() {
    // other function actions...
    
    // update Published property.
    isDisplayingAlert = true
 
    // other function actions...
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.