Debug a bit more

pull/249/head
nielsandriesse 5 years ago
parent f3f55d3aee
commit 67df07537e

@ -2,6 +2,7 @@
final class Button : UIButton { final class Button : UIButton {
private let style: Style private let style: Style
private let size: Size private let size: Size
private var heightConstraint: NSLayoutConstraint!
enum Style { enum Style {
case unimportant, regular, prominentOutline, prominentFilled, regularBorderless case unimportant, regular, prominentOutline, prominentFilled, regularBorderless
@ -16,6 +17,7 @@ final class Button : UIButton {
self.size = size self.size = size
super.init(frame: .zero) super.init(frame: .zero)
setUpStyle() setUpStyle()
NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil)
} }
override init(frame: CGRect) { override init(frame: CGRect) {
@ -26,6 +28,10 @@ final class Button : UIButton {
preconditionFailure("Use init(style:) instead.") preconditionFailure("Use init(style:) instead.")
} }
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setUpStyle() { private func setUpStyle() {
let fillColor: UIColor let fillColor: UIColor
switch style { switch style {
@ -57,7 +63,7 @@ final class Button : UIButton {
case .medium: height = Values.mediumButtonHeight case .medium: height = Values.mediumButtonHeight
case .large: height = Values.largeButtonHeight case .large: height = Values.largeButtonHeight
} }
set(.height, to: height) if heightConstraint == nil { heightConstraint = set(.height, to: height) }
layer.cornerRadius = height / 2 layer.cornerRadius = height / 2
backgroundColor = fillColor backgroundColor = fillColor
layer.borderColor = borderColor.cgColor layer.borderColor = borderColor.cgColor
@ -66,4 +72,8 @@ final class Button : UIButton {
titleLabel!.font = .boldSystemFont(ofSize: fontSize) titleLabel!.font = .boldSystemFont(ofSize: fontSize)
setTitleColor(textColor, for: UIControl.State.normal) setTitleColor(textColor, for: UIControl.State.normal)
} }
@objc private func handleAppModeChangedNotification(_ notification: Notification) {
setUpStyle()
}
} }

@ -217,6 +217,7 @@ private final class NewConversationButton : UIImageView {
self.icon = icon self.icon = icon
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
setUpViewHierarchy() setUpViewHierarchy()
NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil)
} }
override init(frame: CGRect) { override init(frame: CGRect) {
@ -227,7 +228,11 @@ private final class NewConversationButton : UIImageView {
preconditionFailure("Use init(isMainButton:) instead.") preconditionFailure("Use init(isMainButton:) instead.")
} }
private func setUpViewHierarchy() { deinit {
NotificationCenter.default.removeObserver(self)
}
private func setUpViewHierarchy(isUpdate: Bool = false) {
backgroundColor = isMainButton ? Colors.accent : Colors.newConversationButtonCollapsedBackground backgroundColor = isMainButton ? Colors.accent : Colors.newConversationButtonCollapsedBackground
let size = Values.newConversationButtonCollapsedSize let size = Values.newConversationButtonCollapsedSize
layer.cornerRadius = size / 2 layer.cornerRadius = size / 2
@ -238,11 +243,17 @@ private final class NewConversationButton : UIImageView {
let iconColor = (isMainButton && isLightMode) ? UIColor.white : Colors.text let iconColor = (isMainButton && isLightMode) ? UIColor.white : Colors.text
image = icon.asTintedImage(color: iconColor)! image = icon.asTintedImage(color: iconColor)!
contentMode = .center contentMode = .center
if !isUpdate {
widthConstraint = set(.width, to: size) widthConstraint = set(.width, to: size)
heightConstraint = set(.height, to: size) heightConstraint = set(.height, to: size)
} }
} }
@objc private func handleAppModeChangedNotification(_ notification: Notification) {
setUpViewHierarchy(isUpdate: true)
}
}
// MARK: Convenience // MARK: Convenience
private extension UIView { private extension UIView {

@ -80,6 +80,10 @@ class BaseVC : UIViewController {
} }
} }
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// TODO: Post an appModeChanged notification?
}
@objc internal func handleAppModeChangedNotification(_ notification: Notification) { @objc internal func handleAppModeChangedNotification(_ notification: Notification) {
if hasGradient { if hasGradient {
setUpGradientBackground() // Re-do the gradient setUpGradientBackground() // Re-do the gradient

Loading…
Cancel
Save