This is what I have, to present confirmationDialog on tap:
extension View {
func confirmDeletion(isPresented: Binding, title: String? = nil, action: @escaping ()-> Void) -> some View {
confirmationDialog("", isPresented: isPresented) {
Button("delete", role: .destructive) { action() }
} message: {
Text(title)
}
}
}
And it works perfectly, but now I need to define and call it base on selected Item from the list, not just by toggle isPresented, so I simply defined it like this:
extension View {
// here is previous declaration
func confirmDeletion- (item: Binding
- , title: String? = nil, action: @escaping (Item)-> Void) -> some View {
let binding = Binding {
item.wrappedValue != nil
} set: { _ in }
return confirmDeletion(isPresented: binding, title: title) { // call the previous declaration with overriden action
if let item = item.wrappedValue {
action(item)
}
}
}
}
But the issue is that it is displayed only once. When I tap the same PlanElementView in a row, it doesnt present dialog second time. Why? When I tap different PlanElementView it works.
In code I use it like this:
@State var selectedPlanForDeletion: Plan?
var body: some View {
VStack {
ForEach(plans) { plan in
PlanElementView(plan: plan)
.onTapGesture {
selectedPlanForDeletion = plan
}
}
}
.confirmDeletion(
item: $selectedPlanForDeletion,
title: "newTitle"
) { plan in
print(plan)
}
}