Skip to content

Instantly share code, notes, and snippets.

@IsaAliev
Created March 13, 2019 10:18
Show Gist options
  • Save IsaAliev/8534cdf862067da5859e2563486be661 to your computer and use it in GitHub Desktop.
Save IsaAliev/8534cdf862067da5859e2563486be661 to your computer and use it in GitHub Desktop.
import Foundation
protocol CurrentStateObserver: class {
func didChangeStateTo(_ state: State)
}
fileprivate struct AssociatedKey {
static var stateController = "kStateController"
}
extension CurrentStateObserver where Self: StateDrivenViewModel {
var stateController: StateController {
get {
return objc_getAssociatedObject(self, &AssociatedKey.stateController) as! StateController
}
set {
objc_setAssociatedObject(self, &AssociatedKey.stateController, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func setupStateController<T: StateController>(_ type: T.Type) {
var controller = T()
controller.observer = self
stateController = controller
}
func didChangeStateTo(_ state: State) {
self.state.value = state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment