Skip to content

Commit

Permalink
Merge pull request #4 from trafi/feature/destination
Browse files Browse the repository at this point in the history
add destination in ImplicitFlow
  • Loading branch information
Domas Nutautas authored Nov 12, 2019
2 parents 9c3a69c + fd4682e commit a1951c0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
30 changes: 27 additions & 3 deletions StoryFlow/Flow/ImplicitFlow/ImplicitFlow.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import Foundation
import UIKit

extension Flow {

/**
Returns view controller by matching type of provided `value`

See [tests examples](https://github.com/trafi/StoryFlow/blob/master/StoryFlowTests/ImplicitFlowTests.swift).

- Parameter value: The value being passed to next view controller.
*/
public static func destination(for value: Value) -> UIViewController? {
return destination(for: (value, Value.self))
}

private static func destination(for output: ValueAndType) -> UIViewController? {
let (value, type) = OutputTransform.apply(output)

for inType in inputRequiringTypes where oneOf(inType._inputType, contains: type) {
return inType._create(input: value)
}

return nil
}

}

extension Flow {

public static func implicit() -> Flow {
Expand All @@ -24,9 +49,8 @@ extension Flow {
}

// MARK: Input

for inType in inputRequiringTypes where oneOf(inType._inputType, contains: type) {
let to = inType._create(input: value)

if let to = Flow<Any>.destination(for: output) {
let transition = TransitionInfo(from: from, producedType: output.type, receivedType: type, to: to, isUnwind: false)
if CustomTransition.attempt(transition) == false {
from.show(to, sender: nil)
Expand Down
58 changes: 58 additions & 0 deletions StoryFlowTests/ImplicitFlowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ import XCTest
import StoryFlow

class ImplicitFlowTests: XCTestCase {

// MARK: Destination

func testDestination_itReturnsNilWhenNoVcFound() {

// Arrange
class T {}

let output = T()

// Act
let destination = Flow<T>.destination(for: output)

// Assert
XCTAssert(destination == nil)
}


func testDestination_itReturnsVcWithInput() {

// Arrange
class T {}

class To: UIViewController, InputRequiring { typealias InputType = T }

let output = T()

// Act
let destination = Flow<T>.destination(for: output)

// Assert
XCTAssert(destination is To)
XCTAssert((destination as! To).input === output)
}


// MARK: Show

Expand Down Expand Up @@ -483,6 +518,29 @@ class ImplicitFlowTests: XCTestCase {
}

// MARK: Output transforms

func testDestination_itReturnsNextVcByTransformedOutput() {

// Arrange
class T1 {}
class T2 {}

class To: UIViewController, InputRequiring { typealias InputType = T2 }

let transformedOutput = T2()

OutputTransform.register { (_: T1) in transformedOutput }

// Act
let destination = Flow.destination(for: T1())

// Assert
XCTAssert(destination is To)
XCTAssert((destination as! To).input === transformedOutput)

// Clean up
OutputTransform.reset()
}

func testProduce_itShowsNextVcByTransformedOutput() {

Expand Down

0 comments on commit a1951c0

Please sign in to comment.