vds_ios/VDS/Components/Buttons/Button/Button.swift
2023-09-14 11:47:24 -05:00

227 lines
8.6 KiB
Swift

//
// Button.swift
// VDS
//
// Created by Jarrod Courtney on 9/16/22.
//
import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
/// A button is an interactive element that triggers an action. Buttons are prominent and attention-getting, with more visual emphasis than any of the Text Link components. For this reason, buttons are best suited for critical and driving actions. This class can be used within a ``ButtonGroup``.
///
/// If you are using AutoLayoutConstraints you have a combination of Leading/Left and Trailing/Right NSLayoutConstraints,
/// you need to ensure that one of these Horizontal Contraints is not constraint of "equatTo". If you are to pin the left/right edges
/// to its parent this object will stretch to the parent's width.
@objc(VDSButton)
open class Button: ButtonBase, Useable {
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
/// Enum used to describe the size.
public enum Size: String, CaseIterable {
case large
case small
/// Height for this size of button.
public var height: CGFloat {
switch self {
case .large:
return 44
case .small:
return 32
}
}
/// Corner radius for this size of button.
public var cornerRadius: CGFloat {
height / 2
}
/// Minimum width for this size of button.
public var minimumWidth: CGFloat {
switch self {
case .large:
return 76
case .small:
return 60
}
}
/// EdgeInsets for this size of button.
public var edgeInsets: UIEdgeInsets {
switch self {
case .large:
return .axis(horizontal: 24, vertical: 12)
case .small:
return .axis(horizontal: 16, vertical: 8)
}
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var initialSetupPerformed = false
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
/// The ButtonSize for ths Button.
open var size: Size = .large { didSet { setNeedsUpdate() } }
/// The Use for this Button.
open var use: Use = .primary { didSet { setNeedsUpdate() } }
private var _width: CGFloat? = nil
/// If there is a width that is larger than this size's minmumWidth, the button will resize to this width.
open var width: CGFloat? {
get { _width }
set {
if let newValue, newValue > size.minimumWidth {
_width = newValue
} else {
_width = nil
}
setNeedsUpdate()
}
}
open override var textColor: UIColor {
textColorConfiguration.getColor(self)
}
/// TextStyle used on the titleLabel.
open override var textStyle: TextStyle {
size == .large ? TextStyle.boldBodyLarge : TextStyle.boldBodySmall
}
/// The natural size for the receiving view, considering only properties of the view itself.
open override var intrinsicContentSize: CGSize {
guard let width, width > 0 else {
var superSize = super.intrinsicContentSize
superSize.height = size.height
return superSize
}
return CGSize(width: width > size.minimumWidth ? width : size.minimumWidth, height: size.height)
}
//--------------------------------------------------
// MARK: - Configuration Properties
//--------------------------------------------------
// Background Color Config
private var primaryBackgroundColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.backgroundPrimaryDark, VDSColor.backgroundPrimaryLight, forState: .normal)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
}
private var secondaryBackgroundColorConfiguration = ControlColorConfiguration()
private var backgroundColorConfiguration: ControlColorConfiguration{
use == .primary ? primaryBackgroundColorConfiguration : secondaryBackgroundColorConfiguration
}
// Border Color Config
private var primaryBorderColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .normal)
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .highlighted)
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
}
private var secondaryBorderColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
}
private var borderColorConfiguration: ControlColorConfiguration {
use == .primary ? primaryBorderColorConfiguration : secondaryBorderColorConfiguration
}
// Text Color Config
private var primaryTextColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .normal)
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .highlighted)
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .disabled)
}
private var secondaryTextColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
}
private var textColorConfiguration: ControlColorConfiguration {
use == .primary ? primaryTextColorConfiguration : secondaryTextColorConfiguration
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .button
}
/// Resets to default settings.
open override func reset() {
super.reset()
shouldUpdateView = false
use = .primary
width = nil
size = .large
shouldUpdateView = true
setNeedsUpdate()
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
let bgColor = backgroundColorConfiguration.getColor(self)
let borderColor = borderColorConfiguration.getColor(self)
let borderWidth = use == .secondary ? VDSFormControls.widthBorder : 0.0
let cornerRadius = size.cornerRadius
let edgeInsets = size.edgeInsets
backgroundColor = bgColor
layer.borderColor = borderColor.cgColor
layer.cornerRadius = cornerRadius
layer.borderWidth = borderWidth
contentEdgeInsets = edgeInsets
invalidateIntrinsicContentSize()
}
}
extension Use {
public var color: UIColor {
return self == .primary ? VDSColor.backgroundPrimaryDark : .clear
}
}