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

66 lines
2.3 KiB
Swift
Raw Normal View History

2020-02-04 11:13:30 +01:00
2020-02-20 04:37:17 +01:00
class Sheet : BaseVC {
2020-02-04 11:13:30 +01:00
private(set) var bottomConstraint: NSLayoutConstraint!
// MARK: Settings
let overshoot: CGFloat = 40
2021-01-11 02:53:19 +01:00
class var isDismissable: Bool { true }
2020-02-04 11:13:30 +01:00
// MARK: Components
lazy var contentView: UIView = {
let result = UIView()
result.backgroundColor = Colors.modalBackground
result.layer.cornerRadius = 24
result.layer.masksToBounds = false
result.layer.borderColor = isLightMode ? UIColor.white.cgColor : Colors.modalBorder.cgColor
2021-01-29 01:46:32 +01:00
result.layer.borderWidth = 1
2020-02-04 11:13:30 +01:00
result.layer.shadowColor = UIColor.black.cgColor
result.layer.shadowRadius = isLightMode ? 2 : 8
result.layer.shadowOpacity = isLightMode ? 0.1 : 0.64
2020-02-04 11:13:30 +01:00
return result
}()
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
2021-01-29 01:46:32 +01:00
let alpha = isLightMode ? CGFloat(0.1) : Values.highOpacity
view.backgroundColor = UIColor(hex: 0x000000).withAlphaComponent(alpha)
2021-01-11 02:53:19 +01:00
if type(of: self).isDismissable {
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(close))
swipeGestureRecognizer.direction = .down
view.addGestureRecognizer(swipeGestureRecognizer)
}
2020-02-04 11:13:30 +01:00
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
view.addSubview(contentView)
2021-01-29 01:46:32 +01:00
contentView.pin(.leading, to: .leading, of: view, withInset: -1)
contentView.pin(.trailing, to: .trailing, of: view, withInset: 1)
2020-02-04 11:13:30 +01:00
bottomConstraint = contentView.pin(.bottom, to: .bottom, of: view, withInset: overshoot)
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 {
2021-01-11 02:53:19 +01:00
if type(of: self).isDismissable {
close()
}
2020-02-04 11:13:30 +01:00
}
}
@objc func close() {
dismiss(animated: true, completion: nil)
}
}