vds_ios_sample/VDSSample/ViewControllers/RadioBoxGroupViewController.swift
Matt Bruce ae4a63bfcc moved picker to base scrollview
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-24 12:04:44 -05:00

338 lines
11 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
// }
// }
//}
class RadioBoxGroupViewController: ModelScrollViewController<DefaultRadioBoxGroupModel> {
deinit {
print("\(Self.self) deinit")
}
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
}
}
}