375 lines
13 KiB
Swift
375 lines
13 KiB
Swift
//
|
|
// RadioBox.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/23/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import VDSFormControlsTokens
|
|
import Combine
|
|
|
|
@objc(VDSRadioBox)
|
|
public class RadioBox: RadioBoxBase{}
|
|
|
|
@objc(VDSSoloRadioBox)
|
|
public class SoloRadioBox: RadioBoxBase{
|
|
|
|
public override func initialSetup() {
|
|
super.initialSetup()
|
|
publisher(for: .touchUpInside)
|
|
.sink { control in
|
|
control.toggle()
|
|
}.store(in: &subscribers)
|
|
}
|
|
}
|
|
|
|
@objc(VDSRadioBoxBase)
|
|
open class RadioBoxBase: Control, BinaryColorable, Accessable, DataTrackable{
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var mainStackView = UIStackView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.alignment = .top
|
|
$0.axis = .vertical
|
|
$0.spacing = 0
|
|
}
|
|
|
|
private var selectorStackView = UIStackView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.alignment = .top
|
|
$0.axis = .horizontal
|
|
$0.spacing = 12
|
|
}
|
|
|
|
private var selectorLeftLabelStackView = UIStackView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.axis = .vertical
|
|
$0.spacing = 4
|
|
$0.isHidden = false
|
|
}
|
|
|
|
private var textLabel = Label().with {
|
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
$0.textPosition = .left
|
|
$0.typograpicalStyle = .BoldBodyLarge
|
|
}
|
|
|
|
private var subTextLabel = Label().with {
|
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
$0.textPosition = .left
|
|
$0.typograpicalStyle = .BodyLarge
|
|
}
|
|
|
|
private var subTextRightLabel = Label().with {
|
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
$0.textPosition = .right
|
|
$0.typograpicalStyle = .BodyLarge
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public var selectorView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
open var text: String = "Default Text" { didSet { didChange() }}
|
|
|
|
open var textAttributes: [any LabelAttributeModel]? { didSet { didChange() }}
|
|
|
|
open var textAttributedText: NSAttributedString? {
|
|
didSet {
|
|
textLabel.useAttributedText = !(textAttributedText?.string.isEmpty ?? true)
|
|
textLabel.attributedText = textAttributedText
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
open var subText: String? { didSet { didChange() }}
|
|
|
|
open var subTextAttributes: [any LabelAttributeModel]? { didSet { didChange() }}
|
|
|
|
open var subTextAttributedText: NSAttributedString? {
|
|
didSet {
|
|
subTextLabel.useAttributedText = !(subTextAttributedText?.string.isEmpty ?? true)
|
|
subTextLabel.attributedText = subTextAttributedText
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
open var subTextRight: String? { didSet { didChange() }}
|
|
|
|
open var subTextRightAttributes: [any LabelAttributeModel]? { didSet { didChange() }}
|
|
|
|
open var subTextRightAttributedText: NSAttributedString? {
|
|
didSet {
|
|
subTextRightLabel.useAttributedText = !(subTextRightAttributedText?.string.isEmpty ?? true)
|
|
subTextRightLabel.attributedText = subTextRightAttributedText
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
open var strikethrough: Bool = false { didSet { didChange() }}
|
|
|
|
open var inputId: String? { didSet { didChange() }}
|
|
|
|
open var value: AnyHashable? { didSet { didChange() }}
|
|
|
|
open var dataAnalyticsTrack: String? { didSet { didChange() }}
|
|
|
|
open var dataClickStream: String? { didSet { didChange() }}
|
|
|
|
open var dataTrack: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityHintEnabled: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityHintDisabled: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityValueEnabled: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityValueDisabled: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityLabelEnabled: String? { didSet { didChange() }}
|
|
|
|
open var accessibilityLabelDisabled: String? { didSet { didChange() }}
|
|
|
|
//functions
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
//add tapGesture to self
|
|
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
|
|
self?.sendActions(for: .touchUpInside)
|
|
}.store(in: &subscribers)
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .button
|
|
addSubview(selectorView)
|
|
|
|
selectorView.addSubview(mainStackView)
|
|
|
|
mainStackView.addArrangedSubview(selectorStackView)
|
|
|
|
selectorStackView.addArrangedSubview(selectorLeftLabelStackView)
|
|
selectorStackView.addArrangedSubview(subTextRightLabel)
|
|
selectorLeftLabelStackView.addArrangedSubview(textLabel)
|
|
selectorLeftLabelStackView.addArrangedSubview(subTextLabel)
|
|
|
|
updateSelector()
|
|
|
|
selectorView.pinToSuperView()
|
|
mainStackView.pinToSuperView(.init(top: 16, left: 16, bottom: 16, right: 16))
|
|
}
|
|
|
|
func updateLabels() {
|
|
|
|
//add the stackview to hold the 2 labels
|
|
//text label
|
|
textLabel.text = text
|
|
textLabel.surface = surface
|
|
textLabel.disabled = disabled
|
|
textLabel.attributes = textAttributes
|
|
|
|
//subText label
|
|
if let subText {
|
|
subTextLabel.text = subText
|
|
subTextLabel.surface = surface
|
|
subTextLabel.disabled = disabled
|
|
subTextLabel.attributes = subTextAttributes
|
|
subTextLabel.isHidden = false
|
|
|
|
} else if subTextAttributedText != nil {
|
|
subTextLabel.isHidden = false
|
|
|
|
} else {
|
|
subTextLabel.isHidden = true
|
|
}
|
|
|
|
//subTextRight label
|
|
if let subTextRight {
|
|
subTextRightLabel.text = subTextRight
|
|
subTextRightLabel.surface = surface
|
|
subTextRightLabel.disabled = disabled
|
|
subTextRightLabel.attributes = subTextRightAttributes
|
|
subTextRightLabel.isHidden = false
|
|
|
|
} else if subTextAttributedText != nil {
|
|
subTextRightLabel.isHidden = false
|
|
|
|
} else {
|
|
subTextRightLabel.isHidden = true
|
|
}
|
|
}
|
|
|
|
public override func reset() {
|
|
super.reset()
|
|
textLabel.reset()
|
|
subTextLabel.reset()
|
|
subTextRightLabel.reset()
|
|
|
|
textLabel.typograpicalStyle = .BoldBodyLarge
|
|
subTextLabel.typograpicalStyle = .BodyLarge
|
|
subTextRightLabel.typograpicalStyle = .BodyLarge
|
|
|
|
text = "Default Text"
|
|
textAttributes = nil
|
|
textAttributedText = nil
|
|
subText = nil
|
|
subTextAttributes = nil
|
|
subTextAttributedText = nil
|
|
subTextRight = nil
|
|
subTextRightAttributes = nil
|
|
subTextRightAttributedText = nil
|
|
strikethrough = false
|
|
inputId = nil
|
|
value = nil
|
|
dataAnalyticsTrack = nil
|
|
dataClickStream = nil
|
|
dataTrack = nil
|
|
accessibilityHintEnabled = nil
|
|
accessibilityHintDisabled = nil
|
|
accessibilityValueEnabled = nil
|
|
accessibilityValueDisabled = nil
|
|
accessibilityLabelEnabled = nil
|
|
accessibilityLabelDisabled = nil
|
|
|
|
isSelected = false
|
|
|
|
updateSelector()
|
|
setAccessibilityLabel()
|
|
}
|
|
|
|
/// This will radioBox the state of the Selector and execute the actionBlock if provided.
|
|
open func toggle() {
|
|
//removed error
|
|
isSelected.toggle()
|
|
sendActions(for: .valueChanged)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - State
|
|
//--------------------------------------------------
|
|
open override func updateView() {
|
|
updateLabels()
|
|
updateSelector()
|
|
setAccessibilityHint()
|
|
setAccessibilityValue(isSelected)
|
|
setAccessibilityLabel(isSelected)
|
|
setNeedsDisplay()
|
|
}
|
|
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration Properties
|
|
//--------------------------------------------------
|
|
private var strikeThroughLineThickness: CGFloat = 1.0
|
|
private var selectorCornerRadius: CGFloat = 4.0
|
|
private var selectorBorderWidthSelected: CGFloat = 2.0
|
|
private var selectorBorderWidth: CGFloat = 1.0
|
|
|
|
private var radioBoxBackgroundColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
|
|
$0.forFalse.enabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
|
$0.forFalse.enabled.darkColor = VDSFormControlsColor.backgroundOndark
|
|
$0.forFalse.disabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
|
$0.forFalse.disabled.darkColor = VDSFormControlsColor.backgroundOndark
|
|
|
|
$0.forTrue.enabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
|
$0.forTrue.enabled.darkColor = VDSFormControlsColor.backgroundOndark
|
|
$0.forTrue.disabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
|
$0.forTrue.disabled.darkColor = VDSFormControlsColor.backgroundOndark
|
|
}
|
|
|
|
private var radioBoxBorderColorConfiguration = BinaryDisabledSurfaceColorConfiguration().with {
|
|
$0.forTrue.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
|
$0.forTrue.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
|
$0.forFalse.enabled.lightColor = VDSFormControlsColor.borderOnlight
|
|
$0.forFalse.enabled.darkColor = VDSFormControlsColor.borderOndark
|
|
|
|
$0.forTrue.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
$0.forFalse.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
|
$0.forFalse.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - RadioBox View Updates
|
|
//--------------------------------------------------
|
|
/// Manages the appearance of the radioBox.
|
|
|
|
private var shapeLayer: CAShapeLayer?
|
|
|
|
open func updateSelector() {
|
|
//get the colors
|
|
let backgroundColor = radioBoxBackgroundColorConfiguration.getColor(self)
|
|
let borderColor = radioBoxBorderColorConfiguration.getColor(self)
|
|
let borderWidth = isSelected ? selectorBorderWidthSelected : selectorBorderWidth
|
|
|
|
selectorView.backgroundColor = backgroundColor
|
|
selectorView.layer.borderColor = borderColor.cgColor
|
|
selectorView.layer.cornerRadius = selectorCornerRadius
|
|
selectorView.layer.borderWidth = borderWidth
|
|
|
|
layer.setNeedsDisplay()
|
|
}
|
|
|
|
open override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
// Accounts for any size changes
|
|
layer.setNeedsDisplay()
|
|
}
|
|
|
|
open override func draw(_ layer: CALayer, in ctx: CGContext) {
|
|
|
|
let borderColor = radioBoxBorderColorConfiguration.getColor(self)
|
|
|
|
shapeLayer?.removeFromSuperlayer()
|
|
shapeLayer = nil
|
|
|
|
if strikethrough {
|
|
let bounds = selectorView.bounds
|
|
let length = max(bounds.size.height, bounds.size.width)
|
|
guard length > 0.0, shapeLayer == nil else { return }
|
|
|
|
let border = CAShapeLayer()
|
|
border.name = "strikethrough"
|
|
border.fillColor = nil
|
|
border.opacity = 1.0
|
|
border.lineWidth = strikeThroughLineThickness
|
|
border.strokeColor = borderColor.cgColor
|
|
|
|
let linePath = UIBezierPath()
|
|
|
|
let offsetPercent: CGFloat = 0.005
|
|
linePath.move(to: CGPoint(x: selectorCornerRadius, y: bounds.height * (1 - offsetPercent)))
|
|
linePath.addLine(to: CGPoint(x: bounds.width - selectorCornerRadius, y: bounds.height * offsetPercent))
|
|
border.path = linePath.cgPath
|
|
shapeLayer = border
|
|
layer.addSublayer(border)
|
|
}
|
|
}
|
|
}
|
|
|