vds_ios/VDS/Classes/View.swift
Matt Bruce dec1a00bb5 refactored out Handlerable
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-08 09:46:52 -05:00

108 lines
3.1 KiB
Swift

//
// 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, ViewProtocol, UserInfoable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
/// Set of Subscribers for any Publishers for this Control.
open var subscribers = Set<AnyCancellable>()
//--------------------------------------------------
// 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
}
}
}
/// Whether the View is enabled or not.
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: - Lifecycle
//--------------------------------------------------
/// Executed on initialization for this View.
open func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
setup()
setNeedsUpdate()
}
}
/// 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
}
/// Function used to make changes to the View based off a change events or from local properties..
open func updateView() {
}
/// Used to update any Accessibility properties.
open func updateAccessibility() {
if isEnabled {
accessibilityTraits.remove(.notEnabled)
} else {
accessibilityTraits.insert(.notEnabled)
}
}
/// Resets to default settings.
open func reset() {
backgroundColor = .clear
surface = .light
disabled = false
}
}