skip to Main Content

I want to present Confirmation Dialog with 2 different binding properties, something like this:

.confirmationDialog("", isPresented: $calendarViewModel.showingDeleteEventDialog || $remindersViewModel.showingDeleteReminderDialog) {
                    Button("Delete", role: .destructive) {
                        switch detailType {
                        case .reminder:
                            remindersViewModel.deleteReminder()
                        case .event:
                            calendarViewModel.deleteEvent()
                        }
                    }
                    Button("Cancel", role: .cancel) { }
                } message: {
                    Text("Are you sure you want to delete this (correctName.lowercased())?")
                }

I don’t want to create 2 separate confirmation dialogs, if it is possible how can I bind 2 properties in one, so if one of them is true, dialog is opening up and automatically when it’s closed turn them into false

2

Answers


  1. I would go with an enum with a computed property for this

    enum ShowDialog {
        case deleteEvent
        case deleteReminder
        case none
    
        var showDialog: Bool {
            self != .none
        }
    }
    

    And then replace the two boolean properties you are using now with a single one of type ShowDialog

    Login or Signup to reply.
  2. If you use confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:) then you can supply the data for the dialog using the presenting parameter.

    The documentation says:

    In order for the interface to appear, both isPresented must be true and data must not be nil.

    What this means, is that you would need a boolean state variable for controlling the visibility of the dialog and another state variable for controlling the type of dialog. Although there would still be two state variables, you won’t need any more than that, even if there would be more types.

    Like this:

    @State private var isPresenting = false
    @State private var type: DetailType?
    
    var body: some View {
        VStack(spacing: 50) {
            Button("Show reminder confirmation") {
                type = .reminder
                isPresenting = true
            }
            Button("Show event confirmation") {
                type = .event
                isPresenting = true
            }
        }
        .confirmationDialog("", isPresented: $isPresenting, presenting: type) { detailType in
            Button("Delete", role: .destructive) {
                switch detailType {
                case .reminder:
    //                print("deleting reminver")
                    remindersViewModel.deleteReminder()
                case .event:
    //                print("deleting event")
                    calendarViewModel.deleteEvent()
                }
            }
            Button("Cancel", role: .cancel) { }
        } message: { detailType in
    //        Text("Are you sure you want to delete the (String(describing: detailType))?")
            Text("Are you sure you want to delete this (correctName.lowercased())?")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search