Deleting from a List using a ConfirmationDialog in a child view. #1817
-
I have a parent feature that holds an The parent feature looks like this: struct SavedNetworks: ReducerProtocol {
struct State: Equatable {
var networks: IdentifiedArrayOf<NetworkDetails.State>
}
enum Action {
case networkDetailsAction(id: String,
action: NetworkDetails.Action)
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .networkDetailsAction(let id, .delegate(let delegateAction)):
// Handle delete tapped
switch delegateAction {
case .deleteConfirmed:
state.networks.remove(id: id)
return .none
}
}
}
.forEach(\.networks,
action: /Action.networkDetailsAction(id: action:)) {
NetworkDetails()._printChanges()
}
}
} The user can navigate into a Details screen by tapping on a list item. Inside the Details screen is a Delete button. This shows a confirmation dialog that asks the user if they are sure. If they confirm the delete then the action Here's the problem:
Additionally, if I tap the back button in the navigation bar (because the child view does not get dismissed) and go back to the list view the confirmation dialog is shown again, but this time in the list view. Should I be handling this confirmation dialog differently? Or maybe I need to force child view to be popped off the stack after it has been deleted? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Okay. I managed to get it working by handling the confirmation dialog actions differently. When the user selects Delete from the confirmation dialog I set its state to As for navigation I resorted to doing a manual dismiss of the detail view using a method @mbrandonw described here: Basically hold an @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
.onChange(of: viewStore.isPresented) { isPresented in
if !isPresented {
self.presentationMode.wrappedValue.dismiss()
}
} Beginning to understand why people had some qualms with SwiftUI navigation. |
Beta Was this translation helpful? Give feedback.
Okay. I managed to get it working by handling the confirmation dialog actions differently. When the user selects Delete from the confirmation dialog I set its state to
nil
immediately and then send the.deleteConfirmed
action to the parent reducer.As for navigation I resorted to doing a manual dismiss of the detail view using a method @mbrandonw described here:
https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/7
Basically hold an
isPresented
bool in the state and then change that to false when appropriate. Watch for the change in an.onChange
modifier like this: