added buttonSize selector
Signed-off-by: Jarrod Courtney <jarrod.courtney@gmail.com>
This commit is contained in:
parent
adaabe7249
commit
a22bbbc2b3
@ -39,13 +39,29 @@ open class ButtonBase<ModelType: ButtonModel>: UIButton, ModelHandlerable, ViewP
|
||||
//--------------------------------------------------
|
||||
// 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 {
|
||||
@ -181,19 +197,20 @@ open class ButtonBase<ModelType: ButtonModel>: UIButton, ModelHandlerable, ViewP
|
||||
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 = TypographicalStyle.BoldBodyLarge.font
|
||||
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 = self.frame.size.height / 2
|
||||
self.layer.cornerRadius = buttonHeight / 2
|
||||
self.layer.borderWidth = borderWidth
|
||||
let buttonHeight : CGFloat = 44.0
|
||||
|
||||
if buttonWidthConstraint == nil {
|
||||
buttonWidthConstraint = self.widthAnchor.constraint(equalToConstant: buttonWidth)
|
||||
buttonWidthConstraint = self.widthAnchor.constraint(equalToConstant: minWidth)
|
||||
} else {
|
||||
buttonWidthConstraint?.constant = buttonWidth
|
||||
buttonWidthConstraint?.constant = minWidth
|
||||
}
|
||||
buttonWidthConstraint?.isActive = true
|
||||
|
||||
@ -204,8 +221,13 @@ open class ButtonBase<ModelType: ButtonModel>: UIButton, ModelHandlerable, ViewP
|
||||
}
|
||||
buttonHeightConstraint?.isActive = true
|
||||
|
||||
contentEdgeInsets = getContentEdgeInsets(viewModel: viewModel)
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - PRIVATE
|
||||
//--------------------------------------------------
|
||||
|
||||
private class UseableColorConfiguration<ModelType:Disabling & Surfaceable & Useable> : Colorable {
|
||||
public var primary = DisabledSurfaceColorConfiguration<ModelType>()
|
||||
public var secondary = DisabledSurfaceColorConfiguration<ModelType>()
|
||||
@ -216,4 +238,21 @@ open class ButtonBase<ModelType: ButtonModel>: UIButton, ModelHandlerable, ViewP
|
||||
return viewModel.use == .primary ? primary.getColor(viewModel) : secondary.getColor(viewModel)
|
||||
}
|
||||
}
|
||||
|
||||
private func getContentEdgeInsets(viewModel: ModelType) -> UIEdgeInsets {
|
||||
var verticalPadding = 0.0
|
||||
var horizontalPadding = 0.0
|
||||
switch viewModel.buttonSize {
|
||||
case .large:
|
||||
verticalPadding = PaddingThree
|
||||
horizontalPadding = PaddingFive
|
||||
break
|
||||
case .small:
|
||||
verticalPadding = PaddingTwo
|
||||
horizontalPadding = PaddingFour
|
||||
break
|
||||
}
|
||||
return UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -11,6 +11,9 @@ import UIKit
|
||||
public protocol ButtonModel: Modelable, Useable, Equatable, AnyEquatable {
|
||||
var text: String? { get set }
|
||||
var attributes: [any LabelAttributeModel]? { get set }
|
||||
var buttonWidth: CGFloat? { get set }
|
||||
var buttonSize: Use.Size { get set }
|
||||
var use: Use { get set }
|
||||
}
|
||||
|
||||
public struct DefaultButtonModel: ButtonModel {
|
||||
@ -28,6 +31,7 @@ public struct DefaultButtonModel: ButtonModel {
|
||||
&& typograpicalStyle == equatable.typograpicalStyle
|
||||
&& disabled == equatable.disabled
|
||||
&& buttonWidth == equatable.buttonWidth
|
||||
&& buttonSize == equatable.buttonSize
|
||||
}
|
||||
|
||||
public var id = UUID()
|
||||
@ -38,5 +42,6 @@ public struct DefaultButtonModel: ButtonModel {
|
||||
public var use: Use = .primary
|
||||
public var disabled: Bool = false
|
||||
public var buttonWidth: CGFloat?
|
||||
public var buttonSize: Use.Size = .large
|
||||
public init(){}
|
||||
}
|
||||
|
||||
@ -14,6 +14,28 @@ public enum Use: String, Codable, Equatable {
|
||||
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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user