148 lines
5.1 KiB
Swift
148 lines
5.1 KiB
Swift
//
|
|
// RadioButtonViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/1/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
class RadioButtonViewController: ModelViewController<DefaultRadioButtonGroupModel>, StoryboardInitable {
|
|
deinit {
|
|
print("\(Self.self) deinit")
|
|
}
|
|
|
|
enum PickerType {
|
|
case surface
|
|
}
|
|
static var storyboardId: String = "radioButton"
|
|
static var storyboardName: String = "Components"
|
|
|
|
@IBOutlet weak var componentContainerView: UIView!
|
|
@IBOutlet weak var picker: UIPickerView!
|
|
@IBOutlet weak var surfaceLabel: UILabel!
|
|
|
|
@IBOutlet weak var disabledSwitch: UISwitch!
|
|
@IBOutlet weak var labelTextField: UITextField!
|
|
@IBOutlet weak var childTextField: UITextField!
|
|
@IBOutlet weak var showErrorSwitch: UISwitch!
|
|
|
|
var radioButtonGroup = RadioButtonGroup()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
|
|
|
componentContainerView.addSubview(radioButtonGroup)
|
|
radioButtonGroup.leadingAnchor.constraint(equalTo: componentContainerView.leadingAnchor, constant: 10).isActive = true
|
|
radioButtonGroup.topAnchor.constraint(equalTo: componentContainerView.topAnchor, constant: 20).isActive = true
|
|
radioButtonGroup.bottomAnchor.constraint(equalTo: componentContainerView.bottomAnchor, constant: -20).isActive = true
|
|
radioButtonGroup.trailingAnchor.constraint(equalTo: componentContainerView.trailingAnchor, constant: 10).isActive = true
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
func setupModel(){
|
|
var defaultModel = DefaultRadioButtonGroupModel()
|
|
var model1 = DefaultRadioButtonModel()
|
|
model1.value = "model 1 Value"
|
|
model1.labelText = "Terms and conditions"
|
|
model1.childText = "I agree to Verizon's terms and conditions click here"
|
|
model1.childTextAttributes = [
|
|
LabelAttributeUnderline(location: 11, length: 10),
|
|
LabelAttributeStrikeThrough(location: 22, length: 5),
|
|
LabelAttributeColor(location: 31, length: 10, color: UIColor.blue.hexString!),
|
|
LabelAttributeActionModel(location: 31, length: 10){ print("clicked on the word 'conditions'") },
|
|
LabelAttributeFont(location: 2, length: 5, style: .BoldTitleLarge, color: UIColor.red.hexString!)
|
|
]
|
|
|
|
var model2 = DefaultRadioButtonModel()
|
|
model2.value = "model 2 Value"
|
|
model2.childText = "Radio Sample 2"
|
|
defaultModel.selectors = [model1, model2]
|
|
model = defaultModel
|
|
|
|
//update the model
|
|
radioButtonGroup
|
|
.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
|
|
labelTextField.text = model1.labelText
|
|
childTextField.text = model1.childText
|
|
}
|
|
|
|
override func updateView(viewModel: DefaultRadioButtonGroupModel) {
|
|
print("\(Self.self) updateView(viewModel)")
|
|
showErrorSwitch.isOn = viewModel.hasError
|
|
disabledSwitch.isOn = viewModel.disabled
|
|
radioButtonGroup.set(with: viewModel)
|
|
}
|
|
|
|
var radioButton: RadioButton? {
|
|
radioButtonGroup.selectorViews.first
|
|
}
|
|
@IBAction func disabledChanged(_ sender: UISwitch) {
|
|
radioButtonGroup.disabled = sender.isOn
|
|
}
|
|
|
|
@IBAction func onLabelTextDidEnd(_ sender: UITextField) {
|
|
radioButton?.labelText = sender.text
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func onChildTextDidEnd(_ sender: UITextField) {
|
|
radioButton?.childText = sender.text
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func showErrorChanged(_ sender: UISwitch) {
|
|
radioButtonGroup.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?.radioButtonGroup.surface = item
|
|
self?.componentContainerView.backgroundColor = item.color
|
|
self?.surfaceLabel.text = item.rawValue
|
|
}
|
|
}
|
|
}
|