Skip to content
Snippets Groups Projects
Commit 0b7f47e3 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Update collation in ContactListTableController

parent 32aacdad
No related branches found
No related tags found
1 merge request!93Fix connections list index
......@@ -5,11 +5,24 @@ import XXModels
import AppResources
final class ContactListTableController: UITableViewController {
private var collation = UILocalizedIndexedCollation.current()
private var sections: [[Contact]] = [] {
didSet { self.tableView.reloadData() }
private final class Row: NSObject {
init(contact: XXModels.Contact) {
self.contact = contact
self.title = contact.nickname ?? contact.username ?? ""
}
let contact: XXModels.Contact
@objc let title: String
}
private struct Section {
var title: String
var rows: [Row]
}
private var collation = UILocalizedIndexedCollation.current()
private var sections: [Section] = []
private let viewModel: ContactListViewModel
private var cancellables = Set<AnyCancellable>()
private let tapRelay = PassthroughSubject<Contact, Never>()
......@@ -37,18 +50,29 @@ final class ContactListTableController: UITableViewController {
viewModel.contacts
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
let results = IndexedListCollator().sectioned(items: $0)
self.collation = results.collation
self.sections = results.sections
.sink { [unowned self] contacts in
sections = makeSections(from: contacts)
tableView.reloadData()
}.store(in: &cancellables)
}
private func makeSections(from contacts: [XXModels.Contact]) -> [Section] {
let rows = contacts.map(Row.init(contact:))
let selector: Selector = #selector(getter: Row.title)
let sortedRows = collation.sortedArray(from: rows, collationStringSelector: selector) as! [Row]
var sections = collation.sectionTitles.map { Section(title: $0, rows: []) }
sortedRows.forEach { row in
let sectionNumber = collation.section(for: row, collationStringSelector: selector)
sections[sectionNumber].rows.append(row)
}
sections.removeAll(where: \.rows.isEmpty)
return sections
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: AvatarCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
let contact = sections[indexPath.section][indexPath.row]
let contact = sections[indexPath.section].rows[indexPath.row].contact
let name = (contact.nickname ?? contact.username) ?? "Fetching username..."
cell.setup(title: name, image: contact.photo)
return cell
}
......@@ -58,11 +82,12 @@ final class ContactListTableController: UITableViewController {
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
sections[section].count
sections[section].rows.count
}
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
tapRelay.send(sections[indexPath.section][indexPath.row])
let contact = sections[indexPath.section].rows[indexPath.row].contact
tapRelay.send(contact)
}
override func sectionIndexTitles(for: UITableView) -> [String]? {
......@@ -70,11 +95,15 @@ final class ContactListTableController: UITableViewController {
}
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
collation.sectionTitles[section]
sections[section].title
}
override func tableView(_: UITableView, sectionForSectionIndexTitle: String, at index: Int) -> Int {
collation.section(forSectionIndexTitle: index)
if let index = sections.lastIndex(where: { $0.title <= sectionForSectionIndexTitle }) {
return index
} else {
return sections.index(before: sections.endIndex)
}
}
override func tableView(_: UITableView, heightForRowAt: IndexPath) -> CGFloat {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment