Skip to content

Commit

Permalink
expand produce costuzation/
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidas-trafi committed Jan 2, 2024
1 parent 9388a9e commit 04fdb6c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
40 changes: 36 additions & 4 deletions Sources/StoryFlow/Interfaces/Helpers/Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@ extension InputRequiring where Self: UIViewController {

extension OutputProducing where Self: UIViewController {

public init(nibName: String? = nil, bundle: Bundle? = nil, produce: @escaping (OutputType) -> ()) {
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
produce: @escaping (OutputType) -> OutputType?
) {
self.init(nibName: nibName, bundle: bundle)
produceStub = produce
}

public init(
nibName: String? = nil,
bundle: Bundle? = nil,
produce: @escaping (OutputType) -> ()
) {
self.init(nibName: nibName, bundle: bundle) {
produce($0)
return nil
}
}

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

extension InputRequiring where Self: UIViewController & OutputProducing {

public init(nibName: String? = nil, bundle: Bundle? = nil,
input: InputType, produce: @escaping (OutputType) -> ()) {
public init(
nibName: String? = nil,
bundle: Bundle? = nil,
input: InputType,
produce: @escaping (OutputType) -> OutputType?
) {
self.init(nibName: nibName, bundle: bundle)
self.input = input
self.produceStub = produce
}

public init(
nibName: String? = nil,
bundle: Bundle? = nil,
input: InputType,
produce: @escaping (OutputType) -> ()
) {
self.init(nibName: nibName, bundle: bundle) {
produce($0)
return nil
}
}

}
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

0 comments on commit 04fdb6c

Please sign in to comment.