151 lines
5.0 KiB
Swift
151 lines
5.0 KiB
Swift
//
|
|
// RadioBoxGroupViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/23/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
class RadioBoxGroupViewController: ModelViewController<DefaultRadioBoxGroupModel>, StoryboardInitable {
|
|
deinit {
|
|
print("\(Self.self) deinit")
|
|
}
|
|
|
|
enum PickerType {
|
|
case surface
|
|
}
|
|
static var storyboardId: String = "radioBoxGroup"
|
|
static var storyboardName: String = "Components"
|
|
|
|
@IBOutlet weak var componentContainerView: UIView!
|
|
@IBOutlet weak var disabledSwitch: UISwitch!
|
|
@IBOutlet weak var picker: UIPickerView!
|
|
@IBOutlet weak var surfaceLabel: UILabel!
|
|
@IBOutlet weak var textField: UITextField!
|
|
@IBOutlet weak var subTextField: UITextField!
|
|
@IBOutlet weak var subTextRightField: UITextField!
|
|
@IBOutlet weak var showErrorSwitch: UISwitch!
|
|
|
|
var radioBoxGroup = RadioBoxGroup()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
|
|
|
componentContainerView.addSubview(radioBoxGroup)
|
|
radioBoxGroup.leadingAnchor.constraint(equalTo: componentContainerView.leadingAnchor, constant: 10).isActive = true
|
|
radioBoxGroup.topAnchor.constraint(equalTo: componentContainerView.topAnchor, constant: 20).isActive = true
|
|
radioBoxGroup.bottomAnchor.constraint(equalTo: componentContainerView.bottomAnchor, constant: -20).isActive = true
|
|
radioBoxGroup.trailingAnchor.constraint(equalTo: componentContainerView.trailingAnchor, constant: 10).isActive = true
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
func setupModel(){
|
|
var defaultModel = DefaultRadioBoxGroupModel()
|
|
var model1 = DefaultRadioBoxModel()
|
|
model1.value = "model 1 Value"
|
|
model1.text = "iPhone 11 Bundle 1"
|
|
model1.subText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
|
|
model1.subTextRight = "Right Text"
|
|
|
|
var model2 = DefaultRadioBoxModel()
|
|
model2.strikethrough = true
|
|
model2.value = "model 2 Value"
|
|
model2.text = "iPhone 11 Bundle 2"
|
|
model2.subText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
|
|
|
|
defaultModel.selectors = [model1, model2]
|
|
set(with: defaultModel)
|
|
|
|
//update the model
|
|
radioBoxGroup
|
|
.handlerPublisher()
|
|
.sink { [weak self] updatedModel in
|
|
self?.model = updatedModel
|
|
self?.showErrorSwitch.isOn = updatedModel.hasError
|
|
self?.disabledSwitch.isOn = updatedModel.disabled
|
|
}
|
|
.store(in: &subscribers)
|
|
|
|
//set UI values
|
|
surfaceLabel.text = model.surface.rawValue
|
|
disabledSwitch.isOn = model.disabled
|
|
showErrorSwitch.isOn = model.hasError
|
|
textField.text = model1.text
|
|
subTextField.text = model1.subText
|
|
subTextRightField.text = model1.subTextRight
|
|
}
|
|
|
|
override func updateView(viewModel: ModelType) {
|
|
print("\(Self.self) updateView(viewModel)")
|
|
showErrorSwitch.isOn = viewModel.hasError
|
|
disabledSwitch.isOn = viewModel.disabled
|
|
radioBoxGroup.set(with: viewModel)
|
|
}
|
|
|
|
var radioBox: RadioBox? {
|
|
radioBoxGroup.selectorViews.first
|
|
}
|
|
@IBAction func disabledChanged(_ sender: UISwitch) {
|
|
radioBoxGroup.disabled = sender.isOn
|
|
}
|
|
|
|
@IBAction func onTextDidEnd(_ sender: UITextField) {
|
|
radioBox?.text = sender.text ?? "No value entered"
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func onSubTextDidEnd(_ sender: UITextField) {
|
|
radioBox?.subText = sender.text
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func onSubTextRightDidEnd(_ sender: UITextField) {
|
|
radioBox?.subTextRight = sender.text
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func showErrorChanged(_ sender: UISwitch) {
|
|
radioBoxGroup.hasError = sender.isOn
|
|
}
|
|
|
|
@IBAction func surfaceClick(_ sender: Any) {
|
|
pickerType = .surface
|
|
}
|
|
|
|
//Picker
|
|
var surfacePicker = SurfacePicker()
|
|
|
|
var pickerType: PickerType = .surface {
|
|
didSet {
|
|
func update(object: UIPickerViewDelegate & UIPickerViewDataSource){
|
|
picker.delegate = object
|
|
picker.dataSource = object
|
|
}
|
|
|
|
switch pickerType{
|
|
case .surface:
|
|
update(object: surfacePicker)
|
|
}
|
|
picker.reloadAllComponents()
|
|
picker.selectRow(0, inComponent: 0, animated: false)
|
|
picker.isHidden = false
|
|
}
|
|
}
|
|
|
|
func setupPicker(){
|
|
picker.isHidden = true
|
|
surfacePicker.onPickerDidSelect = { [weak self] item in
|
|
self?.radioBoxGroup.surface = item
|
|
self?.componentContainerView.backgroundColor = item.color
|
|
self?.surfaceLabel.text = item.rawValue
|
|
}
|
|
}
|
|
}
|