vds_ios/VDS/Components/Buttons/Button/ButtonBase.swift
Matt Bruce 52b08b88c9 Merge branch 'refactor/enabling' of https://gitlab.verizon.com/BPHV_MIPS/vds_ios.git into refactor/enabling
# Conflicts:
#	VDS/Classes/SelectorGroupHandlerBase.swift
#	VDS/Components/Buttons/Button/ButtonBase.swift
#	VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift
#	VDS/Components/Label/Label.swift
#	VDS/Components/RadioButton/RadioButtonGroup.swift
#	VDS/Components/RadioSwatch/RadioSwatchGroup.swift
#	VDS/Components/TextFields/EntryField/EntryField.swift
#	VDS/Components/Tooltip/TrailingTooltipLabel.swift

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-25 16:20:00 -05:00

228 lines
7.9 KiB
Swift

//
// BaseButton.swift
// VDS
//
// Created by Matt Bruce on 11/22/22.
//
import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
public protocol Buttonable: UIControl, Surfaceable, Enabling {
var availableSizes: [ButtonSize] { get }
var text: String? { get set }
var intrinsicContentSize: CGSize { get }
}
@objc(VDSButtonBase)
open class ButtonBase: UIButton, Buttonable, ViewProtocol, UserInfoable, Clickable {
//--------------------------------------------------
// MARK: - Configuration Properties
//--------------------------------------------------
private let hitAreaHeight = 44.0
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
/// Set of Subscribers for any Publishers for this Control.
open var subscribers = Set<AnyCancellable>()
open var onClickSubscriber: AnyCancellable? {
willSet {
if let onClickSubscriber {
onClickSubscriber.cancel()
}
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var initialSetupPerformed = false
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
/// Key of whether or not updateView() is called in setNeedsUpdate()
open var shouldUpdateView: Bool = true
open var availableSizes: [ButtonSize] { [] }
open var text: String? { didSet { setNeedsUpdate() } }
open var textAttributes: [any LabelAttributeModel]? { nil }
open var useScaledFont: Bool = false { didSet { setNeedsUpdate() }}
/// Current Surface and this is used to pass down to child objects that implement Surfacable
open var surface: Surface = .light { didSet { setNeedsUpdate() }}
open var userInfo = [String: Primitive]()
open var touchUpInsideCount: Int = 0
internal var isHighlightAnimating = false
open override var isHighlighted: Bool {
didSet {
if isHighlightAnimating == false && touchUpInsideCount > 0 {
isHighlightAnimating = true
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.setNeedsUpdate()
}) { [weak self] _ in
//you update the view since this is typically a quick change
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.setNeedsUpdate()
self?.isHighlightAnimating = false
})
}
}
}
}
/// Whether the Control is enabled or not.
open override var isEnabled: Bool { didSet { setNeedsUpdate() } }
open var textStyle: TextStyle { .defaultStyle }
open var textColor: UIColor { .black }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
initialSetup()
}
public override init(frame: CGRect) {
super.init(frame: .zero)
initialSetup()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
initialSetup()
}
//--------------------------------------------------
// MARK: - Public Functions
//--------------------------------------------------
open func initialSetup() {
if !initialSetupPerformed {
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
accessibilityCustomActions = []
setup()
setNeedsUpdate()
}
}
open func setup() {
translatesAutoresizingMaskIntoConstraints = false
titleLabel?.adjustsFontSizeToFitWidth = false
titleLabel?.lineBreakMode = .byTruncatingTail
}
/// Resets to default settings.
open func reset() {
shouldUpdateView = false
surface = .light
isEnabled = true
text = nil
accessibilityCustomActions = []
shouldUpdateView = true
setNeedsUpdate()
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
override open var intrinsicContentSize: CGSize {
let intrinsicContentSize = super.intrinsicContentSize
let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom
return CGSize(width: adjustedWidth, height: adjustedHeight)
}
/// Function used to make changes to the View based off a change events or from local properties.
open func updateView() {
updateLabel()
}
open func updateAccessibility() {
if isEnabled {
accessibilityTraits.remove(.notEnabled)
} else {
accessibilityTraits.insert(.notEnabled)
}
}
//--------------------------------------------------
// MARK: - PRIVATE
//--------------------------------------------------
private func updateLabel() {
//clear the arrays holding actions
accessibilityCustomActions = []
//create the primary string
let mutableText = NSMutableAttributedString.mutableText(for: text ?? "No Text",
textStyle: textStyle,
useScaledFont: useScaledFont,
textColor: textColor,
alignment: titleLabel?.textAlignment ?? .center,
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
if let attributes = textAttributes {
//loop through the models attributes
for attribute in attributes {
//add attribute on the string
attribute.setAttribute(on: mutableText)
}
}
//set the attributed text
setAttributedTitle(mutableText, for: .normal)
setAttributedTitle(mutableText, for: .highlighted)
}
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let size = intrinsicContentSize
// Create a minimumHitArea variable with a value that represents the minimum size of the hit area you want to create for the button.
let minimumHitArea = CGSize(width: size.width, height: hitAreaHeight)
// Create a new hitFrame variable that is the same size as the minimumHitArea variable, but is centered on the button's frame.
let hitFrame = CGRect(
x: self.bounds.midX - minimumHitArea.width / 2,
y: self.bounds.midY - minimumHitArea.height / 2,
width: minimumHitArea.width,
height: minimumHitArea.height
)
// If the point that was passed to the hitTest(_:with:) method is within the hitFrame, return the button itself. This will cause the button to handle the touch event.
if hitFrame.contains(point) {
return self
}
// If the point is not within the hitFrame, return nil. This will cause the touch event to be handled by another view.
return nil
}
}
// MARK: AppleGuidlinesTouchable
extension ButtonBase: AppleGuidlinesTouchable {
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
Self.acceptablyOutsideBounds(point: point, bounds: bounds)
}
}