-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathEditDomainsViewController.swift
241 lines (195 loc) · 8.38 KB
/
EditDomainsViewController.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// EditDomainsViewController.swift
// LockdownSandbox
//
// Created by Aliaksandr Dvoineu on 4.04.23.
//
import UIKit
import SwiftCSV
final class EditDomainsViewController: UIViewController {
// MARK: - Properties
var updateCompletion: (() -> ())?
private var didMakeChange = false
var customBlockedDomains: [(String, Bool)] = []
var selectedDomains: Dictionary<String, Bool> = [:] {
didSet {
if selectedDomains.filter({ $0.value == true }).count == 0 {
bottomMenu.middleButton.isEnabled = false
bottomMenu.rightButton.isEnabled = false
} else {
bottomMenu.middleButton.isEnabled = true
bottomMenu.rightButton.isEnabled = true
}
}
}
private var titleName = NSLocalizedString("Edit Domains", comment: "")
private lazy var navigationView: ConfiguredNavigationView = {
let view = ConfiguredNavigationView()
view.titleLabel.text = NSLocalizedString(titleName, comment: "")
view.leftNavButton.setTitle(NSLocalizedString("CLOSE", comment: ""), for: .normal)
view.leftNavButton.addTarget(self, action: #selector(closeButtonClicked), for: .touchUpInside)
return view
}()
private lazy var domainsLabel: UILabel = {
let label = UILabel()
label.text = NSLocalizedString("Domains", comment: "")
label.textColor = .label
label.font = fontBold18
return label
}()
private lazy var bottomMenu: BottomMenu = {
let view = BottomMenu()
view.leftButton.addTarget(self, action: #selector(selectAllddDomains), for: .touchUpInside)
view.middleButton.addTarget(self, action: #selector(moveToList), for: .touchUpInside)
view.middleButton.isHidden = true
view.middleButton.isEnabled = false
view.rightButton.addTarget(self, action: #selector(deleteDomains), for: .touchUpInside)
view.rightButton.isEnabled = false
return view
}()
private let customBlockedDomainsTableView = CustomTableView()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondarySystemBackground
if UserDefaults.hasSeenAdvancedPaywall || UserDefaults.hasSeenAnonymousPaywall || UserDefaults.hasSeenUniversalPaywall {
bottomMenu.middleButton.isHidden = false }
configureDomainsTableView()
configureUI()
}
// MARK: - Configure UI
private func configureUI() {
view.addSubview(bottomMenu)
bottomMenu.anchors.bottom.pin()
bottomMenu.anchors.height.equal(60)
bottomMenu.anchors.leading.pin()
bottomMenu.anchors.trailing.pin()
}
private func configureDomainsTableView() {
view.addSubview(navigationView)
navigationView.anchors.leading.pin()
navigationView.anchors.trailing.pin()
navigationView.anchors.top.safeAreaPin()
addTableView(customBlockedDomainsTableView) { tableView in
tableView.anchors.top.spacing(24, to: navigationView.anchors.bottom)
tableView.anchors.leading.pin()
tableView.anchors.trailing.pin()
tableView.anchors.bottom.pin(inset: 60)
}
reloadCustomBlockedDomains()
}
}
// MARK: - Functions
private extension EditDomainsViewController {
func reloadCustomBlockedDomains() {
customBlockedDomainsTableView.clear()
customBlockedDomains = {
let lists = getUserBlockedDomains()
return lists.sorted(by: { $0.key < $1.key }).map { (key, value) -> (String, Bool) in
if let status = value as? NSNumber {
return (key, status.boolValue)
} else {
return (key, false)
}
}
}()
createUserBlockedDomainsRows()
customBlockedDomainsTableView.reloadData()
updateLeftButton()
}
func createUserBlockedDomainsRows() {
let tableView = customBlockedDomainsTableView
tableView.separatorStyle = .singleLine
let tableTitle = domainsLabel
tableView.addHeader { view in
view.addSubview(tableTitle)
tableTitle.anchors.top.marginsPin()
tableTitle.anchors.leading.marginsPin()
tableTitle.anchors.bottom.marginsPin()
}
for (domain, isBlocked) in customBlockedDomains {
let blockListView = EditDomainsCell()
blockListView.contents = .userBlocked(
domain: domain,
isSelected: self.selectedDomains[domain] ?? false,
isBlocked: isBlocked
)
let cell = tableView.addRow { (contentView) in
contentView.addSubview(blockListView)
blockListView.anchors.edges.pin()
}.onSelect { [unowned blockListView, unowned self] in
self.didMakeChange = true
let isChecked = self.selectedDomains[domain] ?? false
blockListView.contents = .userBlocked(
domain: domain,
isSelected: !isChecked,
isBlocked: isBlocked
)
self.selectedDomains[domain] = !isChecked
self.updateLeftButton()
}
cell.accessoryType = .none
}
}
private func isAllDomainSelected() -> Bool {
guard !customBlockedDomains.isEmpty else { return false }
for (domain, _) in customBlockedDomains {
if (selectedDomains[domain] ?? false) == false {
return false
}
}
return true
}
@objc func closeButtonClicked() {
updateCompletion?()
navigationController?.popViewController(animated: true)
}
@objc func selectAllddDomains() {
let wasAllSelected = isAllDomainSelected()
for (domain, _) in customBlockedDomains {
selectedDomains[domain] = !wasAllSelected
}
reloadCustomBlockedDomains()
updateLeftButton()
}
@objc func moveToList() {
let sortedDomains = selectedDomains.filter({ $0.value == true })
let vc = MoveToListViewController()
vc.selectedDomains = sortedDomains
vc.moveToListCompletion = { [unowned self] in
self.refreshSelectedDomainsAndReloadCustomBlockedDomains()
}
present(vc, animated: true)
}
@objc func deleteDomains() {
let alert = UIAlertController(title: NSLocalizedString("Delete Entries?", comment: ""),
message: NSLocalizedString("Are you sure you want to remove these domains?", comment: ""),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("No, Return", comment: ""),
style: UIAlertAction.Style.default,
handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Yes, Delete", comment: ""),
style: UIAlertAction.Style.destructive,
handler: { [weak self] (_) in
guard let self else { return }
let sortedDomains = self.selectedDomains.filter({ $0.value == true })
for domain in sortedDomains.keys {
deleteUserBlockedDomain(domain: domain)
}
self.refreshSelectedDomainsAndReloadCustomBlockedDomains()
}))
self.present(alert, animated: true, completion: nil)
}
private func refreshSelectedDomainsAndReloadCustomBlockedDomains() {
let sortedDomains = self.selectedDomains.filter { $0.value == false }
self.selectedDomains = sortedDomains
self.reloadCustomBlockedDomains()
}
private func updateLeftButton() {
bottomMenu.leftButton.setTitle(
isAllDomainSelected() ? NSLocalizedString("Deselect All", comment: "") : NSLocalizedString("Select All", comment: ""),
for: .normal
)
bottomMenu.leftButton.isEnabled = !customBlockedDomains.isEmpty
}
}