Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions Sources/FlowStack/FlowStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public struct FlowStack<Root: View, Overlay: View>: 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))
Expand Down
135 changes: 133 additions & 2 deletions Sources/FlowStack/View+InteractiveDismiss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID> = []

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

Expand All @@ -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))
}
}

Expand Down
Loading