227 lines
8.4 KiB
Swift
227 lines
8.4 KiB
Swift
//
|
|
// ToggleView.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/19/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import Combine
|
|
/**
|
|
A custom implementation of Apple's UISwitch.
|
|
|
|
By default this class begins in the off state.
|
|
|
|
Container: The background of the toggle control.
|
|
Knob: The circular indicator that slides on the container.
|
|
*/
|
|
@objc(VDSToggleView)
|
|
open class ToggleView: Control, Changeable {
|
|
|
|
//--------------------------------------------------
|
|
// 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)
|
|
}
|
|
|
|
private var toggleView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.isUserInteractionEnabled = false
|
|
}
|
|
|
|
private var knobView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.backgroundColor = .white
|
|
$0.isUserInteractionEnabled = false
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration Properties
|
|
//--------------------------------------------------
|
|
// Sizes are from InVision design specs.
|
|
public let toggleSize = CGSize(width: 52, height: 28)
|
|
public let knobSize = CGSize(width: 24, height: 24)
|
|
|
|
private var toggleColorConfiguration = ControlColorConfiguration().with {
|
|
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.paletteGray44, forState: .normal)
|
|
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
|
|
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: [.selected, .disabled])
|
|
$0.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen36, forState: .selected)
|
|
}
|
|
|
|
private var knobColorConfiguration = ControlColorConfiguration().with {
|
|
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOndark, forState: .normal)
|
|
$0.setSurfaceColors(VDSColor.paletteGray95, VDSColor.paletteGray44, forState: .disabled)
|
|
$0.setSurfaceColors(VDSColor.paletteGray95, VDSColor.paletteGray44, forState: [.selected, .disabled])
|
|
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOndark, forState: .selected)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public var onChangeSubscriber: AnyCancellable?
|
|
|
|
open var isOn: Bool {
|
|
get { isSelected }
|
|
set {
|
|
if isSelected != newValue {
|
|
isSelected = newValue
|
|
}
|
|
setNeedsUpdate()
|
|
}
|
|
}
|
|
|
|
open var isAnimated: Bool = true { didSet { setNeedsUpdate() }}
|
|
|
|
open var inputId: String? { didSet { setNeedsUpdate() }}
|
|
|
|
open var value: AnyHashable? { didSet { setNeedsUpdate() }}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Constraints
|
|
//--------------------------------------------------
|
|
private var knobLeadingConstraint: NSLayoutConstraint?
|
|
private var knobTrailingConstraint: NSLayoutConstraint?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
open override func initialSetup() {
|
|
super.initialSetup()
|
|
onClick = { control in
|
|
control.toggle()
|
|
}
|
|
}
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .button
|
|
|
|
addSubview(toggleView)
|
|
toggleView.addSubview(knobView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
toggleView.widthAnchor.constraint(equalToConstant: toggleSize.width),
|
|
toggleView.heightAnchor.constraint(equalToConstant: toggleSize.height),
|
|
toggleView.centerYAnchor.constraint(equalTo: centerYAnchor),
|
|
knobView.heightAnchor.constraint(equalToConstant: knobSize.height),
|
|
knobView.widthAnchor.constraint(equalToConstant: knobSize.width),
|
|
knobView.centerYAnchor.constraint(equalTo: toggleView.centerYAnchor),
|
|
knobView.topAnchor.constraint(greaterThanOrEqualTo: toggleView.topAnchor)
|
|
])
|
|
|
|
// Set cornerRadius
|
|
knobView.layer.cornerRadius = knobSize.height / 2.0
|
|
toggleView.layer.cornerRadius = toggleSize.height / 2.0
|
|
|
|
// Set content hugging priority
|
|
toggleView.setContentHuggingPriority(.required, for: .horizontal)
|
|
toggleView.setContentHuggingPriority(.required, for: .vertical)
|
|
setContentHuggingPriority(.required, for: .horizontal)
|
|
setContentHuggingPriority(.required, for: .vertical)
|
|
}
|
|
|
|
open override var intrinsicContentSize: CGSize { toggleSize }
|
|
|
|
/// Resets back to this objects default settings.
|
|
open override func reset() {
|
|
super.reset()
|
|
isOn = false
|
|
isAnimated = true
|
|
inputId = nil
|
|
value = nil
|
|
toggleView.backgroundColor = toggleColorConfiguration.getColor(self)
|
|
knobView.backgroundColor = knobColorConfiguration.getColor(self)
|
|
}
|
|
|
|
/// This will toggle the state of the Toggle and execute the actionBlock if provided.
|
|
open func toggle() {
|
|
isOn.toggle()
|
|
sendActions(for: .valueChanged)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Toggle
|
|
//--------------------------------------------------
|
|
private func constrainKnob(){
|
|
self.knobLeadingConstraint?.isActive = false
|
|
self.knobTrailingConstraint?.isActive = false
|
|
if isOn {
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
|
} else {
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: knobView.trailingAnchor)
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(equalTo: toggleView.leadingAnchor, constant: 2)
|
|
}
|
|
knobTrailingConstraint?.isActive = true
|
|
knobLeadingConstraint?.isActive = true
|
|
self.layoutIfNeeded()
|
|
}
|
|
|
|
private func updateToggle() {
|
|
let toggleColor = toggleColorConfiguration.getColor(self)
|
|
let knobColor = knobColorConfiguration.getColor(self)
|
|
|
|
if disabled || !isAnimated {
|
|
toggleView.backgroundColor = toggleColor
|
|
knobView.backgroundColor = knobColor
|
|
constrainKnob()
|
|
} else {
|
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
|
|
self.toggleView.backgroundColor = toggleColor
|
|
self.knobView.backgroundColor = knobColor
|
|
}, completion: nil)
|
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: { [weak self] in
|
|
self?.constrainKnob()
|
|
}, completion: nil)
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
open override func updateView() {
|
|
updateToggle()
|
|
updateAccessibilityLabel()
|
|
}
|
|
|
|
open override func updateAccessibilityLabel() {
|
|
accessibilityLabel = "Toggle"
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: AppleGuidlinesTouchable
|
|
extension ToggleView: AppleGuidlinesTouchable {
|
|
|
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
Self.acceptablyOutsideBounds(point: point, bounds: bounds)
|
|
}
|
|
|
|
}
|