mirror of
https://github.com/confirmedcode/Lockdown-iOS.git
synced 2026-03-02 18:23:49 +01:00
261 lines
9.6 KiB
Swift
261 lines
9.6 KiB
Swift
//
|
|
// ListSettingsViewController.swift
|
|
// Lockdown
|
|
//
|
|
// Created by Aliaksandr Dvoineu on 28.03.23.
|
|
// Copyright © 2023 Confirmed Inc. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import CocoaLumberjackSwift
|
|
|
|
final class ListSettingsViewController: UIViewController {
|
|
|
|
// MARK: - Properties
|
|
|
|
var blockedList: UserBlockListsGroup?
|
|
|
|
weak var blockListVC: BlockListViewController?
|
|
|
|
var titleName = ""
|
|
|
|
var didMakeChange = false
|
|
|
|
lazy var navigationView: ConfiguredNavigationView = {
|
|
let view = ConfiguredNavigationView()
|
|
view.titleLabel.text = titleName
|
|
view.leftNavButton.setTitle(NSLocalizedString("BACK", comment: ""), for: .normal)
|
|
view.leftNavButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
|
|
view.leftNavButton.addTarget(self, action: #selector(backButtonClicked), for: .touchUpInside)
|
|
view.rightNavButton.setTitle("...", for: .normal)
|
|
view.rightNavButton.titleLabel?.font = fontBold18
|
|
view.rightNavButton.addTarget(self, action: #selector(showSubmenu), for: .touchUpInside)
|
|
return view
|
|
}()
|
|
|
|
private lazy var switchBlockingView: SwitchBlockingView = {
|
|
let view = SwitchBlockingView()
|
|
view.titleLabel.text = NSLocalizedString("Blocking", comment: "")
|
|
return view
|
|
}()
|
|
|
|
private lazy var subMenu: ListsSubmenuView = {
|
|
let view = ListsSubmenuView()
|
|
view.topButton.setTitle(NSLocalizedString("Export List...", comment: ""), for: .normal)
|
|
view.topButton.setImage(UIImage(named: "icn_export_folder"), for: .normal)
|
|
view.bottomButton.setTitle(NSLocalizedString("Delete List...", comment: ""), for: .normal)
|
|
view.bottomButton.setImage(UIImage(named: "icn_trash"), for: .normal)
|
|
view.bottomButton.addTarget(self, action: #selector(deleteList), for: .touchUpInside)
|
|
return view
|
|
}()
|
|
|
|
private let tableView = UITableView(frame: .zero, style: .insetGrouped)
|
|
|
|
// MARK: - Lifecycle
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
view.backgroundColor = .secondarySystemBackground
|
|
configureUI()
|
|
configureTableView()
|
|
}
|
|
|
|
// MARK: - Configure UI
|
|
func configureUI() {
|
|
view.addSubview(navigationView)
|
|
navigationView.anchors.leading.pin()
|
|
navigationView.anchors.trailing.pin()
|
|
navigationView.anchors.top.safeAreaPin()
|
|
|
|
view.addSubview(switchBlockingView)
|
|
switchBlockingView.anchors.top.spacing(12, to: navigationView.anchors.bottom)
|
|
switchBlockingView.anchors.leading.marginsPin()
|
|
switchBlockingView.anchors.trailing.marginsPin()
|
|
switchBlockingView.anchors.height.equal(40)
|
|
|
|
addTableView(tableView) { tableview in
|
|
tableView.anchors.top.spacing(20, to: switchBlockingView.anchors.bottom)
|
|
tableView.anchors.leading.pin()
|
|
tableView.anchors.trailing.pin()
|
|
tableView.anchors.bottom.pin()
|
|
}
|
|
|
|
view.addSubview(subMenu)
|
|
subMenu.anchors.top.spacing(0, to: navigationView.anchors.bottom)
|
|
subMenu.anchors.trailing.marginsPin()
|
|
subMenu.isHidden = true
|
|
|
|
// let tap = UITapGestureRecognizer(target: self, action: #selector(hideSubmenu))
|
|
// view.addGestureRecognizer(tap)
|
|
}
|
|
|
|
func configureTableView() {
|
|
|
|
tableView.delegate = self
|
|
tableView.dataSource = self
|
|
tableView.rowHeight = 40
|
|
|
|
tableView.register(ListBlockedTableViewCell.self, forCellReuseIdentifier: ListBlockedTableViewCell.identifier)
|
|
tableView.register(DomainsBlockedTableViewCell.self, forCellReuseIdentifier: DomainsBlockedTableViewCell.identifier)
|
|
}
|
|
}
|
|
|
|
extension ListSettingsViewController: UITableViewDataSource {
|
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
|
let view = UIView()
|
|
|
|
let sectionName = UILabel()
|
|
sectionName.font = fontBold13
|
|
|
|
view.addSubview(sectionName)
|
|
sectionName.anchors.top.marginsPin()
|
|
sectionName.anchors.leading.marginsPin()
|
|
sectionName.anchors.bottom.marginsPin()
|
|
|
|
switch section {
|
|
case 0:
|
|
sectionName.text = NSLocalizedString("NAME", comment: "")
|
|
case 1:
|
|
sectionName.text = NSLocalizedString("DESCRIPTION", comment: "")
|
|
case 2:
|
|
sectionName.text = NSLocalizedString("DOMAINS", comment: "")
|
|
let addButton = UIButton(type: .system)
|
|
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .bold, scale: .large)
|
|
addButton.setImage(UIImage(systemName: "plus", withConfiguration: symbolConfig), for: .normal)
|
|
addButton.tintColor = .tunnelsBlue
|
|
addButton.addTarget(self, action: #selector(addDomain), for: .touchUpInside)
|
|
|
|
view.addSubview(addButton)
|
|
addButton.anchors.top.marginsPin()
|
|
addButton.anchors.trailing.marginsPin()
|
|
addButton.anchors.bottom.marginsPin()
|
|
default: break
|
|
}
|
|
return view
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
40
|
|
}
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 3
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
switch section {
|
|
case 0, 1: return 1
|
|
case 2: return getUserBlockedDomains().count
|
|
default: return 0
|
|
}
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
return 50
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let domains = getUserBlockedDomains()
|
|
|
|
switch indexPath.section {
|
|
case 0:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ListBlockedTableViewCell.identifier, for: indexPath) as? ListBlockedTableViewCell else {
|
|
return UITableViewCell()
|
|
}
|
|
cell.label.text = title
|
|
return cell
|
|
case 1:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ListBlockedTableViewCell.identifier, for: indexPath) as? ListBlockedTableViewCell else {
|
|
return UITableViewCell()
|
|
}
|
|
cell.label.text = "Description"
|
|
return cell
|
|
case 2:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: DomainsBlockedTableViewCell.identifier, for: indexPath) as? DomainsBlockedTableViewCell else {
|
|
return UITableViewCell()
|
|
}
|
|
cell.label.text = "reroi.com"
|
|
return cell
|
|
default:
|
|
return UITableViewCell()
|
|
}
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
switch indexPath.section {
|
|
case 0:
|
|
let vc = ListDetailViewController()
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
case 1:
|
|
let vc = ListDescriptionViewController()
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ListSettingsViewController: UITableViewDelegate {
|
|
|
|
}
|
|
|
|
// MARK: - Functions
|
|
extension ListSettingsViewController {
|
|
|
|
@objc func backButtonClicked() {
|
|
navigationController?.popViewController(animated: true)
|
|
}
|
|
|
|
func saveNewDomain(userEnteredDomainName: String) {
|
|
didMakeChange = true
|
|
DDLogInfo("Adding custom domain - \(userEnteredDomainName)")
|
|
addUserBlockedDomain(domain: userEnteredDomainName.lowercased())
|
|
tableView.reloadData()
|
|
}
|
|
|
|
@objc func addDomain() {
|
|
let alertController = UIAlertController(title: "Add a Domain to Block", message: nil, preferredStyle: .alert)
|
|
let saveAction = UIAlertAction(title: "Save", style: .default) { [weak self] (_) in
|
|
guard let self else { return }
|
|
if let txtField = alertController.textFields?.first, let text = txtField.text {
|
|
|
|
self.saveNewDomain(userEnteredDomainName: text)
|
|
self.tableView.reloadData()
|
|
}
|
|
}
|
|
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
|
|
alertController.addTextField { (textField) in
|
|
textField.placeholder = "domain-to-block URL"
|
|
}
|
|
alertController.addAction(saveAction)
|
|
alertController.addAction(cancelAction)
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
|
|
@objc func showSubmenu() {
|
|
subMenu.isHidden = false
|
|
}
|
|
|
|
@objc func hideSubmenu() {
|
|
subMenu.isHidden = true
|
|
}
|
|
|
|
@objc func deleteList() {
|
|
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: UIAlertAction.Style.default, handler: { _ in
|
|
print("Return")
|
|
}))
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("Yes, Delete", comment: ""),
|
|
style: UIAlertAction.Style.destructive,
|
|
handler: { [weak self] (_) in
|
|
guard let self else { return }
|
|
self.tableView.reloadData()
|
|
}))
|
|
self.present(alert, animated: true, completion: nil)
|
|
}
|
|
}
|