116 lines
3.8 KiB
Swift
116 lines
3.8 KiB
Swift
//
|
|
// CheckboxViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/1/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSCoreTokens
|
|
import Combine
|
|
|
|
class CheckboxItemViewController: BaseViewController<CheckboxItem> {
|
|
|
|
var disabledSwitch = Toggle()
|
|
var labelTextField = TextField()
|
|
var childTextField = TextField()
|
|
var errorTextField = 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)
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
addFormRow(label: "Label Text", view: labelTextField)
|
|
addFormRow(label: "Child Text", view: childTextField)
|
|
addFormRow(label: "Error", view: showErrorSwitch)
|
|
addFormRow(label: "Error Text", view: errorTextField)
|
|
|
|
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
|
|
}
|
|
|
|
labelTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.labelText = text
|
|
}.store(in: &subscribers)
|
|
|
|
childTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.childText = text
|
|
}.store(in: &subscribers)
|
|
|
|
errorTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.errorText = text
|
|
}.store(in: &subscribers)
|
|
|
|
component.onChangeActionPublisher("Checkbox", label: actionLabel)
|
|
}
|
|
|
|
func setupModel() {
|
|
let fullText = "I accept the Terms and Conditions"
|
|
let linkText = "Terms and Conditions"
|
|
component.errorText = "Error Text"
|
|
component.childText = fullText
|
|
|
|
if let link = ActionLabelAttribute(text: fullText, linkText: linkText) {
|
|
link.action.sink { [weak self] in
|
|
guard let self else { return }
|
|
present(UIAlertController(title: "TextLink", message: "Clicked \(linkText)", preferredStyle: .alert).with{ $0.addAction(.init(title: "OK", style: .default)) }, animated: true)
|
|
}.store(in: &subscribers)
|
|
component.childTextAttributes = [link]
|
|
}
|
|
|
|
//setup UI
|
|
surfacePickerSelectorView.text = component.surface.rawValue
|
|
disabledSwitch.isOn = !component.isEnabled
|
|
labelTextField.text = component.labelText
|
|
childTextField.text = component.childText
|
|
showErrorSwitch.isOn = component.showError
|
|
errorTextField.text = component.errorText
|
|
|
|
}
|
|
|
|
//Picker
|
|
func setupPicker(){
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
}
|
|
}
|
|
}
|
|
|
|
extension CheckboxItemViewController: ComponentSampleable {
|
|
static func makeSample() -> ComponentSample {
|
|
let component = Self.makeComponent()
|
|
component.labelText = "Terms and conditions"
|
|
component.childText = "I agree to Verizon's terms and conditions click here"
|
|
component.errorText = "Error Text"
|
|
|
|
return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual)
|
|
}
|
|
}
|