Created
May 30, 2018 09:51
-
-
Save bobgodwinx/69c323e2628c42b8ac876c78a84c1ced to your computer and use it in GitHub Desktop.
S4D06
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 | |
| /// let groupedByNationality = Dictionary(grouping: items) { $0.nationality } | |
| /// For this example we are going to use the `LastName` | |
| /// `grouped = [Character: [Person]]` ///new feature in swift 4.x | |
| let grouped = Dictionary(grouping: items) { $0.lastName.first! } | |
| let keys = grouped.keys.sorted() | |
| return keys.map { key -> TableDatasource.Section in | |
| let rows = grouped[key]?.compactMap(PersonRow.init) ?? [] | |
| return TableDatasource.Section(title: String(key), rows: rows) | |
| } | |
| } | |
| .asDriver(onErrorJustReturn: []) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment