-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathBlockListViewController.swift
638 lines (542 loc) · 25.6 KB
/
BlockListViewController.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//
// LockdownViewController.swift
// Lockdown
//
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
import UIKit
import Foundation
import CocoaLumberjackSwift
final class BlockListViewController: BaseViewController {
// MARK: - Properties
var didMakeChange = false
var chosenBlocking = 0
var lockdownBlockLists: [LockdownGroup] = []
var customBlockedDomains: [(String, Bool)] = []
var customBlockedLists: [UserBlockListsGroup] = []
let curatedBlockedDomainsTableView = StaticTableView()
let customBlockedListsTableView = StaticTableView()
let customBlockedDomainsTableView = StaticTableView()
private lazy var listsSubmenuView: ListsSubmenuView = {
let view = ListsSubmenuView()
view.topButton.addTarget(self, action: #selector(addList), for: .touchUpInside)
view.bottomButton.addTarget(self, action: #selector(importBlockList), for: .touchUpInside)
view.isHidden = true
return view
}()
private lazy var customNavigationView: CustomNavigationView = {
let view = CustomNavigationView()
view.title = NSLocalizedString("Configure Blocking", comment: "")
view.buttonTitle = NSLocalizedString("CLOSE", comment: "")
view.onButtonPressed { [unowned self] in
self.close()
}
return view
}()
enum Page: CaseIterable {
case curated
case custom
var localizedTitle: String {
switch self {
case .curated:
return NSLocalizedString("Curated", comment: "")
case .custom:
return NSLocalizedString("Custom", comment: "")
}
}
}
private lazy var segmented: UISegmentedControl = {
let view = UISegmentedControl(items: Page.allCases.map(\.localizedTitle))
view.selectedSegmentIndex = chosenBlocking
view.setTitleTextAttributes([.font: fontMedium14], for: .normal)
view.selectedSegmentTintColor = .tunnelsBlue
view.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
view.addTarget(self, action: #selector(segmentedControlDidChangeValue), for: .valueChanged)
return view
}()
private let paragraphLabel: UILabel = {
let view = UILabel()
view.font = fontRegular14
view.numberOfLines = 0
view.text = NSLocalizedString("Block all your apps from connecting to the domains and sites below. For your convenience, Lockdown also has pre-configured suggestions.", comment: "")
return view
}()
private lazy var addNewListButton: UIButton = {
let button = UIButton(type: .system)
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .bold, scale: .large)
button.tintColor = .tunnelsBlue
button.setImage(UIImage(systemName: "plus", withConfiguration: symbolConfig), for: .normal)
button.addTarget(self, action: #selector(showSubmenu), for: .touchUpInside)
button.isEnabled = false
return button
}()
private lazy var listsLabel: UILabel = {
let label = UILabel()
label.text = NSLocalizedString("Lists", comment: "")
label.textColor = .label
label.font = fontBold18
return label
}()
private lazy var emptyListsView: EmptyListsView = {
let view = EmptyListsView()
view.descriptionLabel.text = NSLocalizedString("No lists yet", comment: "")
view.addButton.setTitle(NSLocalizedString("Create a list", comment: ""), for: .normal)
view.addButton.addTarget(self, action: #selector(addList), for: .touchUpInside)
return view
}()
private lazy var lockedListsView: LockedListsView = {
let view = LockedListsView()
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 addNewDomainButton: UIButton = {
let button = UIButton(type: .system)
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .bold, scale: .large)
button.tintColor = .tunnelsBlue
button.setImage(UIImage(systemName: "plus", withConfiguration: symbolConfig), for: .normal)
button.addTarget(self, action: #selector(addDomain), for: .touchUpInside)
return button
}()
private lazy var editDomainButton: UIButton = {
let button = UIButton(type: .system)
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .bold, scale: .large)
button.tintColor = .tunnelsBlue
button.setImage(UIImage(named: "icn_edit"), for: .normal)
button.addTarget(self, action: #selector(editDomains), for: .touchUpInside)
// button.isHidden = true
return button
}()
private lazy var emptyDomainsView: EmptyListsView = {
let view = EmptyListsView()
view.descriptionLabel.text = NSLocalizedString("No custom domains yet", comment: "")
view.addButton.setTitle(NSLocalizedString("Add a domain", comment: ""), for: .normal)
view.addButton.addTarget(self, action: #selector(addDomain), for: .touchUpInside)
return view
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondarySystemBackground
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissView))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
configure()
configureCuratedBlockedDomainsTableView()
configureCustomBlockedListsTableView()
configureCustomBlockedDomainsTableView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadCustomBlockedLists()
}
private func configure() {
view.addSubview(customNavigationView)
customNavigationView.anchors.leading.pin()
customNavigationView.anchors.trailing.pin()
customNavigationView.anchors.top.safeAreaPin()
view.addSubview(paragraphLabel)
paragraphLabel.anchors.top.spacing(0, to: customNavigationView.anchors.bottom)
paragraphLabel.anchors.leading.readableContentPin(inset: 3)
paragraphLabel.anchors.trailing.readableContentPin(inset: 3)
paragraphLabel.anchors.height.equal(60)
view.addSubview(segmented)
segmented.anchors.top.spacing(12, to: paragraphLabel.anchors.bottom)
segmented.anchors.leading.readableContentPin()
segmented.anchors.trailing.readableContentPin()
segmented.anchors.height.equal(40)
}
private func configureCuratedBlockedDomainsTableView() {
addTableView(curatedBlockedDomainsTableView, layout: { tableView in
tableView.anchors.top.spacing(8, to: segmented.anchors.bottom)
tableView.anchors.leading.pin()
tableView.anchors.trailing.pin()
tableView.anchors.bottom.pin()
})
reloadCuratedBlockDomains()
chosenBlocking == 0 ? transition(toPage: .curated) : transition(toPage: .custom)
}
private func configureCustomBlockedListsTableView() {
view.addSubview(listsLabel)
listsLabel.anchors.top.spacing(24, to: segmented.anchors.bottom)
listsLabel.anchors.leading.marginsPin()
view.addSubview(addNewListButton)
addNewListButton.anchors.centerY.equal(listsLabel.anchors.centerY)
addNewListButton.anchors.trailing.marginsPin()
view.addSubview(domainsLabel)
domainsLabel.anchors.top.spacing(300, to: segmented.anchors.bottom)
domainsLabel.anchors.height.equal(30)
domainsLabel.anchors.leading.marginsPin()
addTableView(customBlockedListsTableView, layout: { tableView in
tableView.anchors.top.spacing(8, to: listsLabel.anchors.bottom)
tableView.anchors.leading.pin()
tableView.anchors.trailing.pin()
tableView.anchors.bottom.spacing(8, to: domainsLabel.anchors.top)
})
view.addSubview(listsSubmenuView)
listsSubmenuView.anchors.trailing.marginsPin()
listsSubmenuView.anchors.top.spacing(60, to: paragraphLabel.anchors.bottom)
customBlockedListsTableView.deselectsCellsAutomatically = true
}
private func configureCustomBlockedDomainsTableView() {
view.addSubview(addNewDomainButton)
addNewDomainButton.anchors.centerY.equal(domainsLabel.anchors.centerY)
addNewDomainButton.anchors.trailing.marginsPin()
// view.addSubview(editDomainButton)
// editDomainButton.anchors.centerY.equal(domainsLabel.anchors.centerY)
// editDomainButton.anchors.trailing.spacing(16, to: addNewDomainButton.anchors.leading)
addTableView(customBlockedDomainsTableView, layout: { tableView in
tableView.anchors.top.spacing(8, to: domainsLabel.anchors.bottom)
tableView.anchors.leading.pin()
tableView.anchors.trailing.pin()
tableView.anchors.bottom.safeAreaPin()
})
customBlockedDomainsTableView.deselectsCellsAutomatically = true
reloadCustomBlockedDomains()
}
// Curated lists
func reloadCuratedBlockDomains() {
curatedBlockedDomainsTableView.clear()
lockdownBlockLists = {
let domains = getLockdownBlockedDomains().lockdownDefaults
let sorted = domains.sorted(by: { $0.key < $1.key })
return Array(sorted.map(\.value))
}()
createCuratedBlockedDomainsRows()
curatedBlockedDomainsTableView.reloadData()
}
func reloadCustomBlockedLists() {
customBlockedListsTableView.clear()
customBlockedLists = {
let lists = getBlockedLists().userBlockListsDefaults
let sorted = lists.sorted(by: { $0.key < $1.key })
return Array(sorted.map(\.value))
}()
createCustomBlockedListsRows()
customBlockedListsTableView.reloadData()
}
func reloadCustomBlockedDomains() {
customBlockedDomainsTableView.clear()
customBlockedDomains = {
let domains = getUserBlockedDomains()
return domains.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)
}
}
}()
createCustomBlockedDomainsRows()
customBlockedDomainsTableView.reloadData()
}
// Curated Lists
func createCuratedBlockedDomainsRows() {
let tableView = curatedBlockedDomainsTableView
for lockdownGroup in lockdownBlockLists {
let cell = tableView.addRow { [unowned self] (contentView) in
let blockListView = BlockListView()
blockListView.contents = .lockdownGroup(lockdownGroup)
contentView.addSubview(blockListView)
blockListView.anchors.edges.pin()
}.onSelect { [unowned self] in
if UserDefaults.hasSeenAdvancedPaywall || UserDefaults.hasSeenAnonymousPaywall || UserDefaults.hasSeenUniversalPaywall {
let storyboard = UIStoryboard.main
let target = storyboard.instantiate(BlockListGroupViewController.self)
target.lockdownGroup = lockdownGroup
// target.blockListVC = self
self.navigationController?.pushViewController(target, animated: true)
} else {
if lockdownGroup.accessLevel == "advanced" {
let vc = VPNPaywallViewController()
present(vc, animated: true)
} else {
let storyboard = UIStoryboard.main
let target = storyboard.instantiate(BlockListGroupViewController.self)
target.lockdownGroup = lockdownGroup
// target.blockListVC = self
self.navigationController?.pushViewController(target, animated: true)
}
}
}
cell.accessoryType = .disclosureIndicator
}
}
@objc
func segmentedControlDidChangeValue() {
let page = Page.allCases[segmented.selectedSegmentIndex]
transition(toPage: page)
}
func transition(toPage page: Page) {
switch page {
case .curated:
customBlockedDomainsTableView.isHidden = true
customBlockedListsTableView.isHidden = true
curatedBlockedDomainsTableView.isHidden = false
listsLabel.isHidden = true
addNewListButton.isHidden = true
listsSubmenuView.isHidden = true
addNewDomainButton.isHidden = true
domainsLabel.isHidden = true
editDomainButton.isHidden = true
case .custom:
customBlockedListsTableView.isHidden = false
customBlockedDomainsTableView.isHidden = false
curatedBlockedDomainsTableView.isHidden = true
listsLabel.isHidden = false
addNewListButton.isHidden = false
addNewDomainButton.isHidden = false
domainsLabel.isHidden = false
editDomainButton.isHidden = false
}
}
func saveNewList(userEnteredListName: String) {
didMakeChange = true
DDLogInfo("Adding custom list - \(userEnteredListName)")
addBlockedList(listName: userEnteredListName)
reloadCustomBlockedLists()
}
func saveNewDomain(userEnteredDomainName: String) {
let validation = DomainNameValidator.validate(userEnteredDomainName)
switch validation {
case .valid:
didMakeChange = true
DDLogInfo("Adding custom domain - \(userEnteredDomainName)")
addUserBlockedDomain(domain: userEnteredDomainName.lowercased())
reloadCustomBlockedDomains()
case .notValid(let reason):
DDLogWarn("Custom domain is not valid - \(userEnteredDomainName), reason - \(reason)")
showPopupDialog(
title: NSLocalizedString("Invalid domain", comment: ""),
message: "\"\(userEnteredDomainName)\"" + NSLocalizedString(" is not a valid entry. Please only enter the host of the domain you want to block. For example, \"google.com\" without \"https://\"", comment: ""),
acceptButton: NSLocalizedString("Okay", comment: "")
)
}
}
}
// MARK: - Functions
extension BlockListViewController {
func createCustomBlockedListsRows() {
let tableView = customBlockedListsTableView
let emptyList = emptyListsView
let lockedList = lockedListsView
if UserDefaults.hasSeenAdvancedPaywall || UserDefaults.hasSeenAnonymousPaywall || UserDefaults.hasSeenUniversalPaywall {
addNewListButton.isEnabled = true
if customBlockedLists.count == 0 {
tableView.addRow { [unowned self] (contentView) in
contentView.addSubview(emptyList)
emptyListsView.anchors.edges.pin()
}.onSelect { [unowned self] in
self.addList()
}
}
} else {
tableView.addRow { [unowned self] (contentView) in
contentView.addSubview(lockedList)
lockedList.anchors.edges.pin()
}.onSelect { [unowned self] in
let vc = VPNPaywallViewController()
self.present(vc, animated: true)
}
}
for list in customBlockedLists {
let blockListView = BlockListView()
blockListView.contents = .listsBlocked(list)
let cell = tableView.addRow { [unowned self] (contentView) in
contentView.addSubview(blockListView)
blockListView.anchors.edges.pin()
}.onSelect { [unowned self] in
self.didMakeChange = true
let vc = ListSettingsViewController()
vc.listName = list.name
vc.didMakeChange = list.enabled
vc.blockListVC = self
navigationController?.pushViewController(vc, animated: true)
}.onSwipeToDelete { [unowned self] in
self.didMakeChange = true
deleteList(list: list.name)
DDLogInfo("Deleting custom list - \(list.name)")
}
cell.accessoryType = .disclosureIndicator
}
}
// Custom Domains
func createCustomBlockedDomainsRows() {
let tableView = customBlockedDomainsTableView
let emptyDomains = emptyDomainsView
if customBlockedDomains.count == 0 {
tableView.addRow { [unowned self] (contentView) in
contentView.addSubview(emptyDomains)
emptyDomains.anchors.edges.pin()
}.onSelect { [unowned self] in
self.addDomain()
}
}
for (domain, isEnabled) in customBlockedDomains {
var currentEnabledStatus = isEnabled
let blockListView = BlockListView()
blockListView.contents = .userBlocked(domain: domain, isEnabled: isEnabled)
tableView.addRow { [unowned self] (contentView) in
contentView.addSubview(blockListView)
blockListView.anchors.edges.pin()
view.addSubview(editDomainButton)
editDomainButton.anchors.centerY.equal(domainsLabel.anchors.centerY)
editDomainButton.anchors.trailing.spacing(16, to: addNewDomainButton.anchors.leading)
}.onSelect { [unowned blockListView, unowned self] in
self.didMakeChange = true
currentEnabledStatus.toggle()
blockListView.contents = .userBlocked(domain: domain, isEnabled: currentEnabledStatus)
setUserBlockedDomain(domain: domain, enabled: currentEnabledStatus)
}.onSwipeToDelete { [unowned self] in
self.didMakeChange = true
deleteUserBlockedDomain(domain: domain)
DDLogInfo("Deleting custom domain - \(domain)")
}
}
}
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 addList() {
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()
self.listsSubmenuView.isHidden = true
}
}
saveAction.isEnabled = false
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { [weak self] (_) in
guard let self else { return }
self.listsSubmenuView.isHidden = true
}
alertController.addTextField { (textField) in
textField.placeholder = NSLocalizedString("List Name", comment: "")
}
NotificationCenter.default.addObserver(
forName: UITextField.textDidChangeNotification,
object: alertController.textFields?.first,
queue: .main) { (notification) -> Void in
guard let textFieldText = alertController.textFields?.first?.text else { return }
saveAction.isEnabled = textFieldText.isValid(.listName)
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func deleteList(list: String) {
let alert = UIAlertController(title: NSLocalizedString("Delete List?", comment: ""),
message: NSLocalizedString("Are you sure you want to remove this list?", comment: ""),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("No, Return", comment: ""),
style: .default,
handler: { [weak self] (_) in
guard let self else { return }
self.reloadCustomBlockedLists()
}))
alert.addAction(UIAlertAction(title: NSLocalizedString("Yes, Delete", comment: ""),
style: .destructive,
handler: { [weak self] (_) in
guard let self else { return }
deleteBlockedList(listName: list)
self.customBlockedListsTableView.clear()
self.reloadCustomBlockedLists()
}))
present(alert, animated: true, completion: nil)
}
@objc func showSubmenu() {
listsSubmenuView.isHidden = false
}
@objc func dismissView() {
listsSubmenuView.isHidden = true
}
@objc func importBlockList() {
listsSubmenuView.isHidden = true
let vc = ImportBlockListViewController()
vc.importCompletion = { [unowned self] in
self.reloadCustomBlockedLists()
self.showSuccessImportAlert()
}
navigationController?.present(vc, animated: true)
}
@objc func addDomain() {
let tableView = customBlockedDomainsTableView
let alertController = UIAlertController(title: NSLocalizedString("Add a Domain to Block", comment: ""),
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.saveNewDomain(userEnteredDomainName: text)
if !getUserBlockedDomains().isEmpty {
tableView.clear()
}
self.reloadCustomBlockedDomains()
// self.editDomainButton.isHidden = false
}
}
saveAction.isEnabled = false
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.keyboardType = .URL
textField.placeholder = "domain-to-block"
}
NotificationCenter.default.addObserver(
forName: UITextField.textDidChangeNotification,
object: alertController.textFields?.first,
queue: .main) { (notification) -> Void in
guard let textFieldText = alertController.textFields?.first?.text else { return }
saveAction.isEnabled = textFieldText.isValid(.domainName) && !textFieldText.isEmpty
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
@objc func editDomains() {
if !customBlockedDomains.isEmpty {
let vc = EditDomainsViewController()
vc.updateCompletion = { [weak self] in
self?.reloadCustomBlockedDomains()
}
navigationController?.pushViewController(vc, animated: true)
}
}
func showSuccessImportAlert() {
let alert = UIAlertController(title: NSLocalizedString("Success!", comment: ""),
message: NSLocalizedString("The list has been imported successfully. You can start blocking the list's domains", comment: ""),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""),
style: .default,
handler: nil))
present(alert, animated: true, completion: nil)
}
func showErrorAlert() {
let alert = UIAlertController(title: NSLocalizedString("Error", comment: ""),
message: NSLocalizedString("Unable to import the list. Please try again or contact support for assistance", comment: ""),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""),
style: .default,
handler: nil))
present(alert, animated: true, completion: nil)
}
}