270 lines
8.6 KiB
Swift
270 lines
8.6 KiB
Swift
//
|
|
// Toggle.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
@objc(VDSToggle)
|
|
open class Toggle: Control, Changeable {
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
public enum TextSize: String, CaseIterable {
|
|
case small, large
|
|
}
|
|
|
|
public enum TextWeight: String, CaseIterable {
|
|
case regular, bold
|
|
}
|
|
|
|
public enum TextPosition: String, CaseIterable {
|
|
case left, right
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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: - Constraints
|
|
//--------------------------------------------------
|
|
private var leftConstraints: [NSLayoutConstraint] = []
|
|
private var rightConstraints: [NSLayoutConstraint] = []
|
|
private var labelConstraints: [NSLayoutConstraint] = []
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration Properties
|
|
//--------------------------------------------------
|
|
private let toggleContainerSize = CGSize(width: 52, height: 44)
|
|
private let spacingBetween = VDSLayout.Spacing.space3X.value
|
|
private let labelMaxWidth = 40.0
|
|
|
|
private var textStyle: TextStyle {
|
|
if textSize == .small {
|
|
if textWeight == .bold {
|
|
return .boldBodySmall
|
|
} else {
|
|
return .bodySmall
|
|
}
|
|
} else {
|
|
if textWeight == .bold {
|
|
return .boldBodyLarge
|
|
} else {
|
|
return .bodyLarge
|
|
}
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
open var onChangeSubscriber: AnyCancellable? {
|
|
willSet {
|
|
if let onChangeSubscriber {
|
|
onChangeSubscriber.cancel()
|
|
}
|
|
}
|
|
}
|
|
|
|
open var toggleView = ToggleView().with {
|
|
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
|
|
$0.isUserInteractionEnabled = false
|
|
}
|
|
|
|
open var label = Label().with {
|
|
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
|
|
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
|
|
|
$0.textColorConfiguration = ViewColorConfiguration().with {
|
|
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forDisabled: true)
|
|
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false)
|
|
}.eraseToAnyColorable()
|
|
}
|
|
|
|
open var isOn: Bool {
|
|
get { isSelected }
|
|
set {
|
|
if isSelected != newValue {
|
|
toggleView.isOn = newValue
|
|
isSelected = newValue
|
|
}
|
|
}
|
|
}
|
|
|
|
open var isAnimated: Bool = true { didSet { setNeedsUpdate() }}
|
|
|
|
open var showText: Bool = false { didSet { setNeedsUpdate() }}
|
|
|
|
open var onText: String = "On" { didSet { setNeedsUpdate() }}
|
|
|
|
open var offText: String = "Off" { didSet { setNeedsUpdate() }}
|
|
|
|
open var statusText: String { isOn ? onText : offText }
|
|
|
|
open var textSize: TextSize = .small { didSet { setNeedsUpdate() }}
|
|
|
|
open var textWeight: TextWeight = .regular { didSet { setNeedsUpdate() }}
|
|
|
|
open var textPosition: TextPosition = .left { didSet { setNeedsUpdate() }}
|
|
|
|
open var inputId: String? { didSet { setNeedsUpdate() }}
|
|
|
|
open var value: AnyHashable? { didSet { setNeedsUpdate() }}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
open override func initialSetup() {
|
|
super.initialSetup()
|
|
onClick = { control in
|
|
control.toggle()
|
|
}
|
|
}
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .button
|
|
addSubview(label)
|
|
addSubview(toggleView)
|
|
|
|
let heightEqual = heightAnchor.constraint(equalToConstant: toggleContainerSize.height)
|
|
heightEqual.priority = .defaultLow
|
|
|
|
let heightGreater = heightAnchor.constraint(greaterThanOrEqualToConstant: toggleContainerSize.height)
|
|
heightGreater.priority = .defaultHigh
|
|
|
|
// Set up initial constraints for label and switch
|
|
toggleView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
|
|
|
labelConstraints = [
|
|
heightEqual, heightGreater,
|
|
label.widthAnchor.constraint(lessThanOrEqualToConstant: labelMaxWidth),
|
|
label.topAnchor.constraint(equalTo: topAnchor),
|
|
label.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
]
|
|
|
|
leftConstraints = [
|
|
toggleView.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: spacingBetween)
|
|
]
|
|
|
|
rightConstraints = [
|
|
label.leadingAnchor.constraint(equalTo: toggleView.trailingAnchor, constant: spacingBetween)
|
|
]
|
|
|
|
}
|
|
|
|
/// Resets back to this objects default settings.
|
|
open override func reset() {
|
|
super.reset()
|
|
shouldUpdateView = false
|
|
label.reset()
|
|
isOn = false
|
|
isAnimated = true
|
|
showText = false
|
|
onText = "On"
|
|
offText = "Off"
|
|
textSize = .small
|
|
textWeight = .regular
|
|
textPosition = .left
|
|
inputId = nil
|
|
value = nil
|
|
shouldUpdateView = true
|
|
setNeedsUpdate()
|
|
}
|
|
|
|
/// This will toggle the state of the Toggle
|
|
open func toggle() {
|
|
isOn.toggle()
|
|
sendActions(for: .valueChanged)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Labels
|
|
//--------------------------------------------------
|
|
private var showLabel: Bool {
|
|
showText && !statusText.isEmpty
|
|
}
|
|
private func updateLabel() {
|
|
label.isHidden = !showLabel
|
|
|
|
if showLabel {
|
|
label.textPosition = textPosition == .left ? .right : .left
|
|
label.textStyle = textStyle
|
|
label.text = statusText
|
|
label.surface = surface
|
|
label.disabled = disabled
|
|
switch textPosition {
|
|
case .left:
|
|
NSLayoutConstraint.deactivate(rightConstraints)
|
|
NSLayoutConstraint.activate(leftConstraints)
|
|
case .right:
|
|
NSLayoutConstraint.deactivate(leftConstraints)
|
|
NSLayoutConstraint.activate(rightConstraints)
|
|
}
|
|
NSLayoutConstraint.activate(labelConstraints)
|
|
} else {
|
|
NSLayoutConstraint.deactivate(leftConstraints)
|
|
NSLayoutConstraint.deactivate(rightConstraints)
|
|
NSLayoutConstraint.deactivate(labelConstraints)
|
|
}
|
|
invalidateIntrinsicContentSize()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
open override func updateView() {
|
|
updateLabel()
|
|
toggleView.surface = surface
|
|
toggleView.disabled = disabled
|
|
toggleView.isOn = isOn
|
|
updateAccessibilityLabel()
|
|
}
|
|
|
|
open override func updateAccessibilityLabel() {
|
|
accessibilityValue = isSelected ? "1" : "0"
|
|
if !accessibilityTraits.contains(.selected) && isSelected {
|
|
accessibilityTraits.insert(.selected)
|
|
} else if accessibilityTraits.contains(.selected) && !isSelected{
|
|
accessibilityTraits.remove(.selected)
|
|
}
|
|
|
|
if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
|
|
accessibilityTraits.insert(.notEnabled)
|
|
} else if accessibilityTraits.contains(.notEnabled) && !isEnabled{
|
|
accessibilityTraits.remove(.notEnabled)
|
|
}
|
|
setAccessibilityLabel(for: [label])
|
|
}
|
|
|
|
open override var intrinsicContentSize: CGSize {
|
|
if showLabel {
|
|
label.sizeToFit()
|
|
let size = CGSize(width: label.frame.width + spacingBetween + toggleContainerSize.width,
|
|
height: max(toggleContainerSize.height, label.frame.height))
|
|
return size
|
|
} else {
|
|
return toggleContainerSize
|
|
}
|
|
}
|
|
}
|