Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand produce customisation #23

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading