Created
June 16, 2016 17:22
-
-
Save mikelikespie/db480360fe9b261cd3441c18a14bcbc9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() { | |
} | |
} | |
/// Responsible for calling serviceA then presenting ViewController_B | |
class ViewController_A : UIViewController { | |
var serviceA: Service_A! | |
var propertyInjector: PropertyInjector<ViewController_B>! | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
// An if would be fine, but using a switch to demonstrate the easiest way if one had multiple VCs | |
switch segue.destinationViewController { | |
case let vc as ViewController_B: | |
serviceA.doSomething() | |
propertyInjector.injectProperties(into: vc) | |
default: | |
break | |
} | |
} | |
func injectProperties(serviceA: Service_A, propertyInjector: PropertyInjector<ViewController_B>) { | |
self.serviceA = serviceA | |
self.propertyInjector = propertyInjector | |
} | |
} | |
class ViewController_B : UIViewController { | |
var serviceB: Service_B! | |
var propertyInjector: PropertyInjector<ViewController_C>! | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
// An if would be fine, but using a switch to demonstrate the easiest way if one had multiple VCs | |
switch segue.destinationViewController { | |
case let vc as ViewController_C: | |
serviceB.doSomething() | |
propertyInjector.injectProperties(into: vc) | |
default: | |
break | |
} | |
} | |
func injectProperties(serviceB: Service_B, propertyInjector: PropertyInjector<ViewController_C>) { | |
self.serviceB = serviceB | |
self.propertyInjector = propertyInjector | |
} | |
} | |
class ViewController_C : UIViewController { | |
var serviceC: Service_C! | |
var propertyInjector: PropertyInjector<ViewController_D>! | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
// An if would be fine, but using a switch to demonstrate the easiest way if one had multiple VCs | |
switch segue.destinationViewController { | |
case let vc as ViewController_D: | |
serviceC.doSomething() | |
propertyInjector.injectProperties(into: vc) | |
default: | |
break | |
} | |
} | |
func injectProperties(serviceC: Service_C, propertyInjector: PropertyInjector<ViewController_D>) { | |
self.serviceC = serviceC | |
self.propertyInjector = propertyInjector | |
} | |
} | |
class ViewController_D : UIViewController { | |
var serviceD: Service_D! | |
func doSomething() { | |
serviceD.doSomething() | |
} | |
} | |
struct VC_A_Component : Component { | |
typealias Root = ViewController_A | |
func configure<B : Binder>(binder binder: B) { | |
binder | |
.bind(UIStoryboard.self) | |
.asSingleton() | |
.to { UIStoryboard(name: "SomeStoryboard", bundle: nil) } | |
// Note, most of this would probably go in separate modules in an actual use case | |
binder.bind(ViewController_A.self).to(factory: makeRootViewController) | |
binder | |
.bindPropertyInjectionOf(ViewController_A.self) | |
.to(injector: ViewController_A.injectProperties) | |
binder | |
.bindPropertyInjectionOf(ViewController_B.self) | |
.to(injector: ViewController_B.injectProperties) | |
binder | |
.bindPropertyInjectionOf(ViewController_C.self) | |
.to(injector: ViewController_C.injectProperties) | |
// One can also use a closure to simplify property injection. This is an example | |
binder | |
.bindPropertyInjectionOf(ViewController_D.self) | |
.to { | |
$0.serviceD = $1 | |
} | |
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) | |
} | |
private func makeRootViewController( | |
storyboard: UIStoryboard, | |
propertyInjector: PropertyInjector<ViewController_A> | |
) -> ViewController_A { | |
let vc = storyboard.instantiateInitialViewController() as! ViewController_A | |
propertyInjector.injectProperties(into: vc) | |
return vc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment