-
-
Save mikelikespie/3abe371bf7f7ab67f71d6cfa22c0145d 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
/// Does something special that ViewController_A wants | |
struct Service_A { | |
func doSomething() { | |
} | |
} | |
/// Does something special that ViewController_B wants | |
struct Service_B { | |
func doSomething() { | |
} | |
} | |
/// Does something special that ViewController_C wants | |
struct Service_C { | |
func doSomething() { | |
} | |
} | |
/// Does something special that ViewController_D wants | |
struct Service_D { | |
func doSomething() { | |
} | |
} | |
class ViewController : UIViewController { | |
init() { | |
super.init(nibName: nil, bundle: nil) | |
} | |
/// make it so we can have a simple example w/o stupid constructors | |
@available(*, unavailable) | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
/// Responsible for calling serviceA then presenting ViewController_B | |
class ViewController_A : ViewController { | |
let serviceA: Service_A | |
// Note that this is equivalent to Provider<ViewController_B>, but probably more clear as an example | |
let vcProvider: () -> ViewController_B | |
init( | |
let serviceA: Service_A, | |
let vcProvider: () -> ViewController_B | |
) { | |
self.vcProvider = vcProvider | |
self.serviceA = serviceA | |
super.init() | |
} | |
func showNestedViewController() { | |
serviceA.doSomething() | |
presentViewController( | |
vcProvider(), | |
animated: true, | |
completion: nil | |
) | |
} | |
} | |
/// Responsible for calling serviceB then presenting ViewController_C | |
class ViewController_B : ViewController { | |
let serviceB: Service_B | |
let vcProvider: () -> ViewController_C | |
init( | |
let serviceB: Service_B, | |
let vcProvider: () -> ViewController_C | |
) { | |
self.vcProvider = vcProvider | |
self.serviceB = serviceB | |
super.init() | |
} | |
func showNestedViewController() { | |
serviceB.doSomething() | |
presentViewController( | |
vcProvider(), | |
animated: true, | |
completion: nil | |
) | |
} | |
} | |
/// Responsible for calling serviceC then presenting ViewController_D | |
class ViewController_C : ViewController { | |
let serviceC: Service_C | |
let vcProvider: () -> ViewController_D | |
init( | |
let serviceB: Service_C, | |
let vcProvider: () -> ViewController_D | |
) { | |
self.vcProvider = vcProvider | |
self.serviceC = serviceB | |
super.init() | |
} | |
func showNestedViewController() { | |
serviceC.doSomething() | |
presentViewController( | |
vcProvider(), | |
animated: true, | |
completion: nil | |
) | |
} | |
} | |
/// Responsible for calling serviceC then presenting ViewController_D | |
class ViewController_D : ViewController { | |
let serviceD: Service_D | |
init( | |
serviceD: Service_D | |
) { | |
self.serviceD = serviceD | |
super.init() | |
} | |
func doSomething() { | |
serviceD.doSomething() | |
} | |
} | |
struct VC_A_Component : Component { | |
typealias Root = ViewController_A | |
func configure<B : Binder>(binder binder: B) { | |
// Note, most of this would probably go in separate modules in an actual use case | |
binder.bind().to(factory: ViewController_A.init) | |
binder.bind().to(factory: ViewController_B.init) | |
binder.bind().to(factory: ViewController_C.init) | |
binder.bind().to(factory: ViewController_D.init) | |
binder.bind().to(factory: Service_A.init) | |
binder.bind().to(factory: Service_B.init) | |
binder.bind().to(factory: Service_C.init) | |
binder.bind().to(factory: Service_D.init) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment