vds_ios/VDS/Classes/View.swift
Matt Bruce abc7f409e7 updated to have 2 subjects for the strategies
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-07-14 09:10:05 -05:00

106 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, Handlerable, ViewProtocol, Resettable, UserInfoable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
public var delayedSubject = PassthroughSubject<Void, Never>()
public var immediateSubject = PassthroughSubject<Void, Never>()
public var updateStrategy: HandlerableUpdateStrategy = .immediate
public var subscribers = Set<AnyCancellable>()
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
private var initialSetupPerformed = false
/// 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 = false { didSet { setNeedsUpdate(); isUserInteractionEnabled = !disabled } }
//--------------------------------------------------
// 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()
setupNeedsUpdateEvent()
updateView()
}
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open override func didMoveToWindow() {
super.didMoveToWindow()
if window != nil {
updateStrategy = .delayed
}
}
/// 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
}
}