Skip to content

Commit b3d0dd3

Browse files
committed
feat: expose helpers for static models
1 parent a29d9d1 commit b3d0dd3

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

WorkflowSwiftUI/Sources/ActionModel.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
public struct ActionModel<State: ObservableState, Action>: ObservableModel, SingleActionModel {
66
public let accessor: StateAccessor<State>
77
public let sendAction: (Action) -> Void
8+
9+
public init(accessor: StateAccessor<State>, sendAction: @escaping (Action) -> Void) {
10+
self.accessor = accessor
11+
self.sendAction = sendAction
12+
}
813
}
914

1015
/// An observable model with a single action.
@@ -22,3 +27,15 @@ extension ActionModel: Identifiable where State: Identifiable {
2227
accessor.id
2328
}
2429
}
30+
31+
#if DEBUG
32+
33+
public extension ActionModel {
34+
/// Creates a static model which ignores all sent values, suitable for static previews
35+
/// or testing.
36+
static func `static`(state: State) -> ActionModel<State, Action> {
37+
ActionModel(accessor: .static(state: state), sendAction: { _ in })
38+
}
39+
}
40+
41+
#endif

WorkflowSwiftUI/Sources/StateAccessor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ extension StateAccessor: Identifiable where State: Identifiable {
3131
state.id
3232
}
3333
}
34+
35+
#if DEBUG
36+
37+
public extension StateAccessor {
38+
/// Creates a static state accessor which ignores all sent values, suitable for static previews
39+
/// or testing.
40+
static func `static`(state: State) -> StateAccessor<State> {
41+
StateAccessor(state: state, sendValue: { _ in })
42+
}
43+
}
44+
45+
#endif

WorkflowSwiftUI/Sources/Store+Preview.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,17 @@ public struct StaticStorePreviewContext {
1212
}
1313

1414
public func makeStateAccessor<State>(state: State) -> StateAccessor<State> {
15-
StateAccessor(
16-
state: state,
17-
sendValue: { _ in }
18-
)
15+
.static(state: state)
1916
}
2017

2118
public func makeActionModel<State, Action>(
2219
state: State
2320
) -> ActionModel<State, Action> {
24-
ActionModel(
25-
accessor: makeStateAccessor(state: state),
26-
sendAction: makeSink(of: Action.self).send
27-
)
21+
.static(state: state)
2822
}
2923
}
3024

31-
extension Store {
25+
public extension Store {
3226
/// Generates a static store for previews.
3327
///
3428
/// Previews generated with this method are static and do not update state. To generate a
@@ -38,15 +32,15 @@ extension Store {
3832
/// - Parameter makeModel: A closure to create the store's model. The provided `context` param
3933
/// is a convenience to generate dummy sinks and state accessors.
4034
/// - Returns: A store for previews.
41-
public static func preview(
35+
static func preview(
4236
makeModel: (StaticStorePreviewContext) -> Model
4337
) -> Store {
4438
let context = StaticStorePreviewContext()
4539
let model = makeModel(context)
4640
let (store, _) = make(model: model)
4741
return store
4842
}
49-
43+
5044
/// Generates a static store for previews.
5145
///
5246
/// Previews generated with this method are static and do not update state. To generate a
@@ -55,14 +49,14 @@ extension Store {
5549
///
5650
/// - Parameter state: The state of the view.
5751
/// - Returns: A store for previews.
58-
public static func preview<State, Action>(
52+
static func preview<State, Action>(
5953
state: State
6054
) -> Store<ActionModel<State, Action>> where Model == ActionModel<State, Action> {
6155
preview { context in
6256
context.makeActionModel(state: state)
6357
}
6458
}
65-
59+
6660
/// Generates a static store for previews.
6761
///
6862
/// Previews generated with this method are static and do not update state. To generate a
@@ -71,7 +65,7 @@ extension Store {
7165
///
7266
/// - Parameter state: The state of the view.
7367
/// - Returns: A store for previews.
74-
public static func preview<State>(
68+
static func preview<State>(
7569
state: State
7670
) -> Store<StateAccessor<State>> where Model == StateAccessor<State> {
7771
preview { context in

0 commit comments

Comments
 (0)