// // RadioBoxGroup.swift // VDS // // Created by Matt Bruce on 8/23/22. // import Foundation import UIKit public class RadioBoxGroup: RadioBoxGroupBase { public override func didSelect(_ selectedControl: RadioBox) { let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first oldSelectedControl?.toggle() selectedControl.toggle() DispatchQueue.main.asyncAfter(deadline: .now() + Constants.ModelStateDebounce) { [weak self] in self?.sendActions(for: .valueChanged) } } } public class RadioBoxGroupBase>: SelectorGroupSelectedHandlerBase { //-------------------------------------------------- // 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 = .fillProportionally } else { mainStackView.axis = .horizontal mainStackView.distribution = .fillEqually } } } open override func setup() { super.setup() isAccessibilityElement = true accessibilityTraits = .button addSubview(mainStackView) ensureDevice() mainStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true mainStackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true mainStackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true mainStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true NotificationCenter.default .publisher(for: UIDevice.orientationDidChangeNotification) .sink() { [weak self] _ in UIView.animate(withDuration: 1.0) { self?.ensureDevice() } }.store(in: &subscribers) } open override func shouldUpdateView(viewModel: ModelType) -> Bool { let update = viewModel.selectedModel?.inputId != model.selectedModel?.inputId || viewModel.selectors.count != model.selectors.count || viewModel.surface != model.surface || viewModel.disabled != model.disabled return update } open override func updateView(viewModel: ModelType) { for selectorModel in viewModel.selectors { //see if view is there for the model if let foundSelectorView = findSelectorView(inputId: selectorModel.inputId) { foundSelectorView.set(with: selectorModel) } else { //create view let newSelectorView = createModelHandler(selector: selectorModel) self.selectorViews.append(newSelectorView) mainStackView.addArrangedSubview(newSelectorView) } } } }