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
| protocol ViewModelBased: class { | |
| associatedtype ViewModelType: ViewModel | |
| var viewModel: ViewModelType { get set } | |
| } |
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
| extension ViewModelBased where Self: StoryboardBased & UIViewController { | |
| static func instantiate<ServicesT> (withServices services: ServicesT) -> Self | |
| where ServicesT == Self.ViewModelType.Services { | |
| let viewController = Self.instantiate() | |
| viewController.viewModel = ViewModelType(withServices: services) | |
| return viewController | |
| } | |
| } |
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
| class MyService { | |
| func executeService() { | |
| print ("Service execution") | |
| } | |
| } |
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
| struct MyViewModel: ViewModel { | |
| typealias Services = MyService | |
| init(withServices services: Services) { | |
| services.executeService() | |
| } | |
| } |
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
| let myViewController = MyViewController.instantiate(withServices: myService) | |
| // we can access the inner ViewModel if needed: myViewController.viewModel |
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
| class Service1 { | |
| func executeService1() { | |
| print ("execution of Service1") | |
| } | |
| } | |
| class Service2 { | |
| func executeService2() { | |
| print ("execution of Service2") | |
| } |
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
| protocol HasService1 { | |
| var service1: Service1 { get } | |
| } | |
| protocol HasService2 { | |
| var service2: Service2 { get } | |
| } | |
| protocol HasService3 { | |
| var service3: Service3 { get } |
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
| struct MyViewModel: ViewModel { | |
| // thanks to protocol composition we define only the services we want to use | |
| typealias Services = HasService1 & HasService2 | |
| init(withServices services: Services) { | |
| services.service1.executeService1() | |
| services.service2.executeService2() | |
| } | |
| } | |
| struct MyOtherViewModel: ViewModel { |
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
| class MyServices: HasService1, HasService2, HasService3 { | |
| let service1 = Service1() | |
| let service2 = Service2() | |
| let service3 = Service3() | |
| } |
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
| let myViewController = MyViewController.instantiate(withServices: myServices) | |
| let myViewController2 = MyViewController2.instantiate(withServices: myServices) | |
| // This is the same myServices instance for the 2 ViewControllers | |
| // but each ViewModel will only access what's needed |