Skip to content

Commit

Permalink
equatable ScreenActionSink
Browse files Browse the repository at this point in the history
  • Loading branch information
square-tomb committed Jun 22, 2023
1 parent 1970f9e commit d348077
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions Development.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Pod::Spec.new do |s|
test_spec.dependency 'BackStackContainer'
test_spec.dependency 'ModalContainer'
test_spec.dependency 'AlertContainer'
test_spec.dependency 'WorkflowSwiftUI'
test_spec.requires_app_host = true
test_spec.app_host_name = 'Development/SampleTicTacToe'
test_spec.source_files = 'Samples/TicTacToe/Tests/**/*.swift'
Expand Down
4 changes: 2 additions & 2 deletions Samples/TicTacToe/Sources/Authentication/LoginScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import SwiftUI
import Workflow
import WorkflowSwiftUI

struct LoginScreen: SwiftUIScreen {
var actionSink: Sink<LoginWorkflow.Action>
struct LoginScreen: SwiftUIScreen, Equatable {
var actionSink: ScreenActionSink<LoginWorkflow.Action>
var title: String
var email: String
var password: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension LoginWorkflow {

func render(state: LoginWorkflow.State, context: RenderContext<LoginWorkflow>) -> Rendering {
LoginScreen(
actionSink: context.makeSink(of: Action.self),
actionSink: .init(context.makeSink(of: Action.self)),
title: "Welcome! Please log in to play TicTacToe!",
email: state.email,
password: state.password
Expand Down
24 changes: 8 additions & 16 deletions Samples/TicTacToe/Tests/AuthenticationWorkflowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ class AuthenticationWorkflowTests: XCTestCase {
.expectWorkflow(
type: LoginWorkflow.self,
producingRendering: LoginScreen(
actionSink: .noop(),
title: "",
email: "",
onEmailChanged: { _ in },
password: "",
onPasswordChanged: { _ in },
onLoginTapped: {}
password: ""
)
)
.render { screen in
Expand All @@ -210,12 +208,10 @@ class AuthenticationWorkflowTests: XCTestCase {
.expectWorkflow(
type: LoginWorkflow.self,
producingRendering: LoginScreen(
actionSink: .noop(),
title: "",
email: "",
onEmailChanged: { _ in },
password: "",
onPasswordChanged: { _ in },
onLoginTapped: {}
password: ""
)
)
.expect(
Expand Down Expand Up @@ -244,12 +240,10 @@ class AuthenticationWorkflowTests: XCTestCase {
.expectWorkflow(
type: LoginWorkflow.self,
producingRendering: LoginScreen(
actionSink: .noop(),
title: "",
email: "",
onEmailChanged: { _ in },
password: "",
onPasswordChanged: { _ in },
onLoginTapped: {}
password: ""
)
)
.expect(
Expand All @@ -275,12 +269,10 @@ class AuthenticationWorkflowTests: XCTestCase {
.expectWorkflow(
type: LoginWorkflow.self,
producingRendering: LoginScreen(
actionSink: .noop(),
title: "",
email: "",
onEmailChanged: { _ in },
password: "",
onPasswordChanged: { _ in },
onLoginTapped: {}
password: ""
)
)
.render { screen in
Expand Down
38 changes: 38 additions & 0 deletions WorkflowSwiftUI/Sources/ScreenActionSink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Workflow

public struct ScreenActionSink<Value>: Equatable {
private let sink: Sink<Value>

// TODO: Only allow this to be initialized by `WorkflowNode.SubtreeManager.Context`?
public init(_ sink: Sink<Value>) {
self.sink = sink
}

public static func noop<T>() -> ScreenActionSink<T> {
ScreenActionSink<T>(Sink { _ in })
}

public func send(_ value: Value) {
sink.send(value)
}

public static func == (lhs: ScreenActionSink<Value>, rhs: ScreenActionSink<Value>) -> Bool {
true
}
}
2 changes: 1 addition & 1 deletion WorkflowSwiftUI/Sources/SwiftUIScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public protocol SwiftUIScreen: Screen {
@ViewBuilder
static func makeView(model: ObservableValue<Self>) -> Content

var actionSink: Sink<Action> { get }
var actionSink: ScreenActionSink<Action> { get }

static var isDuplicate: ((Self, Self) -> Bool)? { get }
}
Expand Down

0 comments on commit d348077

Please sign in to comment.