Created
December 21, 2019 07:58
-
-
Save freddi-kit/e011d2e0c98c1ec5857e460496c2e25e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol Redirectable where Self: UIViewController { | |
associatedtype Input | |
associatedtype Result | |
associatedtype Next | |
var nextVC: Next { get set } | |
func inject(input: Input) | |
func _injectToNext(_ nextRedirectionDependency: Result) -> Next | |
} | |
extension Redirectable where Self.Result == Next.Input, Next: Redirectable { | |
func redirectToNext(next: Result, isPush: Bool) { | |
isPush ? | |
navigationController?.pushViewController(_injectToNext(next), animated: true) : | |
{ | |
dismiss(animated: true, completion: { [unowned self] in | |
self.present(self._injectToNext(next), animated: true, completion: nil) | |
}) | |
}() | |
} | |
func _injectToNext(_ nextRedirectionDependency: Result) -> Next { | |
nextVC.inject(input: nextRedirectionDependency) | |
return nextVC | |
} | |
} | |
extension Redirectable where Self.Next == Never { | |
var nextVC: Never { | |
get { fatalError() } | |
set { /* Do nothing */ } | |
} | |
func redirectToNext(next: Result, isPush: Bool) { | |
// Do nothing | |
} | |
func _injectToNext(_ nextRedirectionDependency: Result) -> Next { | |
fatalError() | |
} | |
} | |
extension Redirectable where Self.Input == Void { | |
func inject(input _: Void) { | |
// Do nothing | |
} | |
} | |
class ViewController1: UIViewController, Redirectable { | |
typealias Result = ViewController2.Input | |
var nextVC: ViewController2 = ViewController2() | |
private var value: Int? | |
func inject(input: Int) { | |
value = input | |
} | |
} | |
class ViewController2: UIViewController, Redirectable { | |
typealias Input = Void | |
typealias Result = Int | |
typealias Next = Never | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment