Skip to content

Commit d348077

Browse files
committed
equatable ScreenActionSink
1 parent 1970f9e commit d348077

File tree

6 files changed

+51
-20
lines changed

6 files changed

+51
-20
lines changed

Development.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Pod::Spec.new do |s|
7373
test_spec.dependency 'BackStackContainer'
7474
test_spec.dependency 'ModalContainer'
7575
test_spec.dependency 'AlertContainer'
76+
test_spec.dependency 'WorkflowSwiftUI'
7677
test_spec.requires_app_host = true
7778
test_spec.app_host_name = 'Development/SampleTicTacToe'
7879
test_spec.source_files = 'Samples/TicTacToe/Tests/**/*.swift'

Samples/TicTacToe/Sources/Authentication/LoginScreen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import SwiftUI
1818
import Workflow
1919
import WorkflowSwiftUI
2020

21-
struct LoginScreen: SwiftUIScreen {
22-
var actionSink: Sink<LoginWorkflow.Action>
21+
struct LoginScreen: SwiftUIScreen, Equatable {
22+
var actionSink: ScreenActionSink<LoginWorkflow.Action>
2323
var title: String
2424
var email: String
2525
var password: String

Samples/TicTacToe/Sources/Authentication/LoginWorkflow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extension LoginWorkflow {
7373

7474
func render(state: LoginWorkflow.State, context: RenderContext<LoginWorkflow>) -> Rendering {
7575
LoginScreen(
76-
actionSink: context.makeSink(of: Action.self),
76+
actionSink: .init(context.makeSink(of: Action.self)),
7777
title: "Welcome! Please log in to play TicTacToe!",
7878
email: state.email,
7979
password: state.password

Samples/TicTacToe/Tests/AuthenticationWorkflowTests.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,10 @@ class AuthenticationWorkflowTests: XCTestCase {
183183
.expectWorkflow(
184184
type: LoginWorkflow.self,
185185
producingRendering: LoginScreen(
186+
actionSink: .noop(),
186187
title: "",
187188
email: "",
188-
onEmailChanged: { _ in },
189-
password: "",
190-
onPasswordChanged: { _ in },
191-
onLoginTapped: {}
189+
password: ""
192190
)
193191
)
194192
.render { screen in
@@ -210,12 +208,10 @@ class AuthenticationWorkflowTests: XCTestCase {
210208
.expectWorkflow(
211209
type: LoginWorkflow.self,
212210
producingRendering: LoginScreen(
211+
actionSink: .noop(),
213212
title: "",
214213
email: "",
215-
onEmailChanged: { _ in },
216-
password: "",
217-
onPasswordChanged: { _ in },
218-
onLoginTapped: {}
214+
password: ""
219215
)
220216
)
221217
.expect(
@@ -244,12 +240,10 @@ class AuthenticationWorkflowTests: XCTestCase {
244240
.expectWorkflow(
245241
type: LoginWorkflow.self,
246242
producingRendering: LoginScreen(
243+
actionSink: .noop(),
247244
title: "",
248245
email: "",
249-
onEmailChanged: { _ in },
250-
password: "",
251-
onPasswordChanged: { _ in },
252-
onLoginTapped: {}
246+
password: ""
253247
)
254248
)
255249
.expect(
@@ -275,12 +269,10 @@ class AuthenticationWorkflowTests: XCTestCase {
275269
.expectWorkflow(
276270
type: LoginWorkflow.self,
277271
producingRendering: LoginScreen(
272+
actionSink: .noop(),
278273
title: "",
279274
email: "",
280-
onEmailChanged: { _ in },
281-
password: "",
282-
onPasswordChanged: { _ in },
283-
onLoginTapped: {}
275+
password: ""
284276
)
285277
)
286278
.render { screen in
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 Square Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Workflow
18+
19+
public struct ScreenActionSink<Value>: Equatable {
20+
private let sink: Sink<Value>
21+
22+
// TODO: Only allow this to be initialized by `WorkflowNode.SubtreeManager.Context`?
23+
public init(_ sink: Sink<Value>) {
24+
self.sink = sink
25+
}
26+
27+
public static func noop<T>() -> ScreenActionSink<T> {
28+
ScreenActionSink<T>(Sink { _ in })
29+
}
30+
31+
public func send(_ value: Value) {
32+
sink.send(value)
33+
}
34+
35+
public static func == (lhs: ScreenActionSink<Value>, rhs: ScreenActionSink<Value>) -> Bool {
36+
true
37+
}
38+
}

WorkflowSwiftUI/Sources/SwiftUIScreen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public protocol SwiftUIScreen: Screen {
2727
@ViewBuilder
2828
static func makeView(model: ObservableValue<Self>) -> Content
2929

30-
var actionSink: Sink<Action> { get }
30+
var actionSink: ScreenActionSink<Action> { get }
3131

3232
static var isDuplicate: ((Self, Self) -> Bool)? { get }
3333
}

0 commit comments

Comments
 (0)