-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathAddLocationsViewController.swift
96 lines (83 loc) · 2.62 KB
/
AddLocationsViewController.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
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
85
86
87
88
89
90
91
92
93
94
95
96
//
// AddLocationsViewController.swift
// MullvadVPN
//
// Created by Mojgan on 2024-02-29.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import MullvadTypes
import UIKit
protocol AddLocationsViewControllerDelegate: AnyObject {
func didUpdateSelectedLocations(locations: [RelayLocation])
func didBack()
}
class AddLocationsViewController: UIViewController {
private var dataSource: AddLocationsDataSource?
private let nodes: [LocationNode]
private let customList: CustomList
weak var delegate: (any AddLocationsViewControllerDelegate)?
private let tableView: UITableView = {
let tableView = UITableView()
tableView.separatorColor = .secondaryColor
tableView.separatorInset = .zero
tableView.rowHeight = 56
tableView.indicatorStyle = .white
tableView.accessibilityIdentifier = .addLocationsView
return tableView
}()
init(
allLocationsNodes: [LocationNode],
customList: CustomList
) {
self.nodes = allLocationsNodes
self.customList = customList
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = view.backgroundColor
view.backgroundColor = .secondaryColor
addConstraints()
setUpDataSource()
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
delegate?.didBack()
}
}
private func addConstraints() {
view.addConstrainedSubviews([tableView]) {
tableView.pinEdgesToSuperview()
}
}
private func setUpDataSource() {
dataSource = AddLocationsDataSource(
tableView: tableView,
allLocationNodes: nodes.copy(),
customList: customList
)
dataSource?.didUpdateCustomList = { [weak self] customListLocationNode in
guard let self else { return }
delegate?.didUpdateSelectedLocations(
locations: customListLocationNode.children.reduce([]) { partialResult, locationNode in
partialResult + locationNode.locations
}
)
}
}
}
fileprivate extension [LocationNode] {
func copy() -> Self {
map {
let copy = $0.copy()
copy.showsChildren = false
copy.flattened.forEach { $0.showsChildren = false }
return copy
}
}
}