220 lines
7.9 KiB
Swift
220 lines
7.9 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 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?
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
@Proxy(\.model.surface)
|
|
open var surface: Surface
|
|
|
|
@Proxy(\.model.use)
|
|
open var use: Use
|
|
|
|
@Proxy(\.model.disabled)
|
|
open var disabled: Bool {
|
|
didSet {
|
|
self.isEnabled = !disabled
|
|
}
|
|
}
|
|
|
|
open var buttonWidth: CGFloat = 200 {
|
|
didSet {
|
|
self.updateView(viewModel: model)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
@Proxy(\.model.attributes)
|
|
open var attributes: [any LabelAttributeModel]?
|
|
|
|
//--------------------------------------------------
|
|
// 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() {
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
mainStackView.addArrangedSubview(self)
|
|
mainStackView.spacing = 12
|
|
}
|
|
|
|
open func reset() {
|
|
// text = nil
|
|
accessibilityCustomActions = []
|
|
accessibilityTraits = .staticText
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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 borderColor = buttonBorderColorConfiguration.getColor(viewModel)
|
|
let borderWidth = viewModel.use == .secondary ? 1.0 : 0.0
|
|
let titleColor = buttonTitleColorConfiguration.getColor(viewModel)
|
|
|
|
self.titleLabel?.font = TypographicalStyle.BoldBodyLarge.font
|
|
self.backgroundColor = backgroundColor
|
|
self.setTitleColor(titleColor, for: .normal)
|
|
self.layer.borderColor = borderColor.cgColor
|
|
self.layer.cornerRadius = self.frame.size.height / 2
|
|
self.layer.borderWidth = borderWidth
|
|
let buttonHeight : CGFloat = 44.0
|
|
|
|
if buttonWidthConstraint == nil {
|
|
buttonWidthConstraint = self.widthAnchor.constraint(equalToConstant: buttonWidth)
|
|
} else {
|
|
buttonWidthConstraint?.constant = buttonWidth
|
|
}
|
|
buttonWidthConstraint?.isActive = true
|
|
|
|
if buttonHeightConstraint == nil {
|
|
buttonHeightConstraint = self.heightAnchor.constraint(equalToConstant: buttonHeight)
|
|
} else {
|
|
buttonHeightConstraint?.constant = buttonHeight
|
|
}
|
|
buttonHeightConstraint?.isActive = true
|
|
|
|
}
|
|
|
|
private class UseableColorConfiguration<ModelType:Disabling & Surfaceable & Useable> : Colorable {
|
|
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)
|
|
}
|
|
}
|
|
}
|