111 lines
3.6 KiB
Swift
111 lines
3.6 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: BaseViewController {
|
|
|
|
var disabledSwitch = Toggle()
|
|
var labelTextField = TextField()
|
|
var childTextField = TextField()
|
|
var showErrorSwitch = Toggle()
|
|
|
|
var checkboxGroup = CheckboxGroup()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
addContentTopView(view: checkboxGroup)
|
|
|
|
setupForm()
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
override func allTextFields() -> [TextField]? { [labelTextField, childTextField] }
|
|
|
|
func setupForm() {
|
|
addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
addFormRow(label: "Label Text", view: labelTextField)
|
|
addFormRow(label: "Child Text", view: childTextField)
|
|
addFormRow(label: "Error", view: .makeWrapper(for: showErrorSwitch))
|
|
|
|
showErrorSwitch.onChange = { [weak self] sender in
|
|
self?.checkboxGroup.showError = sender.isOn
|
|
}
|
|
|
|
disabledSwitch.onChange = { [weak self] sender in
|
|
self?.checkboxGroup.disabled = 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)
|
|
}
|
|
|
|
func setupModel() {
|
|
let checkbox1 = Checkbox()
|
|
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"
|
|
|
|
let checkbox2 = Checkbox()
|
|
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"
|
|
|
|
checkboxGroup.selectorViews = [checkbox1, checkbox2]
|
|
|
|
checkboxGroup.onChange = { [weak self] group in
|
|
let selected = group.selectedHandlers?
|
|
.compactMap{"\($0.labelText!)"}
|
|
.joined(separator: "\r") ?? "none selected"
|
|
|
|
let alertController:UIAlertController = UIAlertController(title: "Alert",
|
|
message: "Selected Checkboxes:\r\(selected)",
|
|
preferredStyle: UIAlertController.Style.alert)
|
|
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil))
|
|
self?.present(alertController, animated: true)
|
|
}
|
|
|
|
//setup UI
|
|
surfacePickerSelectorView.text = checkboxGroup.surface.rawValue
|
|
disabledSwitch.isOn = checkboxGroup.disabled
|
|
labelTextField.text = checkbox1.labelText
|
|
childTextField.text = checkbox1.childText
|
|
showErrorSwitch.isOn = checkboxGroup.showError
|
|
|
|
}
|
|
|
|
var checkbox: Checkbox? {
|
|
checkboxGroup.selectorViews.first
|
|
}
|
|
|
|
//Picker
|
|
func setupPicker(){
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.checkboxGroup.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
}
|
|
}
|
|
|
|
}
|