vds_ios_sample/VDSSample/ViewControllers/InputStepperViewController.swift

240 lines
8.6 KiB
Swift

//
// InputStepperViewController.swift
// VDSSample
//
// Created by Kanamarlapudi, Vasavi on 24/06/24.
//
import Foundation
import UIKit
import VDS
import VDSCoreTokens
import Combine
class InputStepperViewController: BaseViewController<InputStepper> {
var controlWidthTextField = TextField()
var controlWidthPercentageTextField = TextField()
var defaultValueField = NumericField()
var maxValueTextField = NumericField()
var minValueTextField = NumericField()
var trailingTextField = TextField()
var widthTextField = NumericField()
var widthPercentageTextField = NumericField()
var disabledSwitch = Toggle()
var readOnlySwitch = Toggle()
var requiredSwitch = Toggle()
var labelTextField = TextField()
var showErrorSwitch = Toggle()
var errorTextField = TextField()
var helperTextField = TextField()
var tooltipTitleTextField = TextField()
var tooltipContentTextField = TextField()
lazy var helperTextPlacementPickerSelectorView = {
PickerSelectorView(title: "", picker: self.picker, items: InputStepper.HelperTextPlacement.allCases)
}()
lazy var sizePickerSelectorView = {
PickerSelectorView(title: "", picker: self.picker, items: InputStepper.Size.allCases)
}()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: component)
setupPicker()
setupModel()
}
override func setupForm(){
super.setupForm()
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Control Width", view: controlWidthTextField)
addFormRow(label: "ControlWidth Percentage", view: controlWidthPercentageTextField)
addFormRow(label: "Default Value", view: defaultValueField)
addFormRow(label: "Max Value", view: maxValueTextField)
addFormRow(label: "Min Value", view: minValueTextField)
addFormRow(label: "Trailing Text", view: trailingTextField)
addFormRow(label: "Size", view: sizePickerSelectorView)
addFormRow(label: "Width Value", view: widthTextField)
addFormRow(label: "Width Percentage", view: widthPercentageTextField)
addFormRow(label: "Disabled", view: disabledSwitch)
addFormRow(label: "Read Only", view: readOnlySwitch)
addFormRow(label: "Required", view: requiredSwitch)
addFormRow(label: "Label Text", view: labelTextField)
addFormRow(label: "Error", view: showErrorSwitch)
addFormRow(label: "Error Text", view: errorTextField)
addFormRow(label: "Helper Text Placement", view: helperTextPlacementPickerSelectorView)
addFormRow(label: "Helper Text", view: helperTextField)
addFormRow(label: "Tooltip Title", view: tooltipTitleTextField)
addFormRow(label: "Tooltip Content", view: tooltipContentTextField)
controlWidthTextField
.textPublisher
.sink { [weak self] text in
self?.component.controlWidth = text
self?.controlWidthPercentageTextField.text = ""
self?.component.controlWidthPercentage = nil
}.store(in: &subscribers)
controlWidthPercentageTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.controlWidthPercentage = number.cgFloatValue
self?.controlWidthTextField.text = ""
} else {
self?.component.controlWidthPercentage = nil
}
}.store(in: &subscribers)
defaultValueField
.numberPublisher
.sink { [weak self] number in
self?.component.defaultValue = number?.intValue ?? 0
}.store(in: &subscribers)
maxValueTextField
.numberPublisher
.sink { [weak self] number in
self?.component.maxValue = number?.intValue
}.store(in: &subscribers)
minValueTextField
.numberPublisher
.sink { [weak self] number in
self?.component.minValue = number?.intValue
}.store(in: &subscribers)
trailingTextField
.textPublisher
.sink { [weak self] text in
self?.component.trailingText = text
}.store(in: &subscribers)
requiredSwitch.onChange = { [weak self] sender in
self?.component.isRequired = sender.isOn
}
showErrorSwitch.onChange = { [weak self] sender in
guard let self else { return }
self.component.showError = sender.isOn
if self.component.showError != sender.isOn {
self.showErrorSwitch.isOn = self.component.showError
}
}
disabledSwitch.onChange = { [weak self] sender in
self?.component.isEnabled = !sender.isOn
}
readOnlySwitch.onChange = { [weak self] sender in
self?.component.isReadOnly = sender.isOn
}
labelTextField
.textPublisher
.sink { [weak self] text in
self?.component.labelText = text
}.store(in: &subscribers)
helperTextField
.textPublisher
.sink { [weak self] text in
self?.component.helperText = text
}.store(in: &subscribers)
errorTextField
.textPublisher
.sink { [weak self] text in
self?.component.errorText = text
}.store(in: &subscribers)
widthTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.width = number.cgFloatValue
self?.widthPercentageTextField.text = ""
} else {
self?.component.width = nil
}
}.store(in: &subscribers)
widthPercentageTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.widthPercentage = number.cgFloatValue
self?.widthTextField.text = ""
} else {
self?.component.widthPercentage = nil
}
}.store(in: &subscribers)
tooltipTitleTextField
.textPublisher
.sink { [weak self] text in
self?.updateTooltip()
}.store(in: &subscribers)
tooltipContentTextField
.textPublisher
.sink { [weak self] text in
self?.updateTooltip()
}.store(in: &subscribers)
}
func updateTooltip() {
let title = tooltipTitleTextField.text ?? ""
let content = tooltipContentTextField.text ?? ""
component.tooltipModel = !title.isEmpty || !content.isEmpty ? .init(title: title,
content: content) : nil
}
//Picker
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.surface = item
self?.contentTopView.backgroundColor = item.color
}
helperTextPlacementPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.helperTextPlacement = item
}
sizePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.size = item
}
}
func setupModel() {
component.labelText = "Quantity"
component.helperText = "Add up to max lines."
component.errorText = "You must choose a number of lines before advancing to the next step."
component.defaultValue = 88
component.trailingText = ""
component.controlWidth = "auto"
trailingTextField.text = component.trailingText
controlWidthTextField.text = component.controlWidth
errorTextField.text = component.errorText
helperTextField.text = component.helperText
labelTextField.text = component.labelText
defaultValueField.text = String(component.defaultValue)
minValueTextField.text = String(component.minValue ?? 0)
maxValueTextField.text = String(component.maxValue ?? 99)
component.size = .large
sizePickerSelectorView.text = "large"
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
helperTextPlacementPickerSelectorView.text = component.helperTextPlacement.rawValue
}
}