// // RadioButtonViewController.swift // VDSSample // // Created by Matt Bruce on 8/1/22. // import Foundation import UIKit import VDS import VDSCoreTokens import Combine class RadioButtonGroupViewController: BaseViewController { var disabledSwitch = Toggle() var labelTextField = TextField() var childTextField = TextField() var showErrorSwitch = Toggle() override func viewDidLoad() { super.viewDidLoad() addContentTopView(view: component) setupPicker() setupModel() } override func setupForm(){ super.setupForm() addActionRow() addFormRow(label: "Disabled", view: disabledSwitch, pinTrailing: false) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) addFormRow(label: "Child Text", view: childTextField) addFormRow(label: "Error", view: showErrorSwitch, pinTrailing: false) 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?.radioButton?.labelText = text }.store(in: &subscribers) childTextField .textPublisher .sink { [weak self] text in self?.radioButton?.childText = text }.store(in: &subscribers) } func setupModel(){ var radioButton1 = RadioButtonGroup.RadioButtonItemModel() radioButton1.inputId = "model1" radioButton1.value = "model 1 Value" radioButton1.labelText = "iPhone 11 Bundle 1" radioButton1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector" var radioButton2 = RadioButtonGroup.RadioButtonItemModel() radioButton2.inputId = "model2" radioButton2.value = "model 2 Value" radioButton2.labelText = "iPhone 11 Bundle 2" radioButton2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector" var radioButton3 = RadioButtonGroup.RadioButtonItemModel() radioButton3.inputId = "model3" radioButton3.value = "model 3 Value" radioButton3.labelText = "iPhone 11 Bundle 3" radioButton3.childText = "Apple iPhone 11 - 256 GB\nOtterbox Case Black\nScreen Protector" component.selectorModels = [radioButton1, radioButton2, radioButton3] component.onChange = { [weak self] group in let newText = "Selected:\r\(group.selectedItem?.labelText ?? "none")" self?.showErrorSwitch.isOn = group.showError self?.actionLabel.text = newText } //set UI values surfacePickerSelectorView.text = component.surface.rawValue disabledSwitch.isOn = !component.isEnabled showErrorSwitch.isOn = component.showError labelTextField.text = radioButton1.labelText childTextField.text = radioButton1.childText } var radioButton: RadioButtonItem? { component.items.first } //Picker func setupPicker(){ picker.isHidden = true surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in self?.component.surface = item self?.contentTopView.backgroundColor = item.color } } } extension RadioButtonGroupViewController: ComponentSampleable { static func makeSample() -> ComponentSample { let component = Self.makeComponent() var radioButton1 = RadioButtonGroup.RadioButtonItemModel() radioButton1.inputId = "model1" radioButton1.value = "model 1 Value" radioButton1.labelText = "iPhone 11 Bundle 1" radioButton1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector" var radioButton2 = RadioButtonGroup.RadioButtonItemModel() radioButton2.inputId = "model2" radioButton2.value = "model 2 Value" radioButton2.labelText = "iPhone 11 Bundle 2" radioButton2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector" var radioButton3 = RadioButtonGroup.RadioButtonItemModel() radioButton3.inputId = "model3" radioButton3.value = "model 3 Value" radioButton3.labelText = "iPhone 11 Bundle 3" radioButton3.childText = "Apple iPhone 11 - 256 GB\nOtterbox Case Black\nScreen Protector" component.selectorModels = [radioButton1, radioButton2, radioButton3] return ComponentSample(component: component) } }