Created
March 13, 2019 10:18
-
-
Save IsaAliev/8534cdf862067da5859e2563486be661 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 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