vds_ios_sample/VDSSample/ViewControllers/CheckboxViewController.swift
Matt Bruce 46f857e4a8 implement with CurrentValueSubject
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-15 16:47:57 -05:00

148 lines
5.1 KiB
Swift

//
// CheckboxViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/1/22.
//
import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine
class CheckboxViewController: ModelViewController<DefaultCheckboxModel>, StoryboardInitable {
enum PickerType {
case surface
}
static var storyboardId: String = "checkbox"
static var storyboardName: String = "Components"
@IBOutlet weak var checkboxContainerView: 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!
@IBOutlet weak var errorTextField: UITextField!
var checkbox = Checkbox()
override func viewDidLoad() {
super.viewDidLoad()
checkboxContainerView.addSubview(checkbox)
checkbox.leadingAnchor.constraint(equalTo: checkboxContainerView.leadingAnchor, constant: 10).isActive = true
checkbox.bottomAnchor.constraint(equalTo: checkboxContainerView.bottomAnchor, constant: -20).isActive = true
checkbox.topAnchor.constraint(equalTo: checkboxContainerView.topAnchor, constant: 20).isActive = true
checkbox.trailingAnchor.constraint(equalTo: checkboxContainerView.trailingAnchor, constant: 10).isActive = true
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
setupPicker()
createModel()
setupBinding()
}
func createModel() {
var model = DefaultCheckboxModel()
model.labelText = "Terms and conditions"
model.childText = "I agree to Verizon's terms and conditions click here"
model.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!)
]
model.errorText = "Error Text"
checkbox.set(with: model)
//setup UI
surfaceLabel.text = model.surface.rawValue
disabledSwitch.isOn = model.selected
labelTextField.text = model.labelText
childTextField.text = model.childText
showErrorSwitch.isOn = model.hasError
errorTextField.text = model.errorText
}
func setupBinding() {
checkbox.createBinding(with: model, storeIn: &subscribers)
//print out on subject changes
model
.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main)
.sink { [weak self] updatedModel in
self?.showErrorSwitch.isOn = updatedModel.hasError
print("CheckboxViewController local hasError: \(self?.model.value.hasError)")
print("CheckboxViewController hasError: \(updatedModel.hasError)")
print("CheckboxViewController local selected: \(self?.model.value.selected)")
print("CheckboxViewController selected: \(updatedModel.selected)")
}
.store(in: &subscribers)
}
@IBAction func disabledChanged(_ sender: UISwitch) {
checkbox.disabled = sender.isOn
}
@IBAction func onLabelTextDidEnd(_ sender: UITextField) {
checkbox.labelText = sender.text
sender.resignFirstResponder()
}
@IBAction func onChildTextDidEnd(_ sender: UITextField) {
checkbox.childText = sender.text
sender.resignFirstResponder()
}
@IBAction func showErrorChanged(_ sender: UISwitch) {
checkbox.hasError = sender.isOn
}
@IBAction func onErrorTextDidEnd(_ sender: UITextField) {
checkbox.errorText = sender.text
sender.resignFirstResponder()
}
@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?.checkbox.surface = item
self?.checkboxContainerView.backgroundColor = item.color
self?.surfaceLabel.text = item.rawValue
}
}
}