vds_ios/VDS/Components/RadioBox/RadioBoxGroup.swift
Matt Bruce 77a02a1e25 moved valueChanged to method
again this is due to the updating internal models via the control, therefore there will be a delay in the eventing from the publisher back up to the parent model

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-14 11:59:01 -05:00

107 lines
3.8 KiB
Swift

//
// RadioBoxGroup.swift
// VDS
//
// Created by Matt Bruce on 8/23/22.
//
import Foundation
import UIKit
public class RadioBoxGroup: RadioBoxGroupBase<DefaultRadioBoxGroupModel, RadioBox> {
public override func didSelect(_ selectedControl: RadioBox) {
let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first
oldSelectedControl?.toggle()
selectedControl.toggle()
valueChanged()
}
}
public class RadioBoxGroupBase<GroupModelType: RadioBoxGroupModel, ModelHandlerType: RadioBoxBase<GroupModelType.SelectorModelType>>: SelectorGroupSelectedHandlerBase<GroupModelType, ModelHandlerType> {
//--------------------------------------------------
// 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)
}
}
}
public var selectedModelHandler: ModelHandlerType? {
if let index = selectorViews.firstIndex(where: { element in
return element.isSelected == true
}) {
return selectorViews[index]
} else {
return nil
}
}
}