vds_ios_sample/VDSSample/ViewControllers/LabelViewController.swift
Matt Bruce 31fae5c21c using property instead of model
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-16 11:09:28 -05:00

181 lines
5.4 KiB
Swift

//
// LabelViewController.swift
// VDSSample
//
// Created by Matt Bruce on 8/16/22.
//
import Foundation
import UIKit
import VDS
import VDSColorTokens
class LabelViewController: ModelScrollViewController<DefaultLabelModel> {
enum PickerType {
case surface, textSize, fontCategory
}
var surfacePickerSelectorView = PickerSelectorView(title: "")
var textSizePickerSelectorView = PickerSelectorView(title: "")
var fontCategoryPickerSelectorView = PickerSelectorView(title: "")
var boldSwitch = UISwitch()
var disabledSwitch = UISwitch()
var textField = TextField()
private var isBold: Bool = false
var label = Label()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: label)
setupForm()
setupPicker()
setupModel()
}
func setupForm(){
addFormRow(label: "Disabled", view: disabledSwitch)
addFormRow(label: "Bold", view: boldSwitch)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
addFormRow(label: "Text Size", view: textSizePickerSelectorView)
addFormRow(label: "Text", view: textField)
disabledSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.label.disabled = sender.isOn
}.store(in: &subscribers)
boldSwitch
.publisher(for: .valueChanged)
.sink { [weak self] sender in
self?.isBold = sender.isOn
self?.updateLabelStyle()
}.store(in: &subscribers)
surfacePickerSelectorView.button
.publisher(for: .touchUpInside)
.sink { [weak self] _ in
self?.pickerType = .surface
}.store(in: &subscribers)
fontCategoryPickerSelectorView.button
.publisher(for: .touchUpInside)
.sink { [weak self] _ in
self?.pickerType = .fontCategory
}.store(in: &subscribers)
textSizePickerSelectorView.button
.publisher(for: .touchUpInside)
.sink { [weak self] _ in
self?.pickerType = .textSize
}.store(in: &subscribers)
textField
.textPublisher
.sink { [weak self] text in
self?.label.text = text
}.store(in: &subscribers)
}
func setupModel() {
var defaultModel = DefaultLabelModel()
defaultModel.text = "Label Component"
defaultModel.typograpicalStyle = .FeatureSmall
set(with: defaultModel)
label
.handlerPublisher()
.sink { [weak self] viewModel in
self?.model = viewModel
}.store(in: &subscribers)
//setup UI
surfacePickerSelectorView.text = model.surface.rawValue
disabledSwitch.isOn = model.disabled
boldSwitch.isOn = isBold
textField.text = model.text
//set the font
fontCategory = .feature
fontCategoryPickerSelectorView.text = "Feature"
textSize = .small
textSizePickerSelectorView.text = "Small"
}
override func updateView(viewModel: DefaultLabelModel) {
label.set(with: viewModel)
}
//Picker
var surfacePicker = SurfacePicker()
var textSizePicker = TextSizePicker()
var fontCategoryPicker = FontCategoryPicker()
var pickerType: PickerType = .surface {
didSet {
func update(object: UIPickerViewDelegate & UIPickerViewDataSource){
picker.delegate = object
picker.dataSource = object
}
switch pickerType{
case .surface:
update(object: surfacePicker)
case .textSize:
update(object: textSizePicker)
case .fontCategory:
update(object: fontCategoryPicker)
}
picker.reloadAllComponents()
picker.selectRow(0, inComponent: 0, animated: false)
picker.isHidden = false
}
}
private var fontCategory: TypographicalStyle.FontCategory = .feature {
didSet {
fontCategoryPickerSelectorView.text = fontCategory.rawValue
textSizePicker.items = fontCategory.sizes
if textSizePicker.items.count > 0 {
textSize = textSizePicker.items[0]
} else {
textSize = nil
}
updateLabelStyle()
}
}
private var textSize: TypographicalStyle.FontSize? = .large {
didSet {
textSizePickerSelectorView.text = textSize?.rawValue ?? ""
updateLabelStyle()
}
}
func updateLabelStyle(){
if let style = fontCategory.style(for: textSize, isBold: isBold) {
label.typograpicalStyle = style
}
}
func setupPicker(){
surfacePicker.onPickerDidSelect = { [weak self] item in
self?.label.surface = item
self?.contentTopView.backgroundColor = item.color
self?.surfacePickerSelectorView.text = item.rawValue
}
textSizePicker.onPickerDidSelect = { [weak self] item in
self?.textSize = item
}
fontCategoryPicker.onPickerDidSelect = { [weak self] item in
self?.fontCategory = item
}
}
}