|
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 | | -/// 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 | | -/// |
| 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> |
| 39 | + private let content: (ViewStore<State?, Action>) -> Content |
| 40 | + private let store: Store<State?, Action> |
41 | 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: () -> ElseContent |
54 | | - ) where Content == _ConditionalContent<IfContent, ElseContent> { |
55 | | - self.store = store |
56 | | - let elseContent = elseContent() |
57 | | - self.content = { viewStore in |
58 | | - if var state = viewStore.state { |
59 | | - return ViewBuilder.buildEither( |
60 | | - first: 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: () -> ElseContent |
| 54 | + ) where Content == _ConditionalContent<IfContent, ElseContent> { |
| 55 | + self.store = store |
| 56 | + let elseContent = elseContent() |
| 57 | + self.content = { viewStore in |
| 58 | + if var state = viewStore.state { |
| 59 | + return ViewBuilder.buildEither( |
| 60 | + first: ifContent( |
| 61 | + store |
| 62 | + .filter { state, _ in state == nil ? !BindingLocal.isActive : true } |
| 63 | + .scope { |
| 64 | + state = $0 ?? state |
| 65 | + return state |
| 66 | + } |
| 67 | + ) |
| 68 | + ) |
| 69 | + } else { |
| 70 | + return ViewBuilder.buildEither(second: elseContent) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
| 76 | + /// state is `nil` or non-`nil`. |
| 77 | + /// |
| 78 | + /// - Parameters: |
| 79 | + /// - store: A store of optional state. |
| 80 | + /// - ifContent: A function that is given a store of non-optional state and returns a view that |
| 81 | + /// is visible only when the optional state is non-`nil`. |
| 82 | + public init<IfContent>( |
| 83 | + _ store: Store<State?, Action>, |
| 84 | + @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent |
| 85 | + ) where Content == IfContent? { |
| 86 | + self.store = store |
| 87 | + self.content = { viewStore in |
| 88 | + if var state = viewStore.state { |
| 89 | + return ifContent( |
61 | 90 | store |
62 | 91 | .filter { state, _ in state == nil ? !BindingLocal.isActive : true } |
63 | 92 | .scope { |
64 | | - state = $0 ?? state |
65 | | - return state |
66 | | - } |
| 93 | + state = $0 ?? state |
| 94 | + return state |
| 95 | + } |
67 | 96 | ) |
68 | | - ) |
69 | | - } else { |
70 | | - return ViewBuilder.buildEither(second: elseContent) |
| 97 | + } else { |
| 98 | + return nil |
| 99 | + } |
71 | 100 | } |
72 | 101 | } |
73 | | - } |
74 | 102 |
|
75 | | - /// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional |
76 | | - /// state is `nil` or non-`nil`. |
77 | | - /// |
78 | | - /// - Parameters: |
79 | | - /// - store: A store of optional state. |
80 | | - /// - ifContent: A function that is given a store of non-optional state and returns a view that |
81 | | - /// is visible only when the optional state is non-`nil`. |
82 | | - public init<IfContent>( |
83 | | - _ store: Store<State?, Action>, |
84 | | - @ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent |
85 | | - ) where Content == IfContent? { |
86 | | - self.store = store |
87 | | - self.content = { viewStore in |
88 | | - if var state = viewStore.state { |
89 | | - return ifContent( |
90 | | - store |
91 | | - .filter { state, _ in state == nil ? !BindingLocal.isActive : true } |
92 | | - .scope { |
93 | | - state = $0 ?? state |
94 | | - return state |
95 | | - } |
96 | | - ) |
97 | | - } else { |
98 | | - return nil |
99 | | - } |
| 103 | + public var body: some View { |
| 104 | + WithViewStore( |
| 105 | + self.store, |
| 106 | + observe: { $0 }, |
| 107 | + removeDuplicates: { ($0 != nil) == ($1 != nil) }, |
| 108 | + content: self.content |
| 109 | + ) |
100 | 110 | } |
101 | 111 | } |
102 | | - |
103 | | - public var body: some View { |
104 | | - WithViewStore( |
105 | | - self.store, |
106 | | - observe: { $0 }, |
107 | | - removeDuplicates: { ($0 != nil) == ($1 != nil) }, |
108 | | - content: self.content |
109 | | - ) |
110 | | - } |
111 | | -} |
112 | 112 | #endif |
0 commit comments