// // TiletViewController.swift // VDSSample // // Created by Matt Bruce on 12/19/22. // import Foundation import UIKit import VDS import VDSColorTokens import Combine class TileletViewController: BaseViewController { lazy var titleStandardStylePickerSelectorView = { PickerSelectorView(title: "", picker: self.picker, items: Tilelet.TitleModel.StandardStyle.allCases.sorted{ $0.rawValue < $1.rawValue }) }() lazy var otherStandardStylePickerSelectorView = { PickerSelectorView(title: "", picker: self.picker, items: Tilelet.SubTitleModel.StandardStyle.allCases.sorted{ $0.rawValue < $1.rawValue }) }() lazy var subtitleColorPickerSelectorView = { PickerSelectorView(title: "", picker: self.picker, items: [.primary, .secondary]) }() var clickableSwitch = Toggle() var titleTextField = TextField() var subTitleTextField = TextField() var widthTextField = NumericField() var heightTextField = NumericField() var textPercentageTextField = NumericField() var textWidthTextField = NumericField() var showDescriptionIconSwitch = Toggle() var showDirectionalIconSwitch = Toggle() var badgeTextField = TextField() override func viewDidLoad() { super.viewDidLoad() addContentTopView(view: .makeWrapper(for: component)) setupPicker() setupModel() } override func setupForm(){ super.setupForm() addFormRow(label: "Surface", view: surfacePickerSelectorView) addActionRow() addFormRow(label: "Clickable", view: clickableSwitch) addFormRow(label: "Title Style", view: titleStandardStylePickerSelectorView) addFormRow(label: "Other Style", view: otherStandardStylePickerSelectorView) addFormRow(label: "Title Text", view: titleTextField) addFormRow(label: "Subtitle Color", view: subtitleColorPickerSelectorView) addFormRow(label: "Subtitle Text", view: subTitleTextField) addFormRow(label: "Width", view: widthTextField) //addFormRow(label: "Height", view: heightTextField) addFormRow(label: "Text Width", view: textWidthTextField) addFormRow(label: "Text Percentage", view: textPercentageTextField) addFormRow(label: "Badge Text", view: badgeTextField) addFormRow(label: "Description Icon", view: showDescriptionIconSwitch) addFormRow(label: "Directional Icon", view: showDirectionalIconSwitch) clickableSwitch.onChange = { [weak self] sender in guard let self else { return } if sender.isOn { self.component.onClickActionPublisher("Tilelet", label: actionLabel) } else { self.component.onClick = nil } } heightTextField .numberPublisher .sink { [weak self] number in self?.component.height = number?.cgFloatValue }.store(in: &subscribers) widthTextField .numberPublisher .sink { [weak self] number in self?.component.width = number?.cgFloatValue }.store(in: &subscribers) textWidthTextField .numberPublisher .sink { [weak self] number in if let number = number?.cgFloatValue, number > 50.0 { self?.component.textWidth = number self?.textPercentageTextField.text = "" } else { self?.component.textWidth = nil } }.store(in: &subscribers) textPercentageTextField .numberPublisher .sink { [weak self] number in if let number, number.intValue > 15 && number.intValue <= 100 { self?.component.textPercentage = number.cgFloatValue self?.textWidthTextField.text = "" } else { self?.component.textPercentage = nil } }.store(in: &subscribers) titleTextField .textPublisher .sink { [weak self] text in self?.setTitleModel() }.store(in: &subscribers) subTitleTextField .textPublisher .sink { [weak self] text in self?.setSubTitleModel() }.store(in: &subscribers) badgeTextField .textPublisher .sink { [weak self] text in self?.setBadgeModel() }.store(in: &subscribers) showDescriptionIconSwitch.onChange = { [weak self] sender in if sender.isOn { self?.showDirectionalIconSwitch.isOn = false self?.component.descriptiveIconModel = .init(size: .medium, surface: .dark) } else { self?.component.descriptiveIconModel = nil } } showDirectionalIconSwitch.onChange = { [weak self] sender in if sender.isOn { self?.showDescriptionIconSwitch.isOn = false self?.component.directionalIconModel = .init(size: .medium, surface: .dark) } else { self?.component.directionalIconModel = nil } } } func setupModel() { let titleModel = Tilelet.TitleModel(text: "Save $XX on your monthly bill.") let subTitleModel = Tilelet.SubTitleModel(text: "Enroll in Auto Pay & paper-free billing to save on your monthly bill.") component.surface = .light component.titleModel = titleModel component.subTitleModel = subTitleModel //setup UI surfacePickerSelectorView.text = component.surface.rawValue otherStandardStylePickerSelectorView.text = subTitleModel.standardStyle.rawValue titleStandardStylePickerSelectorView.text = titleModel.standardStyle.rawValue subtitleColorPickerSelectorView.text = subTitleModel.textColor.rawValue titleTextField.text = titleModel.text subTitleTextField.text = subTitleModel.text widthTextField.text = component.width != nil ? "\(component.width!)" : "" self.updateOtherTextStyles() } //sub models func setBadgeModel() { if let text = badgeTextField.text, !text.isEmpty { component.badgeModel = Tilelet.BadgeModel(text: text) } else { component.badgeModel = nil } } func setTitleModel() { if let text = titleTextField.text, !text.isEmpty { component.titleModel = Tilelet.TitleModel(text: text, standardStyle: titleStandardStylePickerSelectorView.selectedItem) } else { component.titleModel = nil } } func setSubTitleModel() { if let text = subTitleTextField.text, !text.isEmpty { component.subTitleModel = Tilelet.SubTitleModel(text: text, standardStyle: otherStandardStylePickerSelectorView.selectedItem) } else { component.subTitleModel = nil } } func updateOtherTextStyles() { let items = component.titleLockup.standardStyleConfiguration.configuration(for: titleStandardStylePickerSelectorView.selectedItem.value)!.allOtherStandardStyles let otheritems = items.compactMap { Tilelet.SubTitleModel.StandardStyle(rawValue: $0.rawValue)! } otherStandardStylePickerSelectorView.items = otheritems otherStandardStylePickerSelectorView.text = otheritems.first?.rawValue ?? "" setSubTitleModel() } //Picker func setupPicker(){ surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in self?.component.surface = item self?.contentTopView.backgroundColor = item.color } titleStandardStylePickerSelectorView.onPickerDidSelect = { [weak self] item in guard let self else { return } self.setTitleModel() self.updateOtherTextStyles() } otherStandardStylePickerSelectorView.onPickerDidSelect = { [weak self] item in self?.setSubTitleModel() } subtitleColorPickerSelectorView.onPickerDidSelect = { [weak self] item in self?.setSubTitleModel() } } } extension TileletViewController: ComponentSampleable { static func makeSample() -> ComponentSample { let component = Self.makeComponent() let titleModel = Tilelet.TitleModel(text: "Save $XX on your monthly bill.") let subTitleModel = Tilelet.SubTitleModel(text: "Enroll in Auto Pay & paper-free billing to save on your monthly bill.") component.surface = .light component.titleModel = titleModel component.subTitleModel = subTitleModel component.width = 250 return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual) } }