|
1 | 1 | import SwiftUI
|
2 | 2 |
|
3 |
| -extension View { |
4 |
| - @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
5 |
| - public func navigationDestination<State, Action, Destination: View>( |
6 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
7 |
| - @ViewBuilder destination: @escaping (Store<State, Action>) -> Destination |
8 |
| - ) -> some View { |
9 |
| - self.navigationDestination( |
10 |
| - store: store, state: { $0 }, action: { $0 }, destination: destination |
11 |
| - ) |
12 |
| - } |
| 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 | + } |
13 | 14 |
|
14 |
| - @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
15 |
| - public func navigationDestination< |
16 |
| - State, Action, DestinationState, DestinationAction, Destination: View |
17 |
| - >( |
18 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
19 |
| - state toDestinationState: @escaping (State) -> DestinationState?, |
20 |
| - action fromDestinationAction: @escaping (DestinationAction) -> Action, |
21 |
| - @ViewBuilder destination: @escaping (Store<DestinationState, DestinationAction>) -> Destination |
22 |
| - ) -> some View { |
23 |
| - self.modifier( |
24 |
| - PresentationNavigationDestinationModifier( |
25 |
| - store: store, |
26 |
| - state: toDestinationState, |
27 |
| - action: fromDestinationAction, |
28 |
| - content: destination |
| 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 | + ) |
29 | 32 | )
|
30 |
| - ) |
| 33 | + } |
31 | 34 | }
|
32 |
| -} |
33 | 35 |
|
34 |
| -@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
35 |
| -private struct PresentationNavigationDestinationModifier< |
36 |
| - State, |
37 |
| - Action, |
38 |
| - DestinationState, |
39 |
| - DestinationAction, |
40 |
| - CoverContent: View |
41 |
| ->: ViewModifier { |
42 |
| - let store: Store<PresentationState<State>, PresentationAction<Action>> |
43 |
| - @StateObject var viewStore: ViewStore<Bool, PresentationAction<Action>> |
44 |
| - let toDestinationState: (State) -> DestinationState? |
45 |
| - let fromDestinationAction: (DestinationAction) -> Action |
46 |
| - let coverContent: (Store<DestinationState, DestinationAction>) -> CoverContent |
| 36 | + @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) |
| 37 | + private struct PresentationNavigationDestinationModifier< |
| 38 | + State, |
| 39 | + Action, |
| 40 | + DestinationState, |
| 41 | + DestinationAction, |
| 42 | + CoverContent: 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 coverContent: (Store<DestinationState, DestinationAction>) -> CoverContent |
47 | 49 |
|
48 |
| - init( |
49 |
| - store: Store<PresentationState<State>, PresentationAction<Action>>, |
50 |
| - state toDestinationState: @escaping (State) -> DestinationState?, |
51 |
| - action fromDestinationAction: @escaping (DestinationAction) -> Action, |
52 |
| - content coverContent: @escaping (Store<DestinationState, DestinationAction>) -> CoverContent |
53 |
| - ) { |
54 |
| - self.store = store |
55 |
| - self._viewStore = StateObject( |
56 |
| - wrappedValue: ViewStore( |
57 |
| - store |
58 |
| - .filter { state, _ in state.wrappedValue != nil } |
59 |
| - .scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }) |
| 50 | + init( |
| 51 | + store: Store<PresentationState<State>, PresentationAction<Action>>, |
| 52 | + state toDestinationState: @escaping (State) -> DestinationState?, |
| 53 | + action fromDestinationAction: @escaping (DestinationAction) -> Action, |
| 54 | + content coverContent: @escaping (Store<DestinationState, DestinationAction>) -> CoverContent |
| 55 | + ) { |
| 56 | + self.store = store |
| 57 | + self._viewStore = StateObject( |
| 58 | + wrappedValue: ViewStore( |
| 59 | + store |
| 60 | + .filter { state, _ in state.wrappedValue != nil } |
| 61 | + .scope(state: { $0.wrappedValue.flatMap(toDestinationState) != nil }) |
| 62 | + ) |
60 | 63 | )
|
61 |
| - ) |
62 |
| - self.toDestinationState = toDestinationState |
63 |
| - self.fromDestinationAction = fromDestinationAction |
64 |
| - self.coverContent = coverContent |
65 |
| - } |
| 64 | + self.toDestinationState = toDestinationState |
| 65 | + self.fromDestinationAction = fromDestinationAction |
| 66 | + self.coverContent = coverContent |
| 67 | + } |
66 | 68 |
|
67 |
| - func body(content: Content) -> some View { |
68 |
| - content.navigationDestination(isPresented: self.viewStore.binding(send: .dismiss)) { |
69 |
| - IfLetStore( |
70 |
| - self.store.scope( |
71 |
| - state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, |
72 |
| - action: { .presented(self.fromDestinationAction($0)) } |
73 |
| - ), |
74 |
| - then: self.coverContent |
75 |
| - ) |
| 69 | + func body(content: Content) -> some View { |
| 70 | + content.navigationDestination(isPresented: self.viewStore.binding(send: .dismiss)) { |
| 71 | + IfLetStore( |
| 72 | + self.store.scope( |
| 73 | + state: returningLastNonNilValue { $0.wrappedValue.flatMap(self.toDestinationState) }, |
| 74 | + action: { .presented(self.fromDestinationAction($0)) } |
| 75 | + ), |
| 76 | + then: self.coverContent |
| 77 | + ) |
| 78 | + } |
76 | 79 | }
|
77 | 80 | }
|
78 |
| -} |
| 81 | +#endif |
0 commit comments