-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathMoveToListViewController.swift
More file actions
277 lines (233 loc) · 10 KB
/
MoveToListViewController.swift
File metadata and controls
277 lines (233 loc) · 10 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// MoveToLsisViewController.swift
// LockdownSandbox
//
// Created by Aliaksandr Dvoineu on 28.04.23.
//
import UIKit
import CocoaLumberjackSwift
final class MoveToListViewController: UIViewController {
// MARK: - Properties
var moveToListCompletion: (() -> ())?
private var didMakeChange = false
var successMessage = ""
var selectedDomains: Dictionary<String, Bool> = [:] {
didSet {
if selectedDomains.count == 1 {
numberOfdomains.text = "\(selectedDomains.count) " + NSLocalizedString("domain", comment: "")
successMessage = "\(selectedDomains.count) domain has been moved to list successfully."
} else {
numberOfdomains.text = "\(selectedDomains.count) " + NSLocalizedString("domains", comment: "")
successMessage = "\(selectedDomains.count) domains have been moved to list successfully."
}
domainsList.text = selectedDomains.map(\.0).joined(separator: ", ")
}
}
private var customBlockedLists: [UserBlockListsGroup] = []
private let customBlockedListsTableView = CustomTableView()
private lazy var descriptionText: UILabel = {
let label = UILabel()
label.text = NSLocalizedString("Move selected Domains to an existing or a new list", comment: "")
label.textColor = .label
label.font = fontRegular14
label.textColor = .gray
label.numberOfLines = 0
label.textAlignment = .center
return label
}()
private lazy var navigationView: ConfiguredNavigationView = {
let view = ConfiguredNavigationView()
view.titleLabel.text = NSLocalizedString("Move to list", comment: "")
view.leftNavButton.setTitle(NSLocalizedString("BACK", comment: ""), for: .normal)
view.leftNavButton.addTarget(self, action: #selector(backButtonClicked), for: .touchUpInside)
view.rightNavButton.setTitle(NSLocalizedString("CANCEL", comment: ""), for: .normal)
view.rightNavButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside)
return view
}()
private lazy var domainImage: UIImageView = {
let image = UIImageView()
image.image = UIImage(systemName: "globe")
image.contentMode = .scaleAspectFit
image.tintColor = .gray
return image
}()
private lazy var domainsList: UILabel = {
let label = UILabel()
label.textColor = .label
label.font = fontRegular14
label.textAlignment = .natural
label.numberOfLines = 0
return label
}()
private lazy var numberOfdomains: UILabel = {
let label = UILabel()
label.textColor = .label
label.font = fontBold13
label.textAlignment = .natural
return label
}()
private lazy var vstackView: UIStackView = {
let stackView = UIStackView()
stackView.addArrangedSubview(domainsList)
stackView.addArrangedSubview(numberOfdomains)
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.spacing = 3
return stackView
}()
private lazy var hstackView: UIStackView = {
let stackView = UIStackView()
stackView.addArrangedSubview(domainImage)
stackView.addArrangedSubview(vstackView)
stackView.axis = .horizontal
stackView.distribution = .fillProportionally
stackView.spacing = 8
stackView.alignment = .top
return stackView
}()
private lazy var addNewListButton: UIButton = {
let button = UIButton(type: .system)
button.tintColor = .tunnelsBlue
button.setTitle(NSLocalizedString("Add a new List", comment: ""), for: .normal)
button.setImage(UIImage(systemName: "plus.circle.fill"), for: .normal)
button.setTitleColor(.label, for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
button.addTarget(self, action: #selector(addNewList), for: .touchUpInside)
return button
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondarySystemBackground
configureUI()
configureListsTableView()
}
// MARK: - Configure UI
private func configureUI() {
view.addSubview(descriptionText)
descriptionText.anchors.leading.readableContentPin(inset: 12)
descriptionText.anchors.trailing.readableContentPin(inset: 12)
descriptionText.anchors.top.safeAreaPin()
view.addSubview(navigationView)
navigationView.anchors.top.spacing(8, to: descriptionText.anchors.bottom)
navigationView.anchors.leading.pin()
navigationView.anchors.trailing.pin()
view.addSubview(hstackView)
hstackView.anchors.top.spacing(12, to: navigationView.anchors.bottom)
hstackView.anchors.leading.marginsPin()
hstackView.anchors.trailing.marginsPin()
}
private func configureListsTableView() {
addTableView(customBlockedListsTableView) { tableView in
tableView.anchors.top.spacing(16, to: hstackView.anchors.bottom)
tableView.anchors.leading.pin()
tableView.anchors.trailing.pin()
}
reloadCustomBlockedLists()
}
}
// MARK: - Private functions
private extension MoveToListViewController {
func reloadCustomBlockedLists() {
customBlockedListsTableView.clear()
customBlockedLists = {
let lists = getBlockedLists().userBlockListsDefaults
let sorted = lists.sorted(by: { $0.key < $1.key })
return Array(sorted.map(\.value))
}()
createUserBlockedListsRows()
customBlockedListsTableView.reloadData()
}
func createUserBlockedListsRows() {
let userBlockedLists = getBlockedLists().userBlockListsDefaults
let tableView = customBlockedListsTableView
tableView.separatorStyle = .singleLine
let plusButton = UIButton(type: .system)
plusButton.setImage(UIImage(systemName: "plus"), for: .normal)
plusButton.tintColor = .tunnelsBlue
tableView.addHeader { view in
plusButton.addTarget(self, action: #selector(addNewList), for: .touchUpInside)
view.addSubview(plusButton)
plusButton.anchors.top.marginsPin()
plusButton.anchors.trailing.marginsPin()
plusButton.anchors.bottom.marginsPin()
}
for list in customBlockedLists {
let blockListView = BlockListView()
blockListView.contents = .listsBlocked(list)
let cell = tableView.addRow { (contentView) in
contentView.addSubview(blockListView)
blockListView.anchors.edges.pin()
}.onSelect { [unowned self] in
self.didMakeChange = true
let blockedList = userBlockedLists[list.name]
if let blockedList = blockedList {
for domain in self.selectedDomains.keys {
addDomainToBlockedList(domain: domain, for: blockedList.name)
}
}
moveToList()
}
cell.accessoryType = .none
}
}
func saveNewList(userEnteredListName: String) {
DDLogInfo("Adding custom list - \(userEnteredListName)")
addBlockedList(listName: userEnteredListName)
reloadCustomBlockedLists()
}
func close() {
dismiss(animated: true, completion: { [weak self] in
guard let self else { return }
if (self.didMakeChange == true) {
if getIsCombinedBlockListEmpty() {
FirewallController.shared.setEnabled(false, isUserExplicitToggle: true)
} else if (FirewallController.shared.status() == .connected) {
FirewallController.shared.restart()
}
}
})
}
@objc func backButtonClicked() {
dismiss(animated: true)
}
@objc func cancelButtonClicked() {
dismiss(animated: true)
}
@objc func addNewList() {
let tableView = customBlockedListsTableView
let alertController = UIAlertController(title: "Create New List", message: nil, preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { [weak self] (_) in
if let txtField = alertController.textFields?.first, let text = txtField.text {
guard let self else { return }
self.saveNewList(userEnteredListName: text)
// if !getBlockedLists().isEmpty {
// tableView.clear()
// }
self.reloadCustomBlockedLists()
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addTextField { (textField) in
textField.placeholder = NSLocalizedString("List Name", comment: "")
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
func moveToList() {
for domain in selectedDomains.keys {
deleteUserBlockedDomain(domain: domain)
}
moveToListCompletion?()
let alert = UIAlertController(title: NSLocalizedString("Success!", comment: ""),
message: NSLocalizedString("\(successMessage)", comment: ""),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""),
style: .default,
handler: { _ in
self.dismiss(animated: true)
}))
present(alert, animated: true, completion: nil)
}
}