Skip to content

Combine Worker #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ SampleApp.xcworkspace

# ios-snapshot-test-case Failure Diffs
FailureDiffs/

Samples/**/*Info.plist
!Samples/Tutorial/AppHost/Configuration/Info.plist
!Samples/Tutorial/AppHost/TutorialTests/Info.plist
22 changes: 22 additions & 0 deletions Development.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Pod::Spec.new do |s|
s.dependency 'WorkflowUI'
s.dependency 'WorkflowReactiveSwift'
s.dependency 'WorkflowRxSwift'
# s.dependency 'WorkflowCombine' # TODO: Disabled because app specs cannot increase the deployment target of the root
s.source_files = 'Samples/Dummy.swift'

s.subspec 'Dummy' do |ss|
Expand Down Expand Up @@ -141,4 +142,25 @@ Pod::Spec.new do |s|
test_spec.dependency 'WorkflowTesting'
test_spec.dependency 'WorkflowRxSwiftTesting'
end

# TODO: Disabled because app specs cannot increase the deployment target of the root
# To use, increase the deployment target of this spec to 13.0 or higher
# s.app_spec 'WorkflowCombineSampleApp' do |app_spec|
# app_spec.source_files = 'Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/**/*.swift'
# end

# s.test_spec 'WorkflowCombineSampleAppTests' do |test_spec|
# test_spec.dependency 'Development/WorkflowCombineSampleApp'
# test_spec.requires_app_host = true
# test_spec.app_host_name = 'Development/WorkflowCombineSampleApp'
# test_spec.source_files = 'Samples/WorkflowCombineSampleApp/WorkflowCombineSampleAppUnitTests/**/*.swift'
# end
#
# s.test_spec 'WorkflowCombineTests' do |test_spec|
# test_spec.requires_app_host = true
# test_spec.source_files = 'WorkflowCombine/Tests/**/*.swift'
# test_spec.framework = 'XCTest'
# test_spec.dependency 'WorkflowTesting'
# test_spec.dependency 'WorkflowCombineTesting'
# end
end
9 changes: 9 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ let package = Package(
name: "WorkflowReactiveSwift",
targets: ["WorkflowReactiveSwift"]
),
.library(
name: "WorkflowCombine",
targets: ["WorkflowCombine"]
),
],
dependencies: [
.package(url: "https://github.com/ReactiveCocoa/ReactiveSwift.git", from: "6.3.0"),
Expand Down Expand Up @@ -62,6 +66,11 @@ let package = Package(
dependencies: ["ReactiveSwift", "Workflow"],
path: "WorkflowReactiveSwift/Sources"
),
.target(
name: "WorkflowCombine",
dependencies: ["Workflow"],
path: "WorkflowCombine/Sources"
),
],
swiftLanguageVersions: [.v5]
)
60 changes: 0 additions & 60 deletions Samples/SampleSwiftUIApp/SampleSwiftUIApp/Info.plist

This file was deleted.

22 changes: 0 additions & 22 deletions Samples/TicTacToe/Tests/Info.plist

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// AppDelegate.swift
// WorkflowCombineSampleApp
//
// Created by Soo Rin Park on 10/28/21.
//

import UIKit
import WorkflowUI

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ContainerViewController(workflow: DemoWorkflow())
window?.makeKeyAndVisible()

return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// DemoViewController.swift
// WorkflowCombineSampleApp
//
// Created by Soo Rin Park on 10/28/21.
//

import Foundation
import UIKit
import WorkflowUI

class DemoViewController: ScreenViewController<DemoScreen> {
private let label = UILabel()

override func viewDidLoad() {
super.viewDidLoad()

label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
label.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

override func screenDidChange(from previousScreen: DemoScreen, previousEnvironment: ViewEnvironment) {
super.screenDidChange(from: previousScreen, previousEnvironment: previousEnvironment)

label.text = screen.date
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// DemoWorker.swift
// WorkflowCombineSampleApp
//
// Created by Soo Rin Park on 10/28/21.
//

import Combine
import Workflow
import WorkflowCombine
import WorkflowUI

// MARK: Workers

extension DemoWorkflow {
struct DemoWorker: WorkflowCombine.Worker {
typealias Output = Action

// This publisher publishes the current date on a timer that fires every second
func run() -> AnyPublisher<Action, Never> {
Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()
.map { Action(publishedDate: $0) }
.eraseToAnyPublisher()
}

func isEquivalent(to otherWorker: DemoWorkflow.DemoWorker) -> Bool { true }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// DemoWorkflow.swift
// WorkflowCombineSampleApp
//
// Created by Soo Rin Park on 10/28/21.
//

import Workflow
import WorkflowUI

// MARK: Input and Output

let dateFormatter = DateFormatter()

struct DemoWorkflow: Workflow {
typealias Output = Never
}

// MARK: State and Initialization

extension DemoWorkflow {
struct State: Equatable {
var date: Date
}

func makeInitialState() -> DemoWorkflow.State { State(date: Date()) }

func workflowDidChange(from previousWorkflow: DemoWorkflow, state: inout State) {}
}

// MARK: Actions

extension DemoWorkflow {
struct Action: WorkflowAction {
typealias WorkflowType = DemoWorkflow

let publishedDate: Date

func apply(toState state: inout DemoWorkflow.State) -> DemoWorkflow.Output? {
state.date = publishedDate
return nil
}
}
}

// MARK: Rendering

extension DemoWorkflow {
// TODO: Change this to your actual rendering type
typealias Rendering = DemoScreen

func render(state: DemoWorkflow.State, context: RenderContext<DemoWorkflow>) -> Rendering {
DemoWorker()
.rendered(in: context)

dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let formattedDate = dateFormatter.string(from: state.date)
let rendering = Rendering(date: formattedDate)

return rendering
}
}

struct DemoScreen: Screen {
let date: String

func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
DemoViewController.description(for: self, environment: environment)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// DemoWorkflowTests.swift
// WorkflowCombineSampleAppUnitTests
//
// Created by Soo Rin Park on 11/1/21.
//

import Combine
import Workflow
import WorkflowTesting
import XCTest
@testable import Development_WorkflowCombineSampleApp

class DemoWorkflowTests: XCTestCase {
func test_workflowIsRenderedEverySecondForFiveSeconds() {
let expectedDate = Date(timeIntervalSince1970: 0)

DemoWorkflow
.Action
.tester(withState: .init(date: Date())) // the initial date itself does not matter
.send(action: .init(publishedDate: expectedDate))
.assert(state: .init(date: expectedDate))
}
}
24 changes: 24 additions & 0 deletions WorkflowCombine.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative('version')

Pod::Spec.new do |s|
s.name = 'WorkflowCombine'
s.version = WORKFLOW_VERSION
s.summary = 'Infrastructure for Combined-powered Workers'
s.homepage = 'https://www.github.com/square/workflow-swift'
s.license = 'Apache License, Version 2.0'
s.author = 'Square'
s.source = { :git => 'https://github.com/square/workflow-swift.git', :tag => "v#{s.version}" }

# 1.7 is needed for `swift_versions` support
s.cocoapods_version = '>= 1.7.0'

s.swift_versions = ['5.1']
s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'

s.source_files = 'WorkflowCombine/Sources/*.swift'

s.dependency 'Workflow', "#{s.version}"

end

Loading