From bb689dc2939ea9db9e8c17dcd8e6a263cefc598d Mon Sep 17 00:00:00 2001 From: Tyler Thompson Date: Sat, 19 Oct 2019 12:41:21 -0600 Subject: [PATCH] [master] - 'Add convenience for the Never Swift Type so FlowRepresentables can declare they do not take in data - TT' --- .travis.yml | 2 +- Podfile.lock | 8 ++++---- Workflow/Protocols/FlowRepresentable.swift | 16 +++++++++++++-- .../TypeErased/AnyFlowRepresentable.swift | 2 +- WorkflowTests/UIKitPresenterTests.swift | 20 +++++++++++++++++++ WorkflowTests/WorkflowTests.swift | 6 ++++-- 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7b64979f6..2c8f2cb3a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Podfile.lock b/Podfile.lock index 0e5e8ddd2..97483dca0 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -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: @@ -12,7 +12,7 @@ DEPENDENCIES: - UIUTest SPEC REPOS: - https://github.com/cocoapods/specs.git: + trunk: - UIUTest EXTERNAL SOURCES: @@ -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 diff --git a/Workflow/Protocols/FlowRepresentable.swift b/Workflow/Protocols/FlowRepresentable.swift index dc481de4e..1fef486b0 100644 --- a/Workflow/Protocols/FlowRepresentable.swift +++ b/Workflow/Protocols/FlowRepresentable.swift @@ -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) } diff --git a/Workflow/Protocols/TypeErased/AnyFlowRepresentable.swift b/Workflow/Protocols/TypeErased/AnyFlowRepresentable.swift index c26230119..eb7992c8b 100644 --- a/Workflow/Protocols/TypeErased/AnyFlowRepresentable.swift +++ b/Workflow/Protocols/TypeErased/AnyFlowRepresentable.swift @@ -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` diff --git a/WorkflowTests/UIKitPresenterTests.swift b/WorkflowTests/UIKitPresenterTests.swift index 8af2c2a10..acee3f4ca 100644 --- a/WorkflowTests/UIKitPresenterTests.swift +++ b/WorkflowTests/UIKitPresenterTests.swift @@ -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, 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 { diff --git a/WorkflowTests/WorkflowTests.swift b/WorkflowTests/WorkflowTests.swift index 630f7795a..122badaeb 100644 --- a/WorkflowTests/WorkflowTests.swift +++ b/WorkflowTests/WorkflowTests.swift @@ -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")