Files
lockdown-iOS-mirror/LockdowniOS/BlockListGroupViewController.swift
Alexander Parshakov 2bc6adf847 Release 1.6.1
2023-01-10 21:17:38 +05:00

102 lines
3.2 KiB
Swift

//
// BlockListGroupViewController.swift
// Lockdown
//
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
import UIKit
class BlockListGroupViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
var lockdownGroup: LockdownGroup?
@IBOutlet var warningContainer: UIView!
@IBOutlet var warningLabel: UILabel!
@IBOutlet var lockdownEnabled: UISwitch!
@IBOutlet var groupTitle: UILabel!
weak var blockListVC: BlockListViewController?
override func viewDidLoad() {
super.viewDidLoad()
if let lockdown = lockdownGroup {
self.groupTitle?.text = lockdown.name
self.lockdownEnabled?.isOn = lockdown.enabled
}
warningLabel.text = lockdownGroup?.warning
if lockdownGroup?.warning != nil {
warningContainer.isHidden = false
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return (lockdownGroup?.domains.count)!
}
return 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 32
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 32))
view.backgroundColor = .systemGroupedBackground
let isLeftToRight = traitCollection.layoutDirection == .leftToRight
let label = UILabel(frame: CGRect.init(x: isLeftToRight ? 12 : -12, y: 6, width: tableView.frame.size.width, height: 24))
label.font = .mediumLockdownFont(size: 14)
label.textColor = UIColor.darkGray
if section == 0 {
label.text = .localized("Blocked Domains")
}
view.addSubview(label)
return view
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "BlockListGroupCell", for: indexPath) as? BlockListGroupCell else {
return UITableViewCell()
}
if indexPath.section == 0 {
if let domainKeys = lockdownGroup?.domains {
let keys = domainKeys.keys.sorted {$0 < $1}
cell.cellTitle?.text = keys[indexPath.row]
}
}
return cell
}
@IBAction func toggleLockdown(sender: UISwitch) {
setIsLockdownEnabled(sender.isOn)
}
private func setIsLockdownEnabled(_ isEnabled: Bool) {
if let vc = self.blockListVC {
vc.didMakeChange = true
}
lockdownGroup?.enabled = isEnabled
var ldDefaults = getLockdownBlockedDomains()
ldDefaults.lockdownDefaults[(lockdownGroup?.internalID)!] = lockdownGroup
defaults.set(try? PropertyListEncoder().encode(ldDefaults), forKey: kLockdownBlockedDomains)
}
@IBAction func dismiss() {
blockListVC?.reloadBlockLists()
self.navigationController?.popViewController(animated: true)
}
}