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
| /// Now composing our `MVVM` | |
| /// just like a plug and play | |
| /// `Model` | |
| let provider = ContactProvider() | |
| /// `ViewModel` | |
| let viewModel = ContactViewModel(provider) | |
| /// `View` | |
| let viewController = ContactViewController(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
| /// `ContactViewController` = `View` | |
| class ContactViewController: UIViewController { | |
| private let bag = DisposeBag() | |
| let viewModel: Contactable | |
| /// `dependency inversion` | |
| init(_ viewModel: Contactable) { | |
| self.viewModel = viewModel | |
| super.init(nibName: nil, bundle: nil) | |
| configure() |
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
| /// `ContactViewModel` = `ViewModel` | |
| class ContactViewModel: Contactable { | |
| let datasource: Driver<[TableDatasource.Section]> | |
| /// `dependency inversion` | |
| init(_ provider: ContactProviderType) { | |
| self.datasource = provider | |
| .contacts | |
| .map { items -> [TableDatasource.Section] in | |
| /// groupedByNationality = [Nationality: [Person]]` ///new feature in swift 4.x |
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
| /// `TableDatasource` | |
| class TableDatasource: NSObject, UITableViewDataSource, RxTableViewDataSourceType, SectionedViewDataSourceType, UITableViewDelegate { | |
| struct Section { | |
| let title: String | |
| let rows: [TableRow] | |
| init(title: String, rows: [TableRow]) { | |
| self.title = title | |
| self.rows = rows | |
| } |
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
| /// `Contactable` = `ViewModel` requirement | |
| protocol Contactable { | |
| var datasource: Driver<[TableDatasource.Section]> {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
| /// `ContactProvider` = `Model` | |
| class ContactProvider: ContactProviderType { | |
| let contacts: Observable<Set<Person>> | |
| init() { | |
| /// Only Leader and the richest | |
| /// Disclaimer this data is just | |
| /// for example purposes. I am not | |
| /// responsible for any incorrect | |
| /// information. I hereby declare |
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
| /// `ContactProviderType` = `Model` requirement | |
| protocol ContactProviderType { | |
| /// Set that contains `Person` | |
| var contacts: Observable<Set<Person>> {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
| /// `Person` | |
| struct Person: Hashable { | |
| let firstName: String | |
| let lastName: String | |
| let age: Int | |
| let nationality: Nationality | |
| } |
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
| //MARK: Two-way Bindings | |
| extension ControlProperty { | |
| func twoWayBindTo(_ variable: Variable<E>) -> Disposable { | |
| let bindUI = variable.asObservable().bind(to: self) | |
| let bindVariable = subscribe( | |
| onNext: {value in | |
| variable.value = value | |
| }, onCompleted: { | |
| bindUI.dispose() | |
| }) |
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
| /// Usage of shadowed protocol styled type erasure | |
| let rows: [TableRow] = [ProductCell(name: "Hello"), ItemCell(id: "123456")] | |
| for row in rows { | |
| if let cell = row as? ProductCell { | |
| cell.configure(with: Product()) | |
| } | |
| if let cell = row as? ItemCell { | |
| cell.configure(with: Item()) |