vds_ios/VDS/Classes/Control.swift
Matt Bruce e82a94304c refactored to remove combine for local updater, replaced with setNeedsUpdate
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-05-08 11:51:10 -05:00

135 lines
3.8 KiB
Swift

//
// Control.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import UIKit
import Combine
@objc(VDSControl)
open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoable, Clickable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
public var subscribers = Set<AnyCancellable>()
public var onClickSubscriber: AnyCancellable? {
willSet {
if let onClickSubscriber {
onClickSubscriber.cancel()
}
}
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
private var initialSetupPerformed = false
open var shouldUpdateView: Bool = true
open var userInfo = [String: Primitive]()
open var surface: Surface = .light { didSet { setNeedsUpdate() } }
open var disabled: Bool = false { didSet { isEnabled = !disabled } }
open override var isSelected: Bool { didSet { setNeedsUpdate() } }
public var touchUpInsideCount: Int = 0
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?.updateView()
}) { [weak self] _ in
//you update the view since this is typically a quick change
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.updateView()
self?.isHighlightAnimating = false
})
}
}
}
}
open override var isEnabled: Bool {
get { !disabled }
set {
if disabled != !newValue {
disabled = !newValue
}
isUserInteractionEnabled = isEnabled
setNeedsUpdate()
}
}
//--------------------------------------------------
// 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: - Setup
//--------------------------------------------------
open func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
setup()
setNeedsUpdate()
}
}
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
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open func updateView() {
updateAccessibilityLabel()
}
open func updateAccessibilityLabel() {
}
open func reset() {
backgroundColor = .clear
surface = .light
disabled = false
}
// MARK: - ViewProtocol
/// Will be called only once.
open func setup() {
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
}
}