vds_ios_sample/VDSSample/ViewControllers/CheckBoxGroupViewController.swift
Matt Bruce 6cd6685da4 refactored hasError to showError
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-10-03 12:21:50 -05:00

127 lines
3.9 KiB
Swift

//
// CheckBoxGroup2ViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/24/22.
//
import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine
class CheckboxGroupViewController: ModelScrollViewController<DefaultCheckboxGroupModel> {
var disabledSwitch = UISwitch()
var labelTextField = TextField()
var childTextField = TextField()
var showErrorSwitch = UISwitch()
var checkboxGroup = CheckboxGroup()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: checkboxGroup)
setupForm()
setupPicker()
setupModel()
}
func setupForm() {
addFormRow(label: "Disabled", view: disabledSwitch)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Label Text", view: labelTextField)
addFormRow(label: "Child Text", view: childTextField)
addFormRow(label: "Error", view: showErrorSwitch)
showErrorSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.checkboxGroup.showError = sender.isOn
}.store(in: &subscribers)
disabledSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.checkboxGroup.disabled = sender.isOn
}.store(in: &subscribers)
labelTextField
.textPublisher
.sink { [weak self] text in
self?.checkbox?.labelText = text
}.store(in: &subscribers)
childTextField
.textPublisher
.sink { [weak self] text in
self?.checkbox?.childText = text
}.store(in: &subscribers)
}
func setupModel() {
var defaultModel = DefaultCheckboxGroupModel()
var model1 = DefaultCheckboxModel()
model1.inputId = "model1"
model1.value = "model 1 Value"
model1.labelText = "iPhone 11 Bundle 1"
model1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
var model2 = DefaultCheckboxModel()
model2.inputId = "model2"
model2.value = "model 2 Value"
model2.labelText = "iPhone 11 Bundle 2"
model2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
defaultModel.selectors = [model1, model2]
set(with: defaultModel)
checkboxGroup
.handlerPublisher()
.sink { [weak self] viewModel in
self?.model = viewModel
}.store(in: &subscribers)
checkboxGroup
.publisher(for: .valueChanged)
.sink { group in
let selected = group.selectedModelHandlers?
.compactMap{$0.labelText}
.joined(separator: ", ") ?? "none"
print("Selected: \(selected)")
}.store(in: &subscribers)
//setup UI
surfacePickerSelectorView.text = model.surface.rawValue
disabledSwitch.isOn = model.disabled
labelTextField.text = model2.labelText
childTextField.text = model1.childText
showErrorSwitch.isOn = model.showError
}
override func updateView(viewModel: DefaultCheckboxGroupModel) {
print("\(Self.self) updateView(viewModel)")
showErrorSwitch.isOn = viewModel.showError
disabledSwitch.isOn = viewModel.disabled
checkboxGroup.set(with: viewModel)
}
var checkbox: Checkbox? {
checkboxGroup.selectorViews.first
}
//Picker
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.checkboxGroup.surface = item
self?.contentTopView.backgroundColor = item.color
self?.surfacePickerSelectorView.text = item.rawValue
}
}
}