|
1 | 1 | #if canImport(SwiftUI)
|
2 |
| - import SwiftUI |
| 2 | +import SwiftUI |
3 | 3 |
|
4 |
| - /// A view that safely unwraps a store of optional state in order to show one of two views. |
5 |
| - /// |
6 |
| - /// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store`` |
7 |
| - /// that holds onto non-optional state, and otherwise the `else` closure will be performed. |
8 |
| - /// |
9 |
| - /// This is useful for deciding between two views to show depending on an optional piece of state: |
10 |
| - /// |
11 |
| - /// ```swift |
12 |
| - /// IfLetStore( |
13 |
| - /// store.scope(state: \SearchState.results, action: SearchAction.results), |
14 |
| - /// ) { |
15 |
| - /// SearchResultsView(store: $0) |
16 |
| - /// } else: { |
17 |
| - /// Text("Loading search results...") |
18 |
| - /// } |
19 |
| - /// ``` |
20 |
| - /// |
21 |
| - /// And for showing a sheet when a piece of state becomes non-`nil`: |
22 |
| - /// |
23 |
| - /// ```swift |
24 |
| - /// .sheet( |
25 |
| - /// isActive: viewStore.binding( |
26 |
| - /// get: \.isGameActive, |
27 |
| - /// send: { $0 ? .startButtonTapped : .detailDismissed } |
28 |
| - /// ) |
29 |
| - /// ) { |
30 |
| - /// IfLetStore( |
31 |
| - /// self.store.scope(state: \.detail, action: AppAction.detail) |
32 |
| - /// ) { |
33 |
| - /// DetailView(store: $0) |
34 |
| - /// } |
35 |
| - /// } |
36 |
| - /// ``` |
37 |
| - /// |
| 4 | +/// A view that safely unwraps a store of optional state in order to show one of two views. |
| 5 | +/// |
| 6 | +/// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store`` |
| 7 | +/// that holds onto non-optional state, and otherwise the `else` closure will be performed. |
| 8 | +/// |
| 9 | +/// This is useful for deciding between two views to show depending on an optional piece of state: |
| 10 | +/// |
| 11 | +/// ```swift |
| 12 | +/// IfLetStore( |
| 13 | +/// store.scope(state: \SearchState.results, action: SearchAction.results), |
| 14 | +/// ) { |
| 15 | +/// SearchResultsView(store: $0) |
| 16 | +/// } else: { |
| 17 | +/// Text("Loading search results...") |
| 18 | +/// } |
| 19 | +/// ``` |
| 20 | +/// |
| 21 | +/// And for showing a sheet when a piece of state becomes non-`nil`: |
| 22 | +/// |
| 23 | +/// ```swift |
| 24 | +/// .sheet( |
| 25 | +/// isPresented: viewStore.binding( |
| 26 | +/// get: \.isGameActive, |
| 27 | +/// send: { $0 ? .startButtonTapped : .detailDismissed } |
| 28 | +/// ) |
| 29 | +/// ) { |
| 30 | +/// IfLetStore( |
| 31 | +/// self.store.scope(state: \.detail, action: AppAction.detail) |
| 32 | +/// ) { |
| 33 | +/// DetailView(store: $0) |
| 34 | +/// } |
| 35 | +/// } |
| 36 | +/// ``` |
| 37 | +/// |
38 | 38 | public struct IfLetStore<State, Action, Content>: View where Content: View {
|
39 |
| - private let content: (ViewStore<State?, Action>) -> Content |
40 |
| - private let store: Store<State?, Action> |
41 |
| - |
42 |
| - /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
43 |
| - /// state is `nil` or non-`nil`. |
44 |
| - /// |
45 |
| - /// - Parameters: |
46 |
| - /// - store: A store of optional state. |
47 |
| - /// - ifContent: A function that is given a store of non-optional state and returns a view that |
48 |
| - /// is visible only when the optional state is non-`nil`. |
49 |
| - /// - elseContent: A view that is only visible when the optional state is `nil`. |
50 |
| - public init<IfContent, ElseContent>( |
51 |
| - _ store: Store<State?, Action>, |
52 |
| - @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent, |
53 |
| - @ViewBuilder else elseContent: @escaping () -> ElseContent |
54 |
| - ) where Content == _ConditionalContent<IfContent, ElseContent> { |
55 |
| - self.store = store |
56 |
| - self.content = { viewStore in |
57 |
| - if var state = viewStore.state { |
58 |
| - return ViewBuilder.buildEither( |
59 |
| - first: ifContent( |
60 |
| - store.scope { |
61 |
| - state = $0 ?? state |
62 |
| - return state |
63 |
| - } |
64 |
| - ) |
65 |
| - ) |
66 |
| - } else { |
67 |
| - return ViewBuilder.buildEither(second: elseContent()) |
68 |
| - } |
69 |
| - } |
70 |
| - } |
| 39 | + private let content: (ViewStore<State?, Action>) -> Content |
| 40 | + private let store: Store<State?, Action> |
71 | 41 |
|
72 |
| - /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
73 |
| - /// state is `nil` or non-`nil`. |
74 |
| - /// |
75 |
| - /// - Parameters: |
76 |
| - /// - store: A store of optional state. |
77 |
| - /// - ifContent: A function that is given a store of non-optional state and returns a view that |
78 |
| - /// is visible only when the optional state is non-`nil`. |
79 |
| - public init<IfContent>( |
80 |
| - _ store: Store<State?, Action>, |
81 |
| - @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent |
82 |
| - ) where Content == IfContent? { |
83 |
| - self.store = store |
84 |
| - self.content = { viewStore in |
85 |
| - if var state = viewStore.state { |
86 |
| - return ifContent( |
| 42 | + /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
| 43 | + /// state is `nil` or non-`nil`. |
| 44 | + /// |
| 45 | + /// - Parameters: |
| 46 | + /// - store: A store of optional state. |
| 47 | + /// - ifContent: A function that is given a store of non-optional state and returns a view that |
| 48 | + /// is visible only when the optional state is non-`nil`. |
| 49 | + /// - elseContent: A view that is only visible when the optional state is `nil`. |
| 50 | + public init<IfContent, ElseContent>( |
| 51 | + _ store: Store<State?, Action>, |
| 52 | + @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent, |
| 53 | + @ViewBuilder else elseContent: @escaping () -> ElseContent |
| 54 | + ) where Content == _ConditionalContent<IfContent, ElseContent> { |
| 55 | + self.store = store |
| 56 | + self.content = { viewStore in |
| 57 | + if var state = viewStore.state { |
| 58 | + return ViewBuilder.buildEither( |
| 59 | + first: ifContent( |
87 | 60 | store.scope {
|
88 | 61 | state = $0 ?? state
|
89 | 62 | return state
|
90 | 63 | }
|
91 | 64 | )
|
92 |
| - } else { |
93 |
| - return nil |
94 |
| - } |
| 65 | + ) |
| 66 | + } else { |
| 67 | + return ViewBuilder.buildEither(second: elseContent()) |
95 | 68 | }
|
96 | 69 | }
|
| 70 | + } |
97 | 71 |
|
98 |
| - public var body: some View { |
99 |
| - WithViewStore( |
100 |
| - self.store, |
101 |
| - removeDuplicates: { ($0 != nil) == ($1 != nil) }, |
102 |
| - content: self.content |
103 |
| - ) |
| 72 | + /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
| 73 | + /// state is `nil` or non-`nil`. |
| 74 | + /// |
| 75 | + /// - Parameters: |
| 76 | + /// - store: A store of optional state. |
| 77 | + /// - ifContent: A function that is given a store of non-optional state and returns a view that |
| 78 | + /// is visible only when the optional state is non-`nil`. |
| 79 | + public init<IfContent>( |
| 80 | + _ store: Store<State?, Action>, |
| 81 | + @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent |
| 82 | + ) where Content == IfContent? { |
| 83 | + self.store = store |
| 84 | + self.content = { viewStore in |
| 85 | + if var state = viewStore.state { |
| 86 | + return ifContent( |
| 87 | + store.scope { |
| 88 | + state = $0 ?? state |
| 89 | + return state |
| 90 | + } |
| 91 | + ) |
| 92 | + } else { |
| 93 | + return nil |
| 94 | + } |
104 | 95 | }
|
105 | 96 | }
|
| 97 | + |
| 98 | + public var body: some View { |
| 99 | + WithViewStore( |
| 100 | + self.store, |
| 101 | + removeDuplicates: { ($0 != nil) == ($1 != nil) }, |
| 102 | + content: self.content |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
106 | 106 | #endif
|
0 commit comments