Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import UIKit
import Shared
import Combine
import Models
import DifferenceKit
final class ContactListTableController: UITableViewController {
private var contacts = [Contact]()
private let viewModel: ContactListViewModel
private var cancellables = Set<AnyCancellable>()
private let tapRelay = PassthroughSubject<Contact, Never>()
var didTap: AnyPublisher<Contact, Never> { tapRelay.eraseToAnyPublisher() }
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
init(_ viewModel: ContactListViewModel) {
self.viewModel = viewModel
super.init(style: .grouped)
}
required init?(coder: NSCoder) { nil }
func filter(_ text: String) {
viewModel.filter(text)
}
private func setupTableView() {
tableView.separatorStyle = .none
tableView.register(SmallAvatarAndTitleCell.self)
tableView.backgroundColor = Asset.neutralWhite.color
tableView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
viewModel
.contacts
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
guard !self.contacts.isEmpty else {
self.contacts = $0
tableView.reloadData()
return
}
self.tableView.reload(
using: StagedChangeset(source: self.contacts, target: $0),
deleteSectionsAnimation: .none,
insertSectionsAnimation: .none,
reloadSectionsAnimation: .none,
deleteRowsAnimation: .none,
insertRowsAnimation: .none,
reloadRowsAnimation: .none
) { [unowned self] in
self.contacts = $0
}
}.store(in: &cancellables)
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: SmallAvatarAndTitleCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
cell.title.text = contacts[indexPath.row].nickname ?? contacts[indexPath.row].username
cell.avatar.set(
cornerRadius: 10,
username: contacts[indexPath.row].nickname ?? contacts[indexPath.row].username,
image: contacts[indexPath.row].photo
)
return cell
}
override func tableView(_: UITableView, numberOfRowsInSection: Int) -> Int { contacts.count }
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
tapRelay.send(contacts[indexPath.row])
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
64
}
}