|
1 | 1 | import SwiftUI
|
| 2 | +import SwiftUINavigation |
2 | 3 |
|
3 |
| -#if swift(>=5.7) |
4 |
| - extension View { |
5 |
| - @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
6 |
| - public func navigationDestination<State, Action, Destination: View>( |
7 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
8 |
| - @ViewBuilder destination: @escaping (Store<State, Action>) -> Destination |
9 |
| - ) -> some View { |
10 |
| - self.navigationDestination( |
11 |
| - store: store, state: { $0 }, action: { $0 }, destination: destination |
12 |
| - ) |
13 |
| - } |
| 4 | +extension View { |
| 5 | + @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
| 6 | + public func navigationDestination<State, Action, Destination: View>( |
| 7 | + store: Store<PresentationState<State>, PresentationAction<Action>>, |
| 8 | + @ViewBuilder destination: @escaping (Store<State, Action>) -> Destination |
| 9 | + ) -> some View { |
| 10 | + self.navigationDestination( |
| 11 | + store: store, state: { $0 }, action: { $0 }, destination: destination |
| 12 | + ) |
| 13 | + } |
14 | 14 |
|
15 |
| - @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
16 |
| - public func navigationDestination< |
17 |
| - State, Action, DestinationState, DestinationAction, Destination: View |
18 |
| - >( |
19 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
20 |
| - state toDestinationState: @escaping (State) -> DestinationState?, |
21 |
| - action fromDestinationAction: @escaping (DestinationAction) -> Action, |
22 |
| - @ViewBuilder destination: @escaping (Store<DestinationState, DestinationAction>) -> |
23 |
| - Destination |
24 |
| - ) -> some View { |
25 |
| - self.modifier( |
26 |
| - PresentationNavigationDestinationModifier( |
27 |
| - store: store, |
28 |
| - state: toDestinationState, |
29 |
| - action: fromDestinationAction, |
30 |
| - content: destination |
31 |
| - ) |
| 15 | + @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
| 16 | + public func navigationDestination< |
| 17 | + State, Action, DestinationState, DestinationAction, Destination: View |
| 18 | + >( |
| 19 | + store: Store<PresentationState<State>, PresentationAction<Action>>, |
| 20 | + state toDestinationState: @escaping (State) -> DestinationState?, |
| 21 | + action fromDestinationAction: @escaping (DestinationAction) -> Action, |
| 22 | + @ViewBuilder destination: @escaping (Store<DestinationState, DestinationAction>) -> |
| 23 | + Destination |
| 24 | + ) -> some View { |
| 25 | + self.modifier( |
| 26 | + PresentationNavigationDestinationModifier( |
| 27 | + store: store, |
| 28 | + state: toDestinationState, |
| 29 | + action: fromDestinationAction, |
| 30 | + content: destination |
32 | 31 | )
|
33 |
| - } |
| 32 | + ) |
34 | 33 | }
|
| 34 | +} |
35 | 35 |
|
36 |
| - @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
37 |
| - private struct PresentationNavigationDestinationModifier< |
38 |
| - State, |
39 |
| - Action, |
40 |
| - DestinationState, |
41 |
| - DestinationAction, |
42 |
| - DestinationContent: View |
43 |
| - >: ViewModifier { |
44 |
| - let store: Store<PresentationState<State>, PresentationAction<Action>> |
45 |
| - @StateObject var viewStore: ViewStore<Bool, PresentationAction<Action>> |
46 |
| - let toDestinationState: (State) -> DestinationState? |
47 |
| - let fromDestinationAction: (DestinationAction) -> Action |
48 |
| - let destinationContent: (Store<DestinationState, DestinationAction>) -> DestinationContent |
| 36 | +@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
| 37 | +private struct PresentationNavigationDestinationModifier< |
| 38 | + State, |
| 39 | + Action, |
| 40 | + DestinationState, |
| 41 | + DestinationAction, |
| 42 | + DestinationContent: View |
| 43 | +>: ViewModifier { |
| 44 | + let store: Store<PresentationState<State>, PresentationAction<Action>> |
| 45 | + @StateObject var viewStore: ViewStore<Bool, PresentationAction<Action>> |
| 46 | + let toDestinationState: (State) -> DestinationState? |
| 47 | + let fromDestinationAction: (DestinationAction) -> Action |
| 48 | + let destinationContent: (Store<DestinationState, DestinationAction>) -> DestinationContent |
49 | 49 |
|
50 |
| - init( |
51 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
52 |
| - state toDestinationState: @escaping (State) -> DestinationState?, |
53 |
| - action fromDestinationAction: @escaping (DestinationAction) -> Action, |
54 |
| - content destinationContent: |
55 |
| - @escaping (Store<DestinationState, DestinationAction>) -> DestinationContent |
56 |
| - ) { |
57 |
| - self.store = store |
58 |
| - self._viewStore = StateObject( |
59 |
| - wrappedValue: ViewStore( |
60 |
| - store |
61 |
| - .filterSend { state, _ in state.wrappedValue != nil } |
62 |
| - .scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }), |
63 |
| - observe: { $0 } |
64 |
| - ) |
| 50 | + init( |
| 51 | + store: Store<PresentationState<State>, PresentationAction<Action>>, |
| 52 | + state toDestinationState: @escaping (State) -> DestinationState?, |
| 53 | + action fromDestinationAction: @escaping (DestinationAction) -> Action, |
| 54 | + content destinationContent: |
| 55 | + @escaping (Store<DestinationState, DestinationAction>) -> DestinationContent |
| 56 | + ) { |
| 57 | + self.store = store |
| 58 | + self._viewStore = StateObject( |
| 59 | + wrappedValue: ViewStore( |
| 60 | + store |
| 61 | + .filterSend { state, _ in state.wrappedValue != nil } |
| 62 | + .scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }) |
65 | 63 | )
|
66 |
| - self.toDestinationState = toDestinationState |
67 |
| - self.fromDestinationAction = fromDestinationAction |
68 |
| - self.destinationContent = destinationContent |
69 |
| - } |
| 64 | + ) |
| 65 | + self.toDestinationState = toDestinationState |
| 66 | + self.fromDestinationAction = fromDestinationAction |
| 67 | + self.destinationContent = destinationContent |
| 68 | + } |
70 | 69 |
|
71 |
| - func body(content: Content) -> some View { |
72 |
| - content.navigationDestination( |
73 |
| - // TODO: do binding with ID check |
74 |
| - isPresented: self.viewStore.binding(send: .dismiss) |
75 |
| - ) { |
76 |
| - IfLetStore( |
77 |
| - self.store.scope( |
78 |
| - state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, |
79 |
| - action: { .presented(self.fromDestinationAction($0)) } |
80 |
| - ), |
81 |
| - then: self.destinationContent |
82 |
| - ) |
83 |
| - } |
| 70 | + func body(content: Content) -> some View { |
| 71 | + content.navigationDestination( |
| 72 | + // TODO: do binding with ID check |
| 73 | + unwrapping: self.viewStore.binding(send: .dismiss).presence |
| 74 | + ) { _ in |
| 75 | + IfLetStore( |
| 76 | + self.store.scope( |
| 77 | + state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, |
| 78 | + action: { .presented(self.fromDestinationAction($0)) } |
| 79 | + ), |
| 80 | + then: self.destinationContent |
| 81 | + ) |
84 | 82 | }
|
85 | 83 | }
|
86 |
| -#endif |
| 84 | +} |
| 85 | + |
| 86 | +fileprivate extension Binding where Value == Bool { |
| 87 | + var presence: Binding<Void?> { |
| 88 | + .init( |
| 89 | + get: { self.wrappedValue ? () : nil }, |
| 90 | + set: { self.transaction($1).wrappedValue = $0 != nil } |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
0 commit comments