vds_ios/VDS/Components/RadioBox/RadioBoxGroup.swift
Matt Bruce bdbc63e677 refactored name to ReadioBoxItem
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-06-05 10:59:24 -05:00

146 lines
5.2 KiB
Swift

//
// RadioBoxGroup.swift
// VDS
//
// Created by Matt Bruce on 8/23/22.
//
import Foundation
import UIKit
@objc(VDSRadioBoxGroup)
open class RadioBoxGroup: SelectorGroupSelectedHandlerBase<RadioBoxItem> {
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public override var selectorViews: [RadioBoxItem] {
willSet {
mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
}
didSet {
for selector in selectorViews {
selector.onClick = { [weak self] handler in
self?.didSelect(handler)
}
mainStackView.addArrangedSubview(selector)
}
}
}
public var selectorModels: [RadioBoxModel]? {
didSet {
if let selectorModels {
selectorViews = selectorModels.map { model in
return RadioBoxItem().with {
$0.accessibilityLabel = model.accessibileText
$0.text = model.text
$0.textAttributes = model.textAttributes
$0.subText = model.subText
$0.subTextAttributes = model.subTextAttributes
$0.subTextRight = model.subText
$0.subTextRightAttributes = model.subTextAttributes
$0.disabled = model.disabled
$0.inputId = model.inputId
$0.isSelected = model.selected
}
}
}
}
}
//--------------------------------------------------
// 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: RadioBoxItem) {
let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first
oldSelectedControl?.toggle()
selectedControl.toggle()
valueChanged()
}
}
extension RadioBoxGroup {
public struct RadioBoxModel: Surfaceable, Initable, Disabling, FormFieldable {
public var disabled: Bool
public var surface: Surface
public var inputId: String?
public var value: AnyHashable?
public var accessibileText: String?
public var text: String
public var textAttributes: [any LabelAttributeModel]?
public var subText: String?
public var subTextAttributes: [any LabelAttributeModel]?
public var subTextRight: String?
public var subTextRightAttributes: [any LabelAttributeModel]?
public var selected: Bool
public init(disabled: Bool, surface: Surface = .light, inputId: String? = nil, value: AnyHashable? = nil,
text: String = "", textAttributes: [any LabelAttributeModel]? = nil,
subText: String? = nil, subTextAttributes: [any LabelAttributeModel]? = nil,
subTextRight: String? = nil, subTextRightAttributes: [any LabelAttributeModel]? = nil,
selected: Bool = false, errorText: String? = nil, accessibileText: String? = nil) {
self.disabled = disabled
self.surface = surface
self.inputId = inputId
self.value = value
self.text = text
self.textAttributes = textAttributes
self.subText = subText
self.subTextAttributes = subTextAttributes
self.subTextRight = subTextRight
self.subTextRightAttributes = subTextRightAttributes
self.selected = selected
self.accessibileText = accessibileText
}
public init() {
self.init(disabled: false)
}
}
}