From 7687c42fe2d80ecabf9f128cecda9c2993931d1c Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 23 Sep 2022 16:38:52 -0500 Subject: [PATCH] minor fixes Signed-off-by: Matt Bruce --- VDS.xcodeproj/project.pbxproj | 2 +- VDS/Components/Button/Button.swift | 173 +++++++++++++----------- VDS/Components/Button/ButtonModel.swift | 35 ++--- VDS/Protocols/Useable.swift | 25 ---- 4 files changed, 106 insertions(+), 129 deletions(-) diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 416c3bdb..a34f8806 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -302,8 +302,8 @@ EA33619D288B1E330071C351 /* Components */ = { isa = PBXGroup; children = ( - 5FC35BE128D513EB004EBEAC /* Button */, EA4DB2FE28DCBC1900103EE3 /* Badge */, + 5FC35BE128D513EB004EBEAC /* Button */, EAF7F092289985E200B287F5 /* Checkbox */, EA3362412892EF700071C351 /* Label */, EA89200B28B530F0006B9984 /* RadioBox */, diff --git a/VDS/Components/Button/Button.swift b/VDS/Components/Button/Button.swift index 85b7a7d5..bdddb0a9 100644 --- a/VDS/Components/Button/Button.swift +++ b/VDS/Components/Button/Button.swift @@ -25,55 +25,34 @@ open class ButtonBase: UIButton, ModelHandlerable, ViewP //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- - private var mainStackView: UIStackView = { - return UIStackView().with { - $0.translatesAutoresizingMaskIntoConstraints = false - $0.alignment = .top - $0.axis = .vertical - $0.spacing = 0 - } - }() - - private var buttonWidthConstraint: NSLayoutConstraint? - private var buttonHeightConstraint: NSLayoutConstraint? + private var minWidthConstraint: NSLayoutConstraint? + private var widthConstraint: NSLayoutConstraint? + private var heightConstraint: NSLayoutConstraint? + //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - private let PaddingOneHalf: CGFloat = 2 - private let PaddingOne: CGFloat = 4 - private let PaddingTwo: CGFloat = 8 - private let PaddingThree: CGFloat = 12 - private let PaddingFour: CGFloat = 16 - private let PaddingFive: CGFloat = 24 - private let PaddingEight: CGFloat = 32 - private let PaddingTen: CGFloat = 40 - private let PaddingTwelve: CGFloat = 48 - private let PaddingEighteen: CGFloat = 72 - @Proxy(\.model.surface) open var surface: Surface - @Proxy(\.model.use) - open var use: Use - - open var buttonSize: Use.Size = .large { - didSet { - model.buttonSize = buttonSize - } - } - @Proxy(\.model.disabled) open var disabled: Bool { didSet { - self.isEnabled = !disabled + isEnabled = !disabled } } + + @Proxy(\.model.text) + open var text: String? - open var buttonWidth: CGFloat = 200 { - didSet { - self.updateView(viewModel: model) - } - } + @Proxy(\.model.use) + open var use: Use + + @Proxy(\.model.size) + open var size: ButtonSize + + @Proxy(\.model.width) + open var width: CGFloat? open override var isEnabled: Bool { get { !model.disabled } @@ -86,10 +65,7 @@ open class ButtonBase: UIButton, ModelHandlerable, ViewP isUserInteractionEnabled = isEnabled } } - - @Proxy(\.model.attributes) - open var attributes: [any LabelAttributeModel]? - + //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- @@ -173,13 +149,21 @@ open class ButtonBase: UIButton, ModelHandlerable, ViewP } open func setup() { - self.translatesAutoresizingMaskIntoConstraints = false - mainStackView.addArrangedSubview(self) - mainStackView.spacing = 12 + translatesAutoresizingMaskIntoConstraints = false + titleLabel?.adjustsFontSizeToFitWidth = false + titleLabel?.lineBreakMode = .byTruncatingTail + + //only 1 of the 2 widths can be on at the same time + widthConstraint = widthAnchor.constraint(equalToConstant: 0) + minWidthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: model.size.minimumWidth) + + //height + heightConstraint = heightAnchor.constraint(equalToConstant: model.size.height) + heightConstraint?.isActive = true } open func reset() { -// text = nil + model = ModelType() accessibilityCustomActions = [] accessibilityTraits = .staticText } @@ -188,40 +172,41 @@ open class ButtonBase: UIButton, ModelHandlerable, ViewP // MARK: - Overrides //-------------------------------------------------- open func updateView(viewModel: ModelType) { - if let text = viewModel.text { - self.setTitle(text, for: .normal) - } else { - self.setTitle("No ViewModel Text", for: .normal) - } - let backgroundColor = buttonBackgroundColorConfiguration.getColor(viewModel) + + let bgColor = buttonBackgroundColorConfiguration.getColor(viewModel) let borderColor = buttonBorderColorConfiguration.getColor(viewModel) - let borderWidth = viewModel.use == .secondary ? 1.0 : 0.0 let titleColor = buttonTitleColorConfiguration.getColor(viewModel) - let buttonHeight = viewModel.buttonSize.getHeight() - let minWidth = buttonWidth >= viewModel.buttonSize.minimumWidth() ? buttonWidth : viewModel.buttonSize.minimumWidth() - - self.titleLabel?.font = viewModel.buttonSize == .large ? TypographicalStyle.BoldBodyLarge.font : TypographicalStyle.BoldBodySmall.font - self.backgroundColor = backgroundColor - self.setTitleColor(titleColor, for: .normal) - self.layer.borderColor = borderColor.cgColor - self.layer.cornerRadius = buttonHeight / 2 - self.layer.borderWidth = borderWidth + let borderWidth = viewModel.use == .secondary ? 1.0 : 0.0 + let buttonHeight = viewModel.size.height + let cornerRadius = buttonHeight / 2 + let minWidth = viewModel.size.minimumWidth + let font = viewModel.size == .large ? TypographicalStyle.BoldBodyLarge.font : TypographicalStyle.BoldBodySmall.font + let edgeInsets = viewModel.size.edgeInsets - if buttonWidthConstraint == nil { - buttonWidthConstraint = self.widthAnchor.constraint(equalToConstant: minWidth) + if let text = viewModel.text { + setTitle(text, for: .normal) } else { - buttonWidthConstraint?.constant = minWidth + setTitle("No ViewModel Text", for: .normal) } - buttonWidthConstraint?.isActive = true - - if buttonHeightConstraint == nil { - buttonHeightConstraint = self.heightAnchor.constraint(equalToConstant: buttonHeight) - } else { - buttonHeightConstraint?.constant = buttonHeight - } - buttonHeightConstraint?.isActive = true + titleLabel?.font = font + backgroundColor = bgColor + setTitleColor(titleColor, for: .normal) + layer.borderColor = borderColor.cgColor + layer.cornerRadius = cornerRadius + layer.borderWidth = borderWidth + contentEdgeInsets = edgeInsets - contentEdgeInsets = getContentEdgeInsets(viewModel: viewModel) + minWidthConstraint?.constant = minWidth + heightConstraint?.constant = buttonHeight + + if let width = viewModel.width, width > minWidth { + widthConstraint?.constant = width + widthConstraint?.isActive = true + minWidthConstraint?.isActive = false + } else { + widthConstraint?.isActive = false + minWidthConstraint?.isActive = true + } } //-------------------------------------------------- @@ -237,22 +222,52 @@ open class ButtonBase: UIButton, ModelHandlerable, ViewP public func getColor(_ viewModel: ModelType) -> UIColor { return viewModel.use == .primary ? primary.getColor(viewModel) : secondary.getColor(viewModel) } + } + +} + +extension ButtonSize { + + public var height: CGFloat { + switch self { + case .large: + return 44 + case .small: + return 32 + } } - private func getContentEdgeInsets(viewModel: ModelType) -> UIEdgeInsets { + public var minimumWidth: CGFloat { + switch self { + case .large: + return 76 + case .small: + return 60 + } + } + + public var edgeInsets: UIEdgeInsets { var verticalPadding = 0.0 var horizontalPadding = 0.0 - switch viewModel.buttonSize { + switch self { case .large: - verticalPadding = PaddingThree - horizontalPadding = PaddingFive + verticalPadding = 12 + horizontalPadding = 24 break case .small: - verticalPadding = PaddingTwo - horizontalPadding = PaddingFour + verticalPadding = 8 + horizontalPadding = 16 break } return UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding) } } + +extension Use { + + public var color: UIColor { + return self == .primary ? VDSColor.backgroundPrimaryDark : .clear + } + +} diff --git a/VDS/Components/Button/ButtonModel.swift b/VDS/Components/Button/ButtonModel.swift index c7eee689..d5b7f0d9 100644 --- a/VDS/Components/Button/ButtonModel.swift +++ b/VDS/Components/Button/ButtonModel.swift @@ -8,40 +8,27 @@ import Foundation import UIKit -public protocol ButtonModel: Modelable, Useable, Equatable, AnyEquatable { +public enum ButtonSize: String, Codable, CaseIterable { + case large + case small +} + +public protocol ButtonModel: Modelable, Useable { var text: String? { get set } - var attributes: [any LabelAttributeModel]? { get set } - var buttonWidth: CGFloat? { get set } - var buttonSize: Use.Size { get set } + var width: CGFloat? { get set } + var size: ButtonSize { get set } var use: Use { get set } } public struct DefaultButtonModel: ButtonModel { - - public static func == (lhs: DefaultButtonModel, rhs: DefaultButtonModel) -> Bool { - lhs.isEqual(rhs) - } - - public func isEqual(_ equatable: DefaultButtonModel) -> Bool { - return id == equatable.id - && attributes == equatable.attributes - && text == equatable.text - && surface == equatable.surface - && use == equatable.use - && typograpicalStyle == equatable.typograpicalStyle - && disabled == equatable.disabled - && buttonWidth == equatable.buttonWidth - && buttonSize == equatable.buttonSize - } - + public var id = UUID() public var text: String? - public var attributes: [any LabelAttributeModel]? public var typograpicalStyle: TypographicalStyle = .BoldBodyLarge public var surface: Surface = .light public var use: Use = .primary public var disabled: Bool = false - public var buttonWidth: CGFloat? - public var buttonSize: Use.Size = .large + public var width: CGFloat? + public var size: ButtonSize = .large public init(){} } diff --git a/VDS/Protocols/Useable.swift b/VDS/Protocols/Useable.swift index 62215ca8..3f60ae18 100644 --- a/VDS/Protocols/Useable.swift +++ b/VDS/Protocols/Useable.swift @@ -11,31 +11,6 @@ import VDSColorTokens public enum Use: String, Codable, Equatable { case primary, secondary - public var color: UIColor { - return self == .primary ? VDSColor.backgroundPrimaryDark : .clear - } - public enum Size: String, Codable { - case large - case small - - func getHeight() -> CGFloat { - switch self { - case .large: - return 44 - case .small: - return 32 - } - } - - func minimumWidth() -> CGFloat { - switch self { - case .large: - return 76 - case .small: - return 60 - } - } - } } public protocol Useable {