88 lines
2.8 KiB
Swift
88 lines
2.8 KiB
Swift
//
|
|
// RadioBoxGroup.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/23/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
@objc(VDSRadioBoxGroup)
|
|
public class RadioBoxGroup: RadioBoxGroupBase<RadioBox> {
|
|
|
|
public override func didSelect(_ selectedControl: RadioBox) {
|
|
let oldSelectedControl = selectorViews.filter { $0.isSelected == true }.first
|
|
oldSelectedControl?.toggle()
|
|
selectedControl.toggle()
|
|
valueChanged()
|
|
}
|
|
}
|
|
|
|
public class RadioBoxGroupBase<HandlerType: RadioBoxBase>: SelectorGroupSelectedHandlerBase<HandlerType> {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public override var selectorViews: [HandlerType] {
|
|
didSet {
|
|
for selector in selectorViews {
|
|
if !mainStackView.arrangedSubviews.contains(selector) {
|
|
selector
|
|
.publisher(for: .touchUpInside)
|
|
.sink { [weak self] handler in
|
|
self?.didSelect(handler)
|
|
}.store(in: &subscribers)
|
|
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)
|
|
}
|
|
}
|