-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathLocationCellFactory.swift
51 lines (42 loc) · 1.4 KB
/
LocationCellFactory.swift
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
//
// LocationCellFactory.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-03-17.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadTypes
import UIKit
protocol LocationCellEventHandler {
func toggleCell(for item: LocationCellViewModel)
}
final class LocationCellFactory: CellFactoryProtocol {
var delegate: LocationCellEventHandler?
let tableView: UITableView
let reuseIdentifier: String
init(
tableView: UITableView,
reuseIdentifier: String
) {
self.tableView = tableView
self.reuseIdentifier = reuseIdentifier
}
func makeCell(for item: LocationCellViewModel, indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: reuseIdentifier,
for: indexPath
)
configureCell(cell, item: item, indexPath: indexPath)
return cell
}
func configureCell(_ cell: UITableViewCell, item: LocationCellViewModel, indexPath: IndexPath) {
guard let cell = cell as? LocationCell else { return }
cell.accessibilityIdentifier = item.node.code
cell.locationLabel.text = item.node.name
cell.showsCollapseControl = !item.node.children.isEmpty
cell.isExpanded = item.node.showsChildren
cell.didCollapseHandler = { [weak self] _ in
self?.delegate?.toggleCell(for: item)
}
}
}