session-ios/Session/Sheets & Modals/Modal.swift

75 lines
2.9 KiB
Swift
Raw Normal View History

2019-12-05 01:42:31 +01:00
@objc(LKModal)
2020-02-20 04:37:17 +01:00
class Modal : BaseVC {
2019-12-05 01:42:31 +01:00
private(set) var verticalCenteringConstraint: NSLayoutConstraint!
// MARK: Components
lazy var contentView: UIView = {
let result = UIView()
result.backgroundColor = Colors.modalBackground
2021-02-22 04:47:40 +01:00
result.layer.cornerRadius = Modal.cornerRadius
2019-12-05 01:42:31 +01:00
result.layer.masksToBounds = false
2020-09-04 05:09:54 +02:00
result.layer.borderColor = isLightMode ? UIColor.white.cgColor : Colors.modalBorder.cgColor
2021-01-29 01:46:32 +01:00
result.layer.borderWidth = 1
2019-12-05 01:42:31 +01:00
result.layer.shadowColor = UIColor.black.cgColor
2020-09-04 05:09:54 +02:00
result.layer.shadowRadius = isLightMode ? 2 : 8
result.layer.shadowOpacity = isLightMode ? 0.1 : 0.64
2019-12-05 01:42:31 +01:00
return result
}()
lazy var cancelButton: UIButton = {
let result = UIButton()
result.set(.height, to: Values.mediumButtonHeight)
2021-02-22 05:10:01 +01:00
result.layer.cornerRadius = Modal.buttonCornerRadius
2019-12-05 01:42:31 +01:00
result.backgroundColor = Colors.buttonBackground
result.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize)
result.setTitleColor(Colors.text, for: UIControl.State.normal)
result.setTitle(NSLocalizedString("cancel", comment: ""), for: UIControl.State.normal)
2019-12-05 01:42:31 +01:00
return result
}()
2021-02-22 04:47:40 +01:00
// MARK: Settings
private static let cornerRadius: CGFloat = 10
2021-02-22 05:10:01 +01:00
static let buttonCornerRadius = CGFloat(5)
2021-02-22 04:47:40 +01:00
2019-12-05 01:42:31 +01:00
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
2021-01-29 01:46:32 +01:00
let alpha = isLightMode ? CGFloat(0.1) : Values.highOpacity
2020-09-04 05:09:54 +02:00
view.backgroundColor = UIColor(hex: 0x000000).withAlphaComponent(alpha)
2019-12-05 05:41:18 +01:00
cancelButton.addTarget(self, action: #selector(close), for: UIControl.Event.touchUpInside)
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(close))
swipeGestureRecognizer.direction = .down
view.addGestureRecognizer(swipeGestureRecognizer)
2019-12-05 01:42:31 +01:00
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
view.addSubview(contentView)
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: Values.veryLargeSpacing).isActive = true
view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: Values.veryLargeSpacing).isActive = true
verticalCenteringConstraint = contentView.center(.vertical, in: view)
populateContentView()
}
/// To be overridden by subclasses.
func populateContentView() {
preconditionFailure("populateContentView() is abstract and must be overridden.")
}
// MARK: Interaction
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: view)
if contentView.frame.contains(location) {
super.touchesBegan(touches, with: event)
} else {
2019-12-05 05:41:18 +01:00
close()
2019-12-05 01:42:31 +01:00
}
}
2019-12-05 05:41:18 +01:00
@objc func close() {
2019-12-05 01:42:31 +01:00
dismiss(animated: true, completion: nil)
}
}