Skip to content

Instantly share code, notes, and snippets.

@freddi-kit
Created December 21, 2019 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freddi-kit/e011d2e0c98c1ec5857e460496c2e25e to your computer and use it in GitHub Desktop.
Save freddi-kit/e011d2e0c98c1ec5857e460496c2e25e to your computer and use it in GitHub Desktop.
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