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")
}
}
}
}
}
}
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...
}
}