vds_ios/VDS/Components/RadioBox/RadioBoxGroup.swift
Matt Bruce aa233515a5 updated control
and subcontrols

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-03-29 13:47:56 -05:00

83 lines
2.6 KiB
Swift

//
// RadioBoxGroup.swift
// VDS
//
// Created by Matt Bruce on 8/23/22.
//
import Foundation
import UIKit
@objc(VDSRadioBoxGroup)
public class RadioBoxGroup: SelectorGroupSelectedHandlerBase<RadioBox> {
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public override var selectorViews: [RadioBox] {
didSet {
for selector in selectorViews {
if !mainStackView.arrangedSubviews.contains(selector) {
selector.onClick = { [weak self] handler in
self?.didSelect(handler)
}
mainStackView.addArrangedSubview(selector)
}
}
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var mainStackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.spacing = 12
}
}()
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
private func ensureDevice() {
if UIDevice.isIPad {
mainStackView.axis = .horizontal
mainStackView.distribution = .fillEqually
} else {
if UIDevice.current.orientation.isPortrait || UIDevice.current.orientation == .unknown {
mainStackView.axis = .vertical
mainStackView.distribution = .fill
} else {
mainStackView.axis = .horizontal
mainStackView.distribution = .fillEqually
}
}
}
open override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .button
addSubview(mainStackView)
ensureDevice()
mainStackView.pinToSuperView()
NotificationCenter.default
.publisher(for: UIDevice.orientationDidChangeNotification)
.sink() { [weak self] _ in
UIView.animate(withDuration: 1.0) {
self?.ensureDevice()
}
}.store(in: &subscribers)
}
open override func didSelect(_ selectedControl: RadioBox) {
let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first
oldSelectedControl?.toggle()
selectedControl.toggle()
valueChanged()
}
}