// // 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() open var onClickSubscriber: AnyCancellable? //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var initialSetupPerformed = false //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- open var shouldUpdateView: Bool = true open var shouldUpdateAccessibility: 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() } } /// State of animating isHighlight. public var isHighlighting = false /// Whether the Control should handle the isHighlighted state. open var shouldHighlight: Bool { isHighlighting == false && onClickSubscriber != nil } /// Whether the Control is highlighted or not. open override var isHighlighted: Bool { didSet { if shouldHighlight { isHighlighting = true setNeedsUpdate() isHighlighting = false } } } /// Whether the Control is enabled or not. open override var isEnabled: Bool { didSet { setNeedsUpdate() } } //-------------------------------------------------- // 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 onClick = nil userInfo.removeAll() } //-------------------------------------------------- // 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 } open override func layoutSubviews() { super.layoutSubviews() setNeedsUpdate() } }