vds_ios_sample/VDSSample/ViewControllers/TrailingTooltipLabelViewController.swift
Matt Bruce 58125423c9 fixed issue with toggles in the UI sample
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-09-19 09:43:00 -05:00

187 lines
6.2 KiB
Swift

//
// TrailingTooltipLabel.swift
// VDSSample
//
// Created by Matt Bruce on 4/14/23.
//
import Foundation
import UIKit
import VDS
import VDSCoreTokens
class TrailingTooltipLabelViewController: BaseViewController<TrailingTooltipLabel> {
lazy var textSizePickerSelectorView = {
TextSizePickerSelectorView(title: "",
picker: self.picker)
}()
lazy var fontCategoryPickerSelectorView = {
FontCategoryPickerSelectorView(title: "",
picker: self.picker)
}()
lazy var textAlignmentPickerSelectorView = {
PickerSelectorView(title: TextAlignment.left.rawValue,
picker: self.picker,
items: TextAlignment.allCases)
}()
var boldSwitch = Toggle()
var disabledSwitch = Toggle()
var textField = TextField()
var titleTextField = TextField()
var contentTextField = TextField()
var closeButtonTextField = TextField()
private var isBold: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: .makeWrapper(for: component, edgeSpacing: 16.0), edgeSpacing: 0.0)
setupPicker()
setupModel()
}
override func setupForm(){
super.setupForm()
addFormRow(label: "Disabled", view: disabledSwitch, pinTrailing: false)
addFormRow(label: "Bold", view: boldSwitch, pinTrailing: false)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Text Alignment", view: textAlignmentPickerSelectorView)
addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
addFormRow(label: "Text Size", view: textSizePickerSelectorView)
addFormRow(label: "Text", view: textField)
addFormRow(label: "Tooltip Title", view: titleTextField)
addFormRow(label: "Tooltip Content", view: contentTextField)
addFormRow(label: "Tooltip Close Button Text", view: closeButtonTextField)
disabledSwitch.onChange = { [weak self] sender in
self?.component.isEnabled = !sender.isOn
}
boldSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.isBold = sender.isOn
self?.updateLabelStyle()
}.store(in: &subscribers)
textField
.textPublisher
.sink { [weak self] text in
self?.component.labelText = text
}.store(in: &subscribers)
titleTextField
.textPublisher
.sink { [weak self] text in
self?.updateTooltip()
}.store(in: &subscribers)
contentTextField
.textPublisher
.sink { [weak self] text in
self?.updateTooltip()
}.store(in: &subscribers)
closeButtonTextField
.textPublisher
.sink { [weak self] text in
self?.updateTooltip()
}.store(in: &subscribers)
}
func setupModel() {
component.labelText = "5G Ultra Wideband is available in your area"
component.tooltipModel = .init(title: "Check the formatting of your address",
content:"House/Building number then street name")
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
disabledSwitch.isOn = !component.isEnabled
boldSwitch.isOn = isBold
textField.text = component.labelText
//set the font
fontCategory = .body
textSize = .small
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
disabledSwitch.isOn = !component.isEnabled
titleTextField.text = component.tooltipModel?.title
contentTextField.text = component.tooltipModel?.content
closeButtonTextField.text = component.tooltipModel?.closeButtonText
}
//Picker
private var fontCategory: TextStyle.FontCategory = .feature {
didSet {
fontCategoryPickerSelectorView.text = fontCategory.rawValue
textSizePickerSelectorView.items = fontCategory.sizes
if textSizePickerSelectorView.items.count > 0 {
textSize = textSizePickerSelectorView.items[0]
} else {
textSize = nil
}
updateLabelStyle()
}
}
private var textSize: TextStyle.FontSize? = .large {
didSet {
textSizePickerSelectorView.text = textSize?.rawValue ?? ""
updateLabelStyle()
}
}
func updateLabelStyle(){
if let style = fontCategory.style(for: textSize, isBold: isBold) {
component.labelTextStyle = style
}
}
func setupPicker(){
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.surface = item
self?.contentTopView.backgroundColor = item.color
}
textAlignmentPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.labelTextAlignment = item
}
textSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.textSize = item
}
fontCategoryPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.fontCategory = item
}
}
func updateTooltip() {
component.tooltipModel = .init(closeButtonText: closeButtonTextField.text ?? "Close",
title: titleTextField.text,
content: contentTextField.text)
}
}
extension TrailingTooltipLabelViewController: ComponentSampleable {
static func makeSample() -> ComponentSample {
let component = Self.makeComponent()
component.labelText = "5G Ultra Wideband is available in your area"
component.tooltipModel = .init(title: "Check the formatting of your address",
content:"House/Building number then street name")
return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual)
}
}