Script blocking UI works now
This commit is contained in:
55
SBrowser/Script Policy UI/ScriptControllerIconView.swift
Normal file
55
SBrowser/Script Policy UI/ScriptControllerIconView.swift
Normal file
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// ScriptControllerIconView.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/24/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class ScriptControllerIconView: UIButton
|
||||
{
|
||||
private let labelView = UILabel(frame: .zero)
|
||||
|
||||
convenience init() {
|
||||
self.init(frame: .zero)
|
||||
|
||||
addSubview(labelView)
|
||||
|
||||
let image = UIImage(systemName: "shield")
|
||||
setImage(image, for: .normal)
|
||||
|
||||
imageView?.contentMode = .scaleAspectFit
|
||||
labelView.backgroundColor = .systemRed
|
||||
labelView.textAlignment = .center
|
||||
labelView.layer.cornerRadius = 4.0
|
||||
labelView.layer.masksToBounds = true
|
||||
labelView.font = .boldSystemFont(ofSize: 8)
|
||||
labelView.textColor = .white
|
||||
|
||||
setBlockedScriptsNumber(0)
|
||||
}
|
||||
|
||||
public func setBlockedScriptsNumber(_ num: Int) {
|
||||
if num > 0 {
|
||||
labelView.isHidden = false
|
||||
labelView.text = "\(num)"
|
||||
} else {
|
||||
labelView.isHidden = true
|
||||
}
|
||||
|
||||
setNeedsLayout()
|
||||
}
|
||||
|
||||
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
||||
return CGSize(width: 44.0, height: 44.0)
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
labelView.sizeToFit()
|
||||
labelView.center = CGPoint(x: bounds.center.x + 10, y: bounds.center.y + 10)
|
||||
labelView.bounds = labelView.bounds.insetBy(dx: -2.0, dy: -2.0)
|
||||
}
|
||||
}
|
||||
73
SBrowser/Script Policy UI/ScriptPolicyControl.swift
Normal file
73
SBrowser/Script Policy UI/ScriptPolicyControl.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// ScriptPolicyControl.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/24/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class ScriptPolicyControl: UIControl
|
||||
{
|
||||
enum PolicyStatus {
|
||||
case allowed
|
||||
case blocked
|
||||
}
|
||||
|
||||
var policyStatus: PolicyStatus = .blocked {
|
||||
didSet {
|
||||
sendActions(for: .valueChanged)
|
||||
setNeedsLayout()
|
||||
}
|
||||
}
|
||||
|
||||
private class PolicyButton: UIButton {
|
||||
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
|
||||
contentRect.insetBy(dx: 8.0, dy: 8.0)
|
||||
}
|
||||
}
|
||||
|
||||
private let allowButton = PolicyButton(frame: .zero)
|
||||
private let denyButton = PolicyButton(frame: .zero)
|
||||
|
||||
convenience init() {
|
||||
self.init(frame: .zero)
|
||||
|
||||
allowButton.addAction(UIAction(handler: { _ in
|
||||
self.policyStatus = .allowed
|
||||
}), for: .touchUpInside)
|
||||
allowButton.imageView?.contentMode = .scaleAspectFit
|
||||
addSubview(allowButton)
|
||||
|
||||
denyButton.addAction(UIAction(handler: { _ in
|
||||
self.policyStatus = .blocked
|
||||
}), for: .touchUpInside)
|
||||
denyButton.imageView?.contentMode = .scaleAspectFit
|
||||
addSubview(denyButton)
|
||||
}
|
||||
|
||||
override var intrinsicContentSize: CGSize {
|
||||
CGSize(width: 100.0, height: UIView.noIntrinsicMetric)
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
allowButton.frame = CGRect(origin: .zero, size: CGSize(width: bounds.width / 2, height: bounds.height))
|
||||
denyButton.frame = CGRect(origin: CGPoint(x: allowButton.frame.maxX, y: 0), size: allowButton.frame.size)
|
||||
|
||||
if policyStatus == .allowed {
|
||||
allowButton.tintColor = .blue
|
||||
allowButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal)
|
||||
|
||||
denyButton.tintColor = .darkGray
|
||||
denyButton.setImage(UIImage(systemName: "stop.circle"), for: .normal)
|
||||
} else {
|
||||
allowButton.tintColor = .darkGray
|
||||
allowButton.setImage(UIImage(systemName: "play.circle"), for: .normal)
|
||||
|
||||
denyButton.tintColor = .red
|
||||
denyButton.setImage(UIImage(systemName: "stop.circle.fill"), for: .normal)
|
||||
}
|
||||
}
|
||||
}
|
||||
113
SBrowser/Script Policy UI/ScriptPolicyViewController.swift
Normal file
113
SBrowser/Script Policy UI/ScriptPolicyViewController.swift
Normal file
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// ScriptPolicyViewController.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/24/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
protocol ScriptPolicyViewControllerDelegate {
|
||||
func didChangeScriptPolicy()
|
||||
}
|
||||
|
||||
class ScriptPolicyControlListCell: UICollectionViewListCell
|
||||
{
|
||||
let policyControl = ScriptPolicyControl()
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
addSubview(policyControl)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
let policyControlWidth = CGFloat(100.0)
|
||||
policyControl.frame = CGRect(x: bounds.maxX - policyControlWidth, y: 0, width: policyControlWidth, height: bounds.height)
|
||||
bringSubviewToFront(policyControl)
|
||||
|
||||
contentView.frame = CGRect(origin: contentView.frame.origin, size: CGSize(width: bounds.width - policyControl.frame.width, height: contentView.frame.height))
|
||||
}
|
||||
}
|
||||
|
||||
class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
|
||||
{
|
||||
var collectionView: UICollectionView?
|
||||
var delegate: ScriptPolicyViewControllerDelegate? = nil
|
||||
var dataSource: UICollectionViewDiffableDataSource<Int, String>?
|
||||
|
||||
private var didChangeScriptPolicy = false
|
||||
|
||||
convenience init(policyManager: ResourcePolicyManager, blockedScripts: Set<String>) {
|
||||
self.init(nibName: nil, bundle: nil)
|
||||
|
||||
let listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
|
||||
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
|
||||
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
|
||||
|
||||
let registry = UICollectionView.CellRegistration<ScriptPolicyControlListCell, String> { (listCell, indexPath, item) in
|
||||
var config = listCell.defaultContentConfiguration()
|
||||
config.text = item
|
||||
|
||||
listCell.contentConfiguration = config
|
||||
|
||||
if policyManager.allowedOriginsForScriptResources().contains(item) {
|
||||
listCell.policyControl.policyStatus = .allowed
|
||||
} else {
|
||||
listCell.policyControl.policyStatus = .blocked
|
||||
}
|
||||
|
||||
listCell.policyControl.addAction(UIAction(handler: { _ in
|
||||
if listCell.policyControl.policyStatus == .allowed {
|
||||
policyManager.allowOriginToLoadScriptResources(item)
|
||||
} else {
|
||||
policyManager.disallowOriginToLoadScriptResources(item)
|
||||
}
|
||||
|
||||
self.didChangeScriptPolicy = true
|
||||
}), for: .valueChanged)
|
||||
}
|
||||
|
||||
let dataSource = UICollectionViewDiffableDataSource<Int, String>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
|
||||
collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: item)
|
||||
}
|
||||
|
||||
collectionView.dataSource = dataSource
|
||||
collectionView.delegate = self
|
||||
|
||||
var snapshot = dataSource.snapshot()
|
||||
snapshot.appendSections([ 0 ])
|
||||
snapshot.appendItems(Array(blockedScripts))
|
||||
dataSource.apply(snapshot)
|
||||
|
||||
self.dataSource = dataSource
|
||||
self.collectionView = collectionView
|
||||
|
||||
title = "Script Origin Policy"
|
||||
navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction(handler: { action in
|
||||
if self.didChangeScriptPolicy {
|
||||
self.delegate?.didChangeScriptPolicy()
|
||||
}
|
||||
|
||||
self.dismiss(animated: true, completion: nil)
|
||||
}), menu: nil)
|
||||
}
|
||||
|
||||
override func loadView() {
|
||||
self.view = collectionView
|
||||
}
|
||||
|
||||
// MARK: UICollectionViewDelegate
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
|
||||
false
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user