111 lines
3.3 KiB
Swift
111 lines
3.3 KiB
Swift
//
|
|
// SelectorBase.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 6/5/23.
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
import VDSColorTokens
|
|
import VDSFormControlsTokens
|
|
|
|
public protocol SelectorControlable: Control, Changeable {
|
|
var showError: Bool { get set }
|
|
var size: CGSize { get set }
|
|
var backgroundColorConfiguration: ControlColorConfiguration { get set }
|
|
var borderColorConfiguration: ControlColorConfiguration { get set }
|
|
var selectorColorConfiguration: ControlColorConfiguration { get set }
|
|
}
|
|
|
|
open class SelectorBase: Control, SelectorControlable {
|
|
public var onChangeSubscriber: AnyCancellable? {
|
|
willSet {
|
|
if let onChangeSubscriber {
|
|
onChangeSubscriber.cancel()
|
|
}
|
|
}
|
|
}
|
|
|
|
open var size = CGSize(width: 20, height: 20) { didSet { setNeedsUpdate() }}
|
|
|
|
var _showError: Bool = false
|
|
open var showError: Bool {
|
|
get { _showError }
|
|
set {
|
|
if !isSelected && _showError != newValue {
|
|
_showError = newValue
|
|
setNeedsUpdate()
|
|
}
|
|
}
|
|
}
|
|
|
|
open override var state: UIControl.State {
|
|
get {
|
|
var state = super.state
|
|
if showError {
|
|
state.insert(.error)
|
|
} else {
|
|
state.remove(.error)
|
|
}
|
|
return state
|
|
}
|
|
}
|
|
|
|
open var backgroundColorConfiguration = ControlColorConfiguration() { didSet { setNeedsUpdate() }}
|
|
|
|
open var borderColorConfiguration = ControlColorConfiguration() { didSet { setNeedsUpdate() }}
|
|
|
|
open var selectorColorConfiguration = ControlColorConfiguration() { didSet { setNeedsUpdate() }}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Constraints
|
|
//--------------------------------------------------
|
|
private var selectorHeightConstraint: NSLayoutConstraint?
|
|
private var selectorWidthConstraint: NSLayoutConstraint?
|
|
|
|
internal var shapeLayer: CAShapeLayer?
|
|
|
|
open override func initialSetup() {
|
|
super.initialSetup()
|
|
onClick = { control in
|
|
control.toggle()
|
|
}
|
|
}
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
let layoutGuide = UILayoutGuide()
|
|
addLayoutGuide(layoutGuide)
|
|
|
|
selectorHeightConstraint = layoutGuide.heightAnchor.constraint(equalToConstant: size.height)
|
|
selectorHeightConstraint?.isActive = true
|
|
|
|
selectorWidthConstraint = layoutGuide.widthAnchor.constraint(equalToConstant: size.width)
|
|
selectorWidthConstraint?.isActive = true
|
|
|
|
NSLayoutConstraint.activate([
|
|
layoutGuide.topAnchor.constraint(equalTo: topAnchor),
|
|
layoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
|
|
|
|
layer.cornerRadius = 2.0
|
|
layer.borderWidth = VDSFormControls.widthBorder
|
|
|
|
}
|
|
|
|
open func toggle() { }
|
|
|
|
open override func updateView() {
|
|
super.updateView()
|
|
|
|
selectorHeightConstraint?.constant = size.height
|
|
selectorWidthConstraint?.constant = size.width
|
|
|
|
setNeedsLayout()
|
|
layoutIfNeeded()
|
|
}
|
|
|
|
}
|