2020-11-09 06:03:59 +01:00
|
|
|
import UIKit
|
2019-12-02 06:54:21 +01:00
|
|
|
|
2020-11-09 06:03:59 +01:00
|
|
|
public final class Button : UIButton {
|
2019-12-02 06:54:21 +01:00
|
|
|
private let style: Style
|
2019-12-05 00:10:09 +01:00
|
|
|
private let size: Size
|
2020-08-24 06:24:44 +02:00
|
|
|
private var heightConstraint: NSLayoutConstraint!
|
2019-12-02 06:54:21 +01:00
|
|
|
|
2020-11-09 06:03:59 +01:00
|
|
|
public enum Style {
|
2019-12-12 06:07:08 +01:00
|
|
|
case unimportant, regular, prominentOutline, prominentFilled, regularBorderless
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-09 06:03:59 +01:00
|
|
|
public enum Size {
|
2019-12-12 01:10:26 +01:00
|
|
|
case medium, large, small
|
2019-12-05 00:10:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-09 06:03:59 +01:00
|
|
|
public init(style: Style, size: Size) {
|
2019-12-02 06:54:21 +01:00
|
|
|
self.style = style
|
2019-12-05 00:10:09 +01:00
|
|
|
self.size = size
|
2019-12-02 06:54:21 +01:00
|
|
|
super.init(frame: .zero)
|
|
|
|
setUpStyle()
|
2020-08-24 06:24:44 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil)
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
preconditionFailure("Use init(style:) instead.")
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
preconditionFailure("Use init(style:) instead.")
|
|
|
|
}
|
2020-08-24 06:24:44 +02:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
2019-12-02 06:54:21 +01:00
|
|
|
private func setUpStyle() {
|
|
|
|
let fillColor: UIColor
|
|
|
|
switch style {
|
2020-08-26 03:06:37 +02:00
|
|
|
case .unimportant: fillColor = isLightMode ? UIColor.clear : Colors.unimportantButtonBackground
|
2019-12-04 06:54:46 +01:00
|
|
|
case .regular: fillColor = UIColor.clear
|
2019-12-06 04:42:43 +01:00
|
|
|
case .prominentOutline: fillColor = UIColor.clear
|
2020-08-26 03:06:37 +02:00
|
|
|
case .prominentFilled: fillColor = isLightMode ? Colors.text : Colors.accent
|
2019-12-12 06:07:08 +01:00
|
|
|
case .regularBorderless: fillColor = UIColor.clear
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|
|
|
|
let borderColor: UIColor
|
|
|
|
switch style {
|
2020-03-17 06:18:53 +01:00
|
|
|
case .unimportant: borderColor = isLightMode ? Colors.text : Colors.unimportantButtonBackground
|
2019-12-04 06:54:46 +01:00
|
|
|
case .regular: borderColor = Colors.text
|
2020-08-26 03:06:37 +02:00
|
|
|
case .prominentOutline: borderColor = isLightMode ? Colors.text : Colors.accent
|
|
|
|
case .prominentFilled: borderColor = isLightMode ? Colors.text : Colors.accent
|
2019-12-12 06:07:08 +01:00
|
|
|
case .regularBorderless: borderColor = UIColor.clear
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|
|
|
|
let textColor: UIColor
|
|
|
|
switch style {
|
|
|
|
case .unimportant: textColor = Colors.text
|
2019-12-04 06:54:46 +01:00
|
|
|
case .regular: textColor = Colors.text
|
2020-08-26 03:06:37 +02:00
|
|
|
case .prominentOutline: textColor = isLightMode ? Colors.text : Colors.accent
|
|
|
|
case .prominentFilled: textColor = isLightMode ? UIColor.white : Colors.text
|
2019-12-12 06:07:08 +01:00
|
|
|
case .regularBorderless: textColor = Colors.text
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|
2019-12-05 00:10:09 +01:00
|
|
|
let height: CGFloat
|
|
|
|
switch size {
|
2019-12-12 01:10:26 +01:00
|
|
|
case .small: height = Values.smallButtonHeight
|
2019-12-05 00:10:09 +01:00
|
|
|
case .medium: height = Values.mediumButtonHeight
|
|
|
|
case .large: height = Values.largeButtonHeight
|
|
|
|
}
|
2020-08-24 06:24:44 +02:00
|
|
|
if heightConstraint == nil { heightConstraint = set(.height, to: height) }
|
2019-12-03 04:17:08 +01:00
|
|
|
layer.cornerRadius = height / 2
|
2019-12-02 06:54:21 +01:00
|
|
|
backgroundColor = fillColor
|
|
|
|
layer.borderColor = borderColor.cgColor
|
2021-01-29 01:46:32 +01:00
|
|
|
layer.borderWidth = 1
|
2019-12-12 01:10:26 +01:00
|
|
|
let fontSize = (size == .small) ? Values.smallFontSize : Values.mediumFontSize
|
2020-03-02 06:45:42 +01:00
|
|
|
titleLabel!.font = .boldSystemFont(ofSize: fontSize)
|
2019-12-02 06:54:21 +01:00
|
|
|
setTitleColor(textColor, for: UIControl.State.normal)
|
|
|
|
}
|
2020-08-24 06:24:44 +02:00
|
|
|
|
|
|
|
@objc private func handleAppModeChangedNotification(_ notification: Notification) {
|
|
|
|
setUpStyle()
|
|
|
|
}
|
2019-12-02 06:54:21 +01:00
|
|
|
}
|