vds_ios_sample/VDSSample/ViewControllers/CheckBoxGroupViewController.swift
Matt Bruce fa89b414b6 refactored to use the 1 VDSTokens framework
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-01-23 10:11:55 -06:00

144 lines
4.6 KiB
Swift

//
// CheckBoxGroup2ViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/24/22.
//
import Foundation
import UIKit
import VDS
import VDSTokens
import Combine
class CheckboxGroupViewController: BaseViewController<CheckboxGroup> {
var disabledSwitch = Toggle()
var labelTextField = TextField()
var childTextField = TextField()
var showErrorSwitch = Toggle()
var checkboxGroup = CheckboxGroup()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: component)
setupPicker()
setupModel()
}
override func setupForm(){
super.setupForm()
addActionRow()
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.onChange = { [weak self] sender in
self?.component.showError = sender.isOn
}
disabledSwitch.onChange = { [weak self] sender in
self?.component.isEnabled = !sender.isOn
}
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)
component.onChange = { [weak self] group in
guard let label = self?.actionLabel else { return }
let selected = group.selectedItems?
.compactMap{"\($0.labelText!)"}
.joined(separator: "\r") ?? "none selected"
let newText = "Selected Checkboxes:\r\(selected) \rclicked - "
if let labelText = label.text {
let components = labelText.components(separatedBy: " - ")
let last: String = (components.last ?? "0").trimmingCharacters(in: .whitespaces)
let count = Int(last)!
label.text = "\(newText)\(count+1)"
} else {
label.text = "\(newText)1"
}
}
}
func setupModel() {
var checkbox1 = CheckboxGroup.CheckboxItemModel()
checkbox1.inputId = "model1"
checkbox1.value = "model 1 Value"
checkbox1.labelText = "iPhone 11 Bundle 1"
checkbox1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
checkbox1.errorText = "Please Choose 1"
var checkbox2 = CheckboxGroup.CheckboxItemModel()
checkbox2.inputId = "model2"
checkbox2.value = "model 2 Value"
checkbox2.labelText = "iPhone 11 Bundle 2"
checkbox2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
checkbox2.errorText = "Please Choose 2"
component.selectorModels = [checkbox1, checkbox2]
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
disabledSwitch.isOn = !component.isEnabled
labelTextField.text = checkbox1.labelText
childTextField.text = checkbox1.childText
showErrorSwitch.isOn = component.showError
}
var checkbox: CheckboxItem? {
component.items.first
}
//Picker
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.surface = item
self?.contentTopView.backgroundColor = item.color
}
}
}
extension CheckboxGroupViewController: ComponentSampleable {
static func makeSample() -> ComponentSample {
let component = Self.makeComponent()
var checkbox1 = CheckboxGroup.CheckboxItemModel()
checkbox1.inputId = "model1"
checkbox1.value = "model 1 Value"
checkbox1.labelText = "iPhone 11 Bundle 1"
checkbox1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
checkbox1.errorText = "Please Choose 1"
var checkbox2 = CheckboxGroup.CheckboxItemModel()
checkbox2.inputId = "model2"
checkbox2.value = "model 2 Value"
checkbox2.labelText = "iPhone 11 Bundle 2"
checkbox2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
checkbox2.errorText = "Please Choose 2"
component.selectorModels = [checkbox1, checkbox2]
return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual)
}
}