From 54e22564f0eb515d856a7371abbcccbbdd9a7428 Mon Sep 17 00:00:00 2001 From: Brody Roberts Date: Mon, 30 Nov 2026 16:53:47 -0800 Subject: [PATCH] InteractiveDismiss modifier changed --- README.md | 2 + Sources/FlowStack/FlowStack.swift | 1 + .../FlowStack/View+InteractiveDismiss.swift | 135 +++++++++++++++++- 3 files changed, 136 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4d426c..b7dbc0f 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ EditorView(draft: $draft) .flowInteractiveDismissDisabled(draft.hasChanges) ``` +The modifier also works on a view pushed inside a flow destination's `NavigationStack`. Any visible view that passes `true` keeps that flow destination from being dismissed; passing `false` elsewhere does not override it. Popping the navigation destination removes its restriction. + ## Returning to a link A dismissal zooms back into the flow link wherever it is *now*, not where it was when it was activated. If the layout changed while the destination was presented, because the device rotated or an iPhone Duo was opened or folded, the destination returns to the link's new position and size. diff --git a/Sources/FlowStack/FlowStack.swift b/Sources/FlowStack/FlowStack.swift index 8cad043..5a64de1 100644 --- a/Sources/FlowStack/FlowStack.swift +++ b/Sources/FlowStack/FlowStack.swift @@ -340,6 +340,7 @@ public struct FlowStack: View { destination.content(element.value) .onAppear { linkContexts.setScrollProxy(proxy, forLevel: element.index + 1) } } + .modifier(FlowInteractiveDismissScope()) .frame(maxWidth: .infinity, maxHeight: .infinity) .id(element.hashValue) .transition(.flowTransition(with: element.context, source: element.source, value: AnyHashable(element.value), level: element.index)) diff --git a/Sources/FlowStack/View+InteractiveDismiss.swift b/Sources/FlowStack/View+InteractiveDismiss.swift index 87c533f..bed5fc0 100644 --- a/Sources/FlowStack/View+InteractiveDismiss.swift +++ b/Sources/FlowStack/View+InteractiveDismiss.swift @@ -24,7 +24,135 @@ struct InteractiveDismissDisabledKey: PreferenceKey { static var defaultValue: Bool = false static func reduce(value: inout Bool, nextValue: () -> Bool) { - value = nextValue() + let next = nextValue() + value = value || next + } +} + +/// Preferences from navigation destinations can be retained alongside those of +/// the hidden root. Share a destination's state through the environment instead, +/// and let only modifiers on appeared views contribute to it. +private final class FlowInteractiveDismissState: ObservableObject { + @Published private(set) var isDisabled = false + private var disabledSources: Set = [] + + func setDisabled(_ disabled: Bool, for source: UUID) { + if disabled { + disabledSources.insert(source) + } else { + disabledSources.remove(source) + } + let isDisabled = !disabledSources.isEmpty + if self.isDisabled != isDisabled { + self.isDisabled = isDisabled + } + } +} + +private struct FlowInteractiveDismissStateKey: EnvironmentKey { + static let defaultValue: FlowInteractiveDismissState? = nil +} + +private extension EnvironmentValues { + var flowInteractiveDismissState: FlowInteractiveDismissState? { + get { self[FlowInteractiveDismissStateKey.self] } + set { self[FlowInteractiveDismissStateKey.self] = newValue } + } +} + +/// Each presentation owns its state, so a covered flow destination cannot disable +/// the one above it. Publish one preference for both the transition and the scrim. +struct FlowInteractiveDismissScope: ViewModifier { + @StateObject private var state = FlowInteractiveDismissState() + + func body(content: Content) -> some View { + content + .environment(\.flowInteractiveDismissState, state) + .preference(key: InteractiveDismissDisabledKey.self, value: state.isDisabled) + } +} + +private struct FlowInteractiveDismissDisabledModifier: ViewModifier { + let isDisabled: Bool + @Environment(\.flowInteractiveDismissState) private var state + + func body(content: Content) -> some View { + content + .background { + FlowInteractiveDismissReporter(state: state, isDisabled: isDisabled) + .frame(width: 0, height: 0) + .allowsHitTesting(false) + } + .transformPreference(InteractiveDismissDisabledKey.self) { value in + // Retain preference behavior for content outside a flow presentation. + if state == nil { value = value || isDisabled } + } + } +} + +/// UIKit forwards navigation appearance changes even on systems where SwiftUI +/// retains the root's onAppear/onDisappear lifetime across a navigation push. +private struct FlowInteractiveDismissReporter: UIViewControllerRepresentable { + let state: FlowInteractiveDismissState? + let isDisabled: Bool + + func makeUIViewController(context: Context) -> Controller { + Controller() + } + + func updateUIViewController(_ controller: Controller, context: Context) { + controller.update(state: state, isDisabled: isDisabled) + } + + static func dismantleUIViewController(_ controller: Controller, coordinator: ()) { + controller.detach() + } + + final class Controller: UIViewController { + private let source = UUID() + private var state: FlowInteractiveDismissState? + private var isDisabled = false + private var isVisible = false + private var isAttached = true + + func update(state: FlowInteractiveDismissState?, isDisabled: Bool) { + if self.state !== state { removeContribution() } + self.state = state + self.isDisabled = isDisabled + report() + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + isVisible = true + report() + } + + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + isVisible = false + report() + } + + func detach() { + isAttached = false + removeContribution() + } + + private func report() { + // Appearance and representable updates can happen during layout. + // Read the latest inputs after that update, including a cancelled pop. + DispatchQueue.main.async { [weak self] in + guard let self else { return } + state?.setDisabled(isAttached && isVisible && isDisabled, for: source) + } + } + + private func removeContribution() { + let state = state + let source = source + DispatchQueue.main.async { state?.setDisabled(false, for: source) } + } } } @@ -36,9 +164,12 @@ public extension View { /// The view can still be dismissed with ``SwiftUICore/EnvironmentValues/flowDismiss``, or by /// removing it from the ``FlowPath``. /// + /// A visible navigation destination can disable dismissal of its enclosing flow + /// destination. Passing `false` does not override another visible view's `true`. + /// /// - Parameter isDisabled: Whether dragging the view away is disabled. func flowInteractiveDismissDisabled(_ isDisabled: Bool = true) -> some View { - preference(key: InteractiveDismissDisabledKey.self, value: isDisabled) + modifier(FlowInteractiveDismissDisabledModifier(isDisabled: isDisabled)) } }