Skip to content

Commit c53a961

Browse files
authored
Add @ViewBuilder to ForEachStore and IfLetStore (#501)
* Add @ViewBuilder to ForEachStore and IfLetStore * Update IfLetStore else branch to take view builder
1 parent ba2b9e0 commit c53a961

File tree

9 files changed

+28
-13
lines changed

9 files changed

+28
-13
lines changed

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-OptionalState.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ struct OptionalBasicsView: View {
6969
.buttonStyle(BorderlessButtonStyle())
7070
}
7171
},
72-
else: Text(template: "`CounterState` is `nil`", .body)
72+
else: {
73+
Text(template: "`CounterState` is `nil`", .body)
74+
}
7375
)
7476
}
7577
}

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Lists-NavigateAndLoad.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct NavigateAndLoadListView: View {
8989
self.store.scope(
9090
state: { $0.selection?.value }, action: NavigateAndLoadListAction.counter),
9191
then: CounterView.init(store:),
92-
else: ActivityIndicator()
92+
else: { ActivityIndicator() }
9393
),
9494
tag: row.id,
9595
selection: viewStore.binding(

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-NavigateAndLoad.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct NavigateAndLoadView: View {
7171
self.store.scope(
7272
state: { $0.optionalCounter }, action: NavigateAndLoadAction.optionalCounter),
7373
then: CounterView.init(store:),
74-
else: ActivityIndicator()
74+
else: { ActivityIndicator() }
7575
),
7676
isActive: viewStore.binding(
7777
get: { $0.isNavigationActive },

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Sheet-PresentAndLoad.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct PresentAndLoadView: View {
7979
self.store.scope(
8080
state: { $0.optionalCounter }, action: PresentAndLoadAction.optionalCounter),
8181
then: CounterView.init(store:),
82-
else: ActivityIndicator()
82+
else: { ActivityIndicator() }
8383
)
8484
}
8585
.navigationBarTitle("Present and load")

Examples/CaseStudies/UIKitCaseStudies/Internal/IfLetStoreController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class IfLetStoreController<State, Action>: UIViewController {
2222
init(
2323
store: Store<State?, Action>,
2424
then ifDestination: @escaping (Store<State, Action>) -> UIViewController,
25-
else elseDestination: @autoclosure @escaping () -> UIViewController
25+
else elseDestination: @escaping () -> UIViewController
2626
) {
2727
self.store = store
2828
self.ifDestination = ifDestination

Examples/CaseStudies/UIKitCaseStudies/NavigateAndLoad.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class EagerNavigationViewController: UIViewController {
9090
store: self.store
9191
.scope(state: { $0.optionalCounter }, action: EagerNavigationAction.optionalCounter),
9292
then: CounterViewController.init(store:),
93-
else: ActivityIndicatorViewController()
93+
else: ActivityIndicatorViewController.init
9494
),
9595
animated: true
9696
)

Sources/ComposableArchitecture/Internal/Deprecations.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import Combine
22
import SwiftUI
33

4+
// NB: Deprecated after 0.17.0:
5+
6+
extension IfLetStore {
7+
@available(*, deprecated, message: "'else' now takes a view builder closure")
8+
public init<IfContent, ElseContent>(
9+
_ store: Store<State?, Action>,
10+
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent,
11+
else elseContent: @escaping @autoclosure () -> ElseContent
12+
) where Content == _ConditionalContent<IfContent, ElseContent> {
13+
self.init(store, then: ifContent, else: elseContent)
14+
}
15+
}
16+
417
// NB: Deprecated after 0.13.0:
518

619
@available(*, deprecated, renamed: "BindingAction")

Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where Data: Collection, ID: Hashable, Content: View {
7171
public init<EachContent>(
7272
_ store: Store<Data, (Data.Index, EachAction)>,
7373
id: KeyPath<EachState, ID>,
74-
content: @escaping (Store<EachState, EachAction>) -> EachContent
74+
@ViewBuilder content: @escaping (Store<EachState, EachAction>) -> EachContent
7575
)
7676
where
7777
Data == [EachState],
@@ -104,7 +104,7 @@ where Data: Collection, ID: Hashable, Content: View {
104104
/// - content: A function that can generate content given a store of an element.
105105
public init<EachContent>(
106106
_ store: Store<Data, (Data.Index, EachAction)>,
107-
content: @escaping (Store<EachState, EachAction>) -> EachContent
107+
@ViewBuilder content: @escaping (Store<EachState, EachAction>) -> EachContent
108108
)
109109
where
110110
Data == [EachState],
@@ -126,7 +126,7 @@ where Data: Collection, ID: Hashable, Content: View {
126126
/// - content: A function that can generate content given a store of an element.
127127
public init<EachContent: View>(
128128
_ store: Store<IdentifiedArray<ID, EachState>, (ID, EachAction)>,
129-
content: @escaping (Store<EachState, EachAction>) -> EachContent
129+
@ViewBuilder content: @escaping (Store<EachState, EachAction>) -> EachContent
130130
)
131131
where
132132
EachContent: View,

Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010
/// IfLetStore(
1111
/// store.scope(state: \SearchState.results, action: SearchAction.results),
1212
/// then: SearchResultsView.init(store:),
13-
/// else: Text("Loading search results...")
13+
/// else: { Text("Loading search results...") }
1414
/// )
1515
///
1616
/// And for performing navigation when a piece of state becomes non-`nil`:
@@ -42,8 +42,8 @@ public struct IfLetStore<State, Action, Content>: View where Content: View {
4242
/// - elseContent: A view that is only visible when the optional state is `nil`.
4343
public init<IfContent, ElseContent>(
4444
_ store: Store<State?, Action>,
45-
then ifContent: @escaping (Store<State, Action>) -> IfContent,
46-
else elseContent: @escaping @autoclosure () -> ElseContent
45+
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent,
46+
@ViewBuilder else elseContent: @escaping () -> ElseContent
4747
) where Content == _ConditionalContent<IfContent, ElseContent> {
4848
self.store = store
4949
self.content = { viewStore in
@@ -64,7 +64,7 @@ public struct IfLetStore<State, Action, Content>: View where Content: View {
6464
/// is visible only when the optional state is non-`nil`.
6565
public init<IfContent>(
6666
_ store: Store<State?, Action>,
67-
then ifContent: @escaping (Store<State, Action>) -> IfContent
67+
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent
6868
) where Content == IfContent? {
6969
self.store = store
7070
self.content = { viewStore in

0 commit comments

Comments
 (0)