2020-07-24 19:26:35 -07:00
|
|
|
//
|
|
|
|
|
// 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 {
|
2020-07-29 17:46:53 -07:00
|
|
|
didSet { setNeedsLayout() }
|
2020-07-24 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2020-07-29 17:46:53 -07:00
|
|
|
self.sendActions(for: .valueChanged)
|
2020-07-24 19:26:35 -07:00
|
|
|
}), for: .touchUpInside)
|
|
|
|
|
allowButton.imageView?.contentMode = .scaleAspectFit
|
|
|
|
|
addSubview(allowButton)
|
|
|
|
|
|
|
|
|
|
denyButton.addAction(UIAction(handler: { _ in
|
|
|
|
|
self.policyStatus = .blocked
|
2020-07-29 17:46:53 -07:00
|
|
|
self.sendActions(for: .valueChanged)
|
2020-07-24 19:26:35 -07:00
|
|
|
}), 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|