vds_ios/VDS/BaseClasses/Control.swift
Matt Bruce ad6c00214e removing touchUpInsideCount, since now we are keying off of onClickSubscriber
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-09-14 08:31:51 -05:00

147 lines
4.3 KiB
Swift

//
// Control.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import UIKit
import Combine
/// Base Class use to build Controls.
@objc(VDSControl)
open class Control: UIControl, ViewProtocol, UserInfoable, Clickable {
//--------------------------------------------------
// 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: - Combine Properties
//--------------------------------------------------
open var subscribers = Set<AnyCancellable>()
open var onClickSubscriber: AnyCancellable? {
willSet {
if let onClickSubscriber {
onClickSubscriber.cancel()
}
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var initialSetupPerformed = false
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
open var shouldUpdateView: Bool = true
open var userInfo = [String: Primitive]()
open var surface: Surface = .light { didSet { setNeedsUpdate() } }
/// Whether the Control is selected or not.
open override var isSelected: Bool { didSet { setNeedsUpdate() } }
/// Whether the Control can handle the isHighlighted state.
open var canHighlight: Bool = true
var isHighlightAnimating = false
/// Whether the Control is highlighted or not.
open override var isHighlighted: Bool {
didSet {
if canHighlight && isHighlightAnimating == false && onClickSubscriber != nil {
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()
//isUserInteractionEnabled = isEnabled
}
}
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
setup()
setNeedsUpdate()
}
}
open func setup() {
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
}
open func updateView() { }
open func updateAccessibility() {
if isSelected {
accessibilityTraits.insert(.selected)
} else {
accessibilityTraits.remove(.selected)
}
if isEnabled {
accessibilityTraits.remove(.notEnabled)
} else {
accessibilityTraits.insert(.notEnabled)
}
}
open func reset() {
backgroundColor = .clear
surface = .light
isEnabled = true
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
/// Implement accessibilityActivate on an element in order to handle the default action.
/// - Returns: Based on whether the userInteraction is enabled.
override open func accessibilityActivate() -> Bool {
// Hold state in case User wanted isAnimated to remain off.
guard isUserInteractionEnabled else { return false }
sendActions(for: .touchUpInside)
return true
}
}