98 lines
2.6 KiB
Swift
98 lines
2.6 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 {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Combine Properties
|
|
//--------------------------------------------------
|
|
public var subject = PassthroughSubject<Void, Never>()
|
|
public var subscribers = Set<AnyCancellable>()
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
private var initialSetupPerformed = false
|
|
|
|
open var surface: Surface = .light { didSet { didChange() } }
|
|
|
|
open var disabled: Bool = false { didSet { isEnabled = !disabled } }
|
|
|
|
open override var isSelected: Bool { didSet { didChange() } }
|
|
|
|
open override var isEnabled: Bool {
|
|
get { !disabled }
|
|
set {
|
|
if disabled != !newValue {
|
|
disabled = !newValue
|
|
}
|
|
isUserInteractionEnabled = isEnabled
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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()
|
|
setupDidChangeEvent()
|
|
}
|
|
}
|
|
|
|
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() {}
|
|
|
|
open func reset() {
|
|
backgroundColor = .clear
|
|
surface = .light
|
|
disabled = false
|
|
}
|
|
|
|
// MARK: - ViewProtocol
|
|
/// Will be called only once.
|
|
open func setup() {
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
insetsLayoutMarginsFromSafeArea = false
|
|
}
|
|
}
|