Skip to content

Commit

Permalink
[master] - 'Add convenience for the Never Swift Type so FlowRepresent…
Browse files Browse the repository at this point in the history
…ables can declare they do not take in data - TT'
  • Loading branch information
Tyler-Keith-Thompson committed Oct 19, 2019
1 parent df3528f commit bb689dc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: swift
osx_image: xcode10.3
osx_image: xcode11.1
xcode_workspace: Workflow.xcworkspace
xcode_scheme: Workflow
xcode_destination: platform=iOS Simulator,name=iPhone 8
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PODS:
- CwlCatchException (1.0.2)
- CwlPreconditionTesting (1.1.1):
- CwlCatchException
- DynamicWorkflow (0.0.3)
- DynamicWorkflow (0.0.7)
- UIUTest (1.6.0)

DEPENDENCIES:
Expand All @@ -12,7 +12,7 @@ DEPENDENCIES:
- UIUTest

SPEC REPOS:
https://github.com/cocoapods/specs.git:
trunk:
- UIUTest

EXTERNAL SOURCES:
Expand All @@ -36,9 +36,9 @@ CHECKOUT OPTIONS:
SPEC CHECKSUMS:
CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c
CwlPreconditionTesting: d33a4e4f285c0b885fddcae5dfedfbb34d4f3961
DynamicWorkflow: a45e06c1d0448d295ed3572ccc591a347fe40237
DynamicWorkflow: 214b6218b7e17dc458c865ea5f09436bf4922da5
UIUTest: 842c642e5bec098b1e2c890cbe25aacab80f2481

PODFILE CHECKSUM: 5f55f064de83e2e4f31f04427448900a8e9571f2

COCOAPODS: 1.7.5
COCOAPODS: 1.8.3
16 changes: 14 additions & 2 deletions Workflow/Protocols/FlowRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ public protocol FlowRepresentable: AnyFlowRepresentable {
/// - Parameter args: Note you can rename this in your implementation if 'args' doesn't make sense. If a previous item in a workflow tries to pass a type that does not match `shouldLoad` will automatically be false, and this method will not be called.
/// - Returns: Void
/// - Note: This method is called *before* your view loads. Do not attempt to do any UI work in this method. This is however a good place to set up data on your view.
func shouldLoad(with args:IntakeType) -> Bool
mutating func shouldLoad(with args:IntakeType) -> Bool
}

extension FlowRepresentable where IntakeType == Never {
mutating func erasedShouldLoad(with args: Any?) -> Bool {
return shouldLoad()
}

mutating func shouldLoad(with args: Never) -> Bool {}

func shouldLoad() -> Bool {
return true
}
}

public extension FlowRepresentable {
func erasedShouldLoad(with args:Any?) -> Bool {
mutating func erasedShouldLoad(with args:Any?) -> Bool {
guard let cast = args as? IntakeType else { return false }
return shouldLoad(with: cast)
}
Expand Down
2 changes: 1 addition & 1 deletion Workflow/Protocols/TypeErased/AnyFlowRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public protocol AnyFlowRepresentable {
var workflow:Workflow? { get set }
var callback:((Any?) -> Void)? { get set }

func erasedShouldLoad(with args:Any?) -> Bool
mutating func erasedShouldLoad(with args:Any?) -> Bool

/// instance: A method to return an instance of the `FlowRepresentable`
/// - Returns: `AnyFlowRepresentable`. Specifically a new instance from the static class passed to a `Workflow`
Expand Down
20 changes: 20 additions & 0 deletions WorkflowTests/UIKitPresenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,26 @@ class UIKitPresenterTests: XCTestCase {
XCTAssert(UIApplication.topViewController() is ExpectedModal, "Top View was not a modal")
XCTAssertNil((UIApplication.topViewController() as? ExpectedModal)?.navigationController, "You didn't present modally")
}

func testFlowRepresentableThatDoesNotTakeInData() {
class ExpectedController: UIWorkflowItem<Never>, FlowRepresentable {
static func instance() -> AnyFlowRepresentable {
let controller = ExpectedController()
controller.view.backgroundColor = .green
return controller
}
}

let rootController = UIViewController()
loadView(controller: rootController)

rootController.launchInto(Workflow([ExpectedController.self]), withLaunchStyle: .navigationStack)
RunLoop.current.singlePass()

XCTAssert(rootController.mostRecentlyPresentedViewController is UINavigationController, "mostRecentlyPresentedViewController should be nav controller: \(String(describing: rootController.mostRecentlyPresentedViewController))")
XCTAssertEqual((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.count, 1)
XCTAssert((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first is ExpectedController)
}
}

extension UIKitPresenterTests {
Expand Down
6 changes: 4 additions & 2 deletions WorkflowTests/WorkflowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class WorkflowTests: XCTestCase {
}
}
let flow:[AnyFlowRepresentable.Type] = [FR1.self, FR2.self]
_ = flow.first?.instance().erasedShouldLoad(with: "str")
_ = flow.last?.instance().erasedShouldLoad(with: 1)
var first = flow.first?.instance()
var last = flow.last?.instance()
_ = first?.erasedShouldLoad(with: "str")
_ = last?.erasedShouldLoad(with: 1)

XCTAssert(FR1.shouldLoadCalledOnFR1, "Should load not called on flow representable 1 with correct corresponding type")
XCTAssert(FR2.shouldLoadCalledOnFR2, "Should load not called on flow representable 2 with correct corresponding type")
Expand Down

0 comments on commit bb689dc

Please sign in to comment.