vds_ios_sample/VDSSample/ViewControllers/RadioBoxGroupViewController.swift
2022-08-24 12:20:33 -05:00

197 lines
6.1 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: ModelScrollViewController<DefaultRadioBoxGroupModel> {
enum PickerType {
case surface
}
var disabledSwitch = UISwitch()
var strikeThroughSwitch = UISwitch()
var surfacePickerSelectorView = PickerSelectorView(title: "light")
var textField = TextField()
var subTextField = TextField()
var subTextRightField = TextField()
var showErrorSwitch = UISwitch()
var radioBoxGroup = RadioBoxGroup()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: radioBoxGroup)
setupForm()
setupPicker()
setupModel()
}
func setupForm() {
addFormRow(label: "Disabled", view: disabledSwitch)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Error", view: showErrorSwitch)
addFormRow(label: "Strikethrough", view: strikeThroughSwitch)
addFormRow(label: "Text", view: textField)
addFormRow(label: "Sub Text", view: subTextField)
addFormRow(label: "Sub Text Right", view: subTextRightField)
radioBoxGroup
.handlerPublisher()
.sink { [weak self] viewModel in
self?.model = viewModel
}.store(in: &subscribers)
showErrorSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.radioBoxGroup.hasError = sender.isOn
}.store(in: &subscribers)
disabledSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.radioBoxGroup.disabled = sender.isOn
}.store(in: &subscribers)
strikeThroughSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.radioBox?.strikethrough = sender.isOn
}.store(in: &subscribers)
textField
.textPublisher
.sink { [weak self] text in
self?.radioBox?.text = text
}.store(in: &subscribers)
subTextField
.textPublisher
.sink { [weak self] text in
self?.radioBox?.subText = text
}.store(in: &subscribers)
subTextRightField
.textPublisher
.sink { [weak self] text in
self?.radioBox?.subTextRight = text
}.store(in: &subscribers)
surfacePickerSelectorView.button
.publisher(for: .touchUpInside)
.sink { [weak self] _ in
self?.pickerType = .surface
}.store(in: &subscribers)
}
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.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
surfacePickerSelectorView.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(){
surfacePicker.onPickerDidSelect = { [weak self] item in
self?.radioBoxGroup.surface = item
self?.contentTopView.backgroundColor = item.color
self?.surfacePickerSelectorView.text = item.rawValue
}
}
}