Skip to content

Instantly share code, notes, and snippets.

@bobgodwinx
Created May 30, 2018 09:51
Show Gist options
  • Select an option

  • Save bobgodwinx/69c323e2628c42b8ac876c78a84c1ced to your computer and use it in GitHub Desktop.

Select an option

Save bobgodwinx/69c323e2628c42b8ac876c78a84c1ced to your computer and use it in GitHub Desktop.
S4D06
/// `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