// // Control.swift // VDS // // Created by Matt Bruce on 7/22/22. // import Foundation import UIKit import Combine @objc(VDSView) /// Base Class used to build Views. open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable { //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- public var subscribers = Set() //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- private var initialSetupPerformed = false /// Key of whether or not updateView() is called in setNeedsUpdate() open var shouldUpdateView: Bool = true /// Dictionary for keeping information for this Control use only Primitives open var userInfo = [String: Primitive]() /// Current Surface and this is used to pass down to child objects that implement Surfacable open var surface: Surface = .light { didSet { setNeedsUpdate() } } /// Whether this object is disabled or not open var disabled: Bool { get { !isEnabled } set { if !isEnabled != newValue { isEnabled = !newValue } } } open var isEnabled: Bool = true { didSet { setNeedsUpdate(); isUserInteractionEnabled = isEnabled } } //-------------------------------------------------- // 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 //-------------------------------------------------- /// Executed on initialization for this View open func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true setup() setNeedsUpdate() } } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- /// Update this view based off of property changes open func updateView() { updateAccessibilityLabel() } /// Used to update any Accessibility properties open func updateAccessibilityLabel() { } /// Resets to the Views default values open func reset() { backgroundColor = .clear surface = .light disabled = false } /// Will be called only once and should be overridden in subclasses to setup UI or defaults open func setup() { backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false } }