vds_ios/VDS/BaseClasses/View.swift

235 lines
6.2 KiB
Swift

//
// Control.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import UIKit
import Combine
/// Base Class used to build Views.
@objcMembers
@objc(VDSView)
open class View: UIView, ViewProtocol, UserInfoable, Clickable {
//--------------------------------------------------
// 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: - Combine Properties
//--------------------------------------------------
open var subscribers = Set<AnyCancellable>()
open var onClickSubscriber: AnyCancellable?
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var initialSetupPerformed = false
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
open var shouldUpdateView: Bool = true
/// Dictionary for keeping information for this Control use only Primitives.
open var userInfo = [String: Primitive]()
open var surface: Surface = .light { didSet { setNeedsUpdate() } }
open var isEnabled: Bool = true { didSet { setNeedsUpdate() } }
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
private func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
shouldUpdateView = false
setup()
setDefaults()
shouldUpdateView = true
setNeedsUpdate()
}
}
open func setup() {
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
}
open func setDefaults() {
backgroundColor = .clear
surface = .light
isEnabled = true
userInfo.removeAll()
}
open func updateView() { }
open func updateAccessibility() {
if isEnabled {
accessibilityTraits.remove(.notEnabled)
} else {
accessibilityTraits.insert(.notEnabled)
}
}
open func reset() {
shouldUpdateView = false
setDefaults()
shouldUpdateView = true
setNeedsUpdate()
}
open override func layoutSubviews() {
super.layoutSubviews()
setNeedsUpdate()
}
//--------------------------------------------------
// MARK: - Accessibility
//--------------------------------------------------
open var accessibilityAction: ((View) -> Void)?
open override var isAccessibilityElement: Bool {
get {
var block: AXBoolReturnBlock?
// if #available(iOS 17, *) {
// block = isAccessibilityElementBlock
// }
if block == nil {
block = bridge_isAccessibilityElementBlock
}
if let block {
return block()
} else {
return super.isAccessibilityElement
}
}
set {
super.isAccessibilityElement = newValue
}
}
open override var accessibilityLabel: String? {
get {
var block: AXStringReturnBlock?
// if #available(iOS 17, *) {
// block = accessibilityLabelBlock
// }
if block == nil {
block = bridge_accessibilityLabelBlock
}
if let block {
return block()
} else {
return super.accessibilityLabel
}
}
set {
super.accessibilityLabel = newValue
}
}
open override var accessibilityHint: String? {
get {
var block: AXStringReturnBlock?
// if #available(iOS 17, *) {
// block = accessibilityHintBlock
// }
if block == nil {
block = bridge_accessibilityHintBlock
}
if let block {
return block()
} else {
return super.accessibilityHint
}
}
set {
super.accessibilityHint = newValue
}
}
open override var accessibilityValue: String? {
get {
var block: AXStringReturnBlock?
// if #available(iOS 17, *) {
// block = accessibilityHintBlock
// }
if block == nil {
block = bridge_accessibilityValueBlock
}
if let block{
return block()
} else {
return super.accessibilityValue
}
}
set {
super.accessibilityValue = newValue
}
}
open override func accessibilityActivate() -> Bool {
guard isEnabled, isUserInteractionEnabled else { return false }
// if #available(iOS 17, *) {
// if let block = accessibilityAction {
// block(self)
// return true
// } else if let block = accessibilityActivateBlock {
// return block()
//
// } else if let block = bridge_accessibilityActivateBlock {
// return block()
//
// } else {
// return true
//
// }
//
// } else {
if let block = accessibilityAction {
block(self)
return true
} else if let block = bridge_accessibilityActivateBlock {
return block()
} else {
return super.accessibilityActivate()
}
// }
}
}