session-ios/SessionUIKit/Components/Button.swift

81 lines
3.0 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
2020-11-09 06:03:59 +01:00
public final class Button : UIButton {
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!
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
}
2020-11-09 06:03:59 +01:00
public enum Size {
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) {
self.style = style
2019-12-05 00:10:09 +01:00
self.size = size
super.init(frame: .zero)
setUpStyle()
2020-08-24 06:24:44 +02:00
NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil)
}
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)
}
private func setUpStyle() {
let fillColor: UIColor
switch style {
2020-08-26 03:06:37 +02:00
case .unimportant: fillColor = isLightMode ? UIColor.clear : Colors.unimportantButtonBackground
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
}
let borderColor: UIColor
switch style {
2020-03-17 06:18:53 +01:00
case .unimportant: borderColor = isLightMode ? Colors.text : Colors.unimportantButtonBackground
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
}
let textColor: UIColor
switch style {
case .unimportant: textColor = Colors.text
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-05 00:10:09 +01:00
let height: CGFloat
switch size {
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) }
layer.cornerRadius = height / 2
backgroundColor = fillColor
layer.borderColor = borderColor.cgColor
2021-01-29 01:46:32 +01:00
layer.borderWidth = 1
let fontSize = (size == .small) ? Values.smallFontSize : Values.mediumFontSize
2020-03-02 06:45:42 +01:00
titleLabel!.font = .boldSystemFont(ofSize: fontSize)
setTitleColor(textColor, for: UIControl.State.normal)
}
2020-08-24 06:24:44 +02:00
@objc private func handleAppModeChangedNotification(_ notification: Notification) {
setUpStyle()
}
}