274 lines
9.3 KiB
Swift
274 lines
9.3 KiB
Swift
//
|
|
// Button.swift
|
|
// VDS
|
|
//
|
|
// Created by Jarrod Courtney on 9/16/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import VDSFormControlsTokens
|
|
import Combine
|
|
|
|
public class Button:ButtonBase<DefaultButtonModel>{}
|
|
|
|
open class ButtonBase<ModelType: ButtonModel>: UIButton, ModelHandlerable, ViewProtocol, Resettable {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Combine Properties
|
|
//--------------------------------------------------
|
|
@Published public var model: ModelType = ModelType()
|
|
public var modelPublisher: Published<ModelType>.Publisher { $model }
|
|
public var subscribers = Set<AnyCancellable>()
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var minWidthConstraint: NSLayoutConstraint?
|
|
private var widthConstraint: NSLayoutConstraint?
|
|
private var heightConstraint: NSLayoutConstraint?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
@Proxy(\.model.surface)
|
|
open var surface: Surface
|
|
|
|
@Proxy(\.model.disabled)
|
|
open var disabled: Bool {
|
|
didSet {
|
|
isEnabled = !disabled
|
|
}
|
|
}
|
|
|
|
@Proxy(\.model.text)
|
|
open var text: String?
|
|
|
|
@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 }
|
|
set {
|
|
//create local vars for clear coding
|
|
let disabled = !newValue
|
|
if model.disabled != disabled {
|
|
model.disabled = disabled
|
|
}
|
|
isUserInteractionEnabled = isEnabled
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration Properties
|
|
//--------------------------------------------------
|
|
private var buttonBackgroundColorConfiguration: UseableColorConfiguration<ModelType> = {
|
|
return UseableColorConfiguration<ModelType>().with {
|
|
$0.primary.enabled.lightColor = VDSColor.backgroundPrimaryDark
|
|
$0.primary.enabled.darkColor = VDSColor.backgroundPrimaryLight
|
|
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
|
|
$0.secondary.enabled.lightColor = UIColor.clear
|
|
$0.secondary.enabled.darkColor = UIColor.clear
|
|
$0.secondary.disabled.lightColor = UIColor.clear
|
|
$0.secondary.disabled.darkColor = UIColor.clear
|
|
}
|
|
}()
|
|
|
|
private var buttonBorderColorConfiguration: UseableColorConfiguration<ModelType> = {
|
|
return UseableColorConfiguration<ModelType>().with {
|
|
$0.primary.enabled.lightColor = VDSColor.elementsPrimaryOndark
|
|
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
|
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
|
|
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
|
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
|
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
}
|
|
}()
|
|
|
|
private var buttonTitleColorConfiguration: UseableColorConfiguration<ModelType> = {
|
|
return UseableColorConfiguration<ModelType>().with {
|
|
$0.primary.enabled.lightColor = VDSColor.elementsPrimaryOndark
|
|
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
|
$0.primary.disabled.lightColor = VDSColor.elementsPrimaryOndark
|
|
$0.primary.disabled.darkColor = VDSColor.elementsPrimaryOnlight
|
|
|
|
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
|
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
|
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
}
|
|
}()
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init(with model: ModelType) {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
set(with: model)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
set(with: model)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Functions
|
|
//--------------------------------------------------
|
|
open func initialSetup() {
|
|
backgroundColor = .clear
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
accessibilityCustomActions = []
|
|
accessibilityTraits = .staticText
|
|
setupUpdateView()
|
|
setup()
|
|
}
|
|
|
|
open func setup() {
|
|
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() {
|
|
model = ModelType()
|
|
accessibilityCustomActions = []
|
|
accessibilityTraits = .staticText
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
open func updateView(viewModel: ModelType) {
|
|
|
|
let bgColor = buttonBackgroundColorConfiguration.getColor(viewModel)
|
|
let borderColor = buttonBorderColorConfiguration.getColor(viewModel)
|
|
let titleColor = buttonTitleColorConfiguration.getColor(viewModel)
|
|
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 let text = viewModel.text {
|
|
setTitle(text, for: .normal)
|
|
} else {
|
|
setTitle("No ViewModel Text", for: .normal)
|
|
}
|
|
titleLabel?.font = font
|
|
backgroundColor = bgColor
|
|
setTitleColor(titleColor, for: .normal)
|
|
layer.borderColor = borderColor.cgColor
|
|
layer.cornerRadius = cornerRadius
|
|
layer.borderWidth = borderWidth
|
|
contentEdgeInsets = edgeInsets
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - PRIVATE
|
|
//--------------------------------------------------
|
|
|
|
private class UseableColorConfiguration<ModelType:Disabling & Surfaceable & Useable> : ClassColorable {
|
|
public var primary = DisabledSurfaceColorConfiguration<ModelType>()
|
|
public var secondary = DisabledSurfaceColorConfiguration<ModelType>()
|
|
|
|
required public init(){}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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 self {
|
|
case .large:
|
|
verticalPadding = 12
|
|
horizontalPadding = 24
|
|
break
|
|
case .small:
|
|
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
|
|
}
|
|
|
|
}
|