Skip to content

Commit

Permalink
expand produce costuzation/ (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidas-trafi authored Jan 29, 2024
1 parent 9388a9e commit 23df85d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
55 changes: 49 additions & 6 deletions Sources/StoryFlow/Interfaces/Helpers/Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,32 @@ extension InputRequiring where Self: UIViewController {

extension OutputProducing where Self: UIViewController {

public init(nibName: String? = nil, bundle: Bundle? = nil, produce: @escaping (OutputType) -> ()) {
/// - Parameters:
/// - produceStub: Closure that optionally overrides default produce behavior.
/// When nil is returned default produce behaviour will not be triggered.
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
produceStub: @escaping (OutputType) -> OutputType?
) {
self.init(nibName: nibName, bundle: bundle)
produceStub = produce
self.produceStub = produceStub
}

/// - Parameters:
/// - produceStub: Closure that overrides default produce behavior.
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
produceStub: @escaping (OutputType) -> ()
) {
self.init(nibName: nibName, bundle: bundle) {
produceStub($0)
return nil
}
}

var produceStub: ((OutputType) -> ())? {
var produceStub: ((OutputType) -> OutputType?)? {
get { return associated(with: &produceStubKey) }
set { associate(newValue, with: &produceStubKey) }
}
Expand All @@ -25,10 +45,33 @@ private var produceStubKey = 0

extension InputRequiring where Self: UIViewController & OutputProducing {

public init(nibName: String? = nil, bundle: Bundle? = nil,
input: InputType, produce: @escaping (OutputType) -> ()) {
/// - Parameters:
/// - produceStub: Closure that optionally overrides default produce behavior.
/// When nil is returned default produce behaviour will not be triggered.
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
input: InputType,
produceStub: @escaping (OutputType) -> OutputType?
) {
self.init(nibName: nibName, bundle: bundle)
self.input = input
self.produceStub = produce
self.produceStub = produceStub
}

/// - Parameters:
/// - produceStub: Closure that overrides default produce behavior.
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
input: InputType,
produceStub: @escaping (OutputType) -> ()
) {
self.init(nibName: nibName, bundle: bundle) {
produceStub($0)
return nil
}
self.input = input
}

}
15 changes: 11 additions & 4 deletions Sources/StoryFlow/Interfaces/OuputProducing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ extension OutputProducing where Self: UIViewController {
- Parameter output: The value being passed to next view controller.
*/
public func produce(_ output: OutputType) {

if let produce = produceStub {
produce(output)
} else if let flow = flow {
if let produceStub {
if let customOutput = produceStub(output) {
_produce(customOutput)
}
} else {
_produce(output)
}
}

private func _produce(_ output: OutputType) {
if let flow = flow {
flow.proceed(with: output, from: self)
} else {
implicitFlow.proceed(with: output, from: self)
Expand Down
4 changes: 2 additions & 2 deletions StoryFlowTests/TestabilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestabilityTests: XCTestCase {
class Vc: UIViewController, OutputProducing { typealias OutputType = T }

var producedOutput: T!
let vc = Vc(produce: { producedOutput = $0 })
let vc = Vc(produceStub: { producedOutput = $0 })

let output = T()

Expand All @@ -48,7 +48,7 @@ class TestabilityTests: XCTestCase {
var producedOutput: T!

// Act
let vc = Vc(input: value, produce: { producedOutput = $0 })
let vc = Vc(input: value, produceStub: { producedOutput = $0 })
vc.produce(value)

// Assert
Expand Down

0 comments on commit 23df85d

Please sign in to comment.