vds_ios_sample/VDSSample/ViewControllers/TileletViewController.swift
Matt Bruce 34cf1d979b more tilelet changes
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-05-22 16:54:47 -05:00

848 lines
34 KiB
Swift

//
// TiletViewController.swift
// VDSSample
//
// Created by Matt Bruce on 12/19/22.
//
import Foundation
import UIKit
import VDS
import VDSTokens
import Combine
class TileletViewController: BaseViewController<Tilelet> {
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.OtherStandardStyle.allCases.sorted{ $0.rawValue < $1.rawValue })
}()
lazy var textPositionPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Tilelet.TextPosition.allCases.sorted{ $0.rawValue < $1.rawValue })
}()
lazy var paddingPickerSelectorView = {
PickerSelectorView(title: "small",
picker: self.picker,
items: Tilelet.Padding.allCases)
}()
lazy var badgeFillColorPickerSelectorView = {
PickerSelectorView(title: "red",
picker: self.picker,
items: Badge.FillColor.allCases)
}()
lazy var badgeNumberOfLinesPickerSelectorView = {
PickerSelectorView(title: "one",
picker: self.picker,
items: BadgeViewController.NumberOfLines.allCases)
}()
lazy var scalingTypePickerSelectorView = {
PickerSelectorView(title: "white",
picker: self.picker,
items: Tilelet.AspectRatio.allCases)
}()
lazy var imageFallbackColorPickerSelectorView = {
SurfacePickerSelectorView(picker: self.picker)
}()
lazy var backgroundEffectSelectorView = {
PickerSelectorView(title: "none",
picker: self.picker,
items: TileContainerViewController.BackgroundEffect.allCases)
}()
lazy var colorPicker: UIColorPickerViewController = {
let picker = UIColorPickerViewController()
picker.delegate = self
return picker
}()
var colorPickerType: ColorPickerType = .backgroundColor
enum ColorPickerType {
case backgroundColor
case gradientColor1, gradientColor2
case contentViewBackgroundColor, light, dark
}
lazy var gradientColorView1: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.gradientColor1) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var gradientColorView2: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.gradientColor2) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var backgroundColorPickerSelectorView = {
PickerSelectorView(title: "white",
picker: self.picker,
items: BackgroundColor.allCases)
}()
lazy var textAlignmentPickerSelectorView = {
PickerSelectorView(title: "left",
picker: self.picker,
items: TitleLockup.TextAlignment.allCases)
}()
/// titleLockup
var currentSurfaceColorType: SurfaceColorType = .title
enum SurfaceColorType {
case eyebrow, title, subtitle, directionalIcon, descriptionIcon
}
enum TitleTextColor: String, CaseIterable {
case primary, custom
}
enum TextColor: String, CaseIterable {
case primary, secondary, custom
}
/// eyebrow
var eyebrowColorsFormStackView = FormSection().with { $0.isHidden = true }
lazy var eyebrowColorPickerSelectorView = {
PickerSelectorView(title: "primary",
picker: self.picker,
items: TextColor.allCases)
}()
lazy var eyebrowLightColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.light) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var eyebrowDarkColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.dark) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
/// title
var titleColorsFormStackView = FormSection().with { $0.isHidden = true }
lazy var titleColorPickerSelectorView = {
PickerSelectorView(title: "primary",
picker: self.picker,
items: TitleTextColor.allCases)
}()
lazy var titleLightColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.light) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var titleDarkColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.dark) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
/// subtitle
var subtitleColorsFormStackView = FormSection().with { $0.isHidden = true }
lazy var subtitleColorPickerSelectorView = {
PickerSelectorView(title: "primary",
picker: self.picker,
items: TextColor.allCases)
}()
lazy var subtitleLightColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.light) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var subtitleDarkColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.dark) { [weak self] picker in
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var descriptionIconLightColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.light) { [weak self] picker in
self?.currentSurfaceColorType = .descriptionIcon
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
var descriptionIconFormStackView = FormSection().with { $0.isHidden = true }
lazy var descriptionIconDarkColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.dark) { [weak self] picker in
self?.currentSurfaceColorType = .descriptionIcon
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
var directionalIconFormStackView = FormSection().with { $0.isHidden = true }
lazy var directionalIconLightColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.light) { [weak self] picker in
self?.currentSurfaceColorType = .directionalIcon
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var directionalIconDarkColorView: ColorPickerView<ColorPickerType> = {
return .init(with: ColorPickerType.dark) { [weak self] picker in
self?.currentSurfaceColorType = .directionalIcon
self?.colorPickerType = picker.pickerType
self?.selectedColorTapped(picker)
}
}()
lazy var directionIconPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Tilelet.DirectionalIcon.IconType.allCases.sorted{ $0.rawValue < $1.rawValue })
}()
lazy var directionIconSizePickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Tilelet.DirectionalIcon.IconSize.allCases.sorted{ $0.rawValue < $1.rawValue })
}()
let backgroundImage = UIImage(named: "backgroundTest")!
var clickableSwitch = Toggle()
var eyebrowTextField = TextField()
var titleTextField = TextField()
var subTitleTextField = TextField()
var widthTextField = NumericField()
var heightTextField = NumericField()
var textPercentageTextField = NumericField()
var textWidthTextField = NumericField()
var showBackgroundImageSwitch = Toggle()
var showDescriptionIconSwitch = Toggle()
var showDirectionalIconSwitch = Toggle()
var badgeTextField = TextField()
var eyebrowIsBold = Toggle().with { $0.isOn = true }
var titleIsBold = Toggle().with { $0.isOn = true }
var selectedGradient1Color: String?
var selectedGradient2Color: String?
var selectedGradientColorView: UIView?
var showDropShadowSwitch = Toggle()
var backgroundColor: BackgroundColor = .black
var maxWidthTextField = NumericField()
var isLinkSwitch = Toggle()
var gradientColorsFormStackView = FormSection().with { $0.isHidden = true }
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: "Is Link", view: isLinkSwitch)
addFormRow(label: "Clickable", view: clickableSwitch)
addFormRow(label: "Aspect Ratio", view: scalingTypePickerSelectorView)
addFormRow(label: "Padding", view: paddingPickerSelectorView)
addFormRow(label: "Width", view: widthTextField)
addFormRow(label: "Height", view: heightTextField)
addFormRow(label: "Text Alignment", view: textAlignmentPickerSelectorView)
addFormRow(label: "Text Width", view: textWidthTextField)
addFormRow(label: "Text Percentage", view: textPercentageTextField)
addFormRow(label: "Text Position", tooltip: .init(title:"Text Position", content: "Minimum height is configurable"), view: textPositionPickerSelectorView)
addFormRow(label: "Background Color", tooltip: .init(title:"Background Color", content: "This color takes precedence over surface and will set all children's surface property to this value."), view: backgroundColorPickerSelectorView)
addFormRow(label: "Background Image", view: showBackgroundImageSwitch)
addFormRow(label: "Show Drop Shadow", view: showDropShadowSwitch)
addFormRow(label: "Image Fallback Color", view: imageFallbackColorPickerSelectorView)
addFormRow(label: "Background Effect", view: backgroundEffectSelectorView)
//Gradient Section
gradientColorsFormStackView.addFormRow(label: "Gradient Color1", view: gradientColorView1)
gradientColorsFormStackView.addFormRow(label: "Gradient Color2", view: gradientColorView2)
append(section: gradientColorsFormStackView)
append(section: .init().with({
$0.title = "Badge"
$0.addFormRow(label: "Text", view: badgeTextField)
$0.addFormRow(label: "Fill Color", view: badgeFillColorPickerSelectorView)
$0.addFormRow(label: "Number of Lines", view: badgeNumberOfLinesPickerSelectorView)
$0.addFormRow(label: "Max Width", view: maxWidthTextField)
}))
append(section: .init().with({
$0.title = "Eyebrow"
$0.addFormRow(label: "Text Style", tooltip: .init(title: "Text Style", content: "Eyebrow and Subtitle will share the same textStyle."), view: otherStandardStylePickerSelectorView)
$0.addFormRow(label: "is Bold", view: eyebrowIsBold)
$0.addFormRow(label: "Text", view: eyebrowTextField)
$0.addFormRow(label: "Color", view: eyebrowColorPickerSelectorView)
}))
eyebrowColorsFormStackView.addFormRow(label: "Light", view: eyebrowLightColorView)
eyebrowColorsFormStackView.addFormRow(label: "Dark", view: eyebrowDarkColorView)
append(section: eyebrowColorsFormStackView)
append(section: .init().with({
$0.title = "Title"
$0.addFormRow(label: "Text Style", view: titleStandardStylePickerSelectorView)
$0.addFormRow(label: "is Bold", view: titleIsBold)
$0.addFormRow(label: "Text", view: titleTextField)
$0.addFormRow(label: "Color", view: titleColorPickerSelectorView)
}))
titleColorsFormStackView.addFormRow(label: "Light", view: titleLightColorView)
titleColorsFormStackView.addFormRow(label: "Dark", view: titleDarkColorView)
append(section: titleColorsFormStackView)
append(section: .init().with({
$0.title = "Subtitle"
$0.addFormRow(label: "Text", view: subTitleTextField)
$0.addFormRow(label: "Color", view: subtitleColorPickerSelectorView)
}))
subtitleColorsFormStackView.addFormRow(label: "Light", view: subtitleLightColorView)
subtitleColorsFormStackView.addFormRow(label: "Dark", view: subtitleDarkColorView)
append(section: subtitleColorsFormStackView)
append(section: .init().with({
$0.title = "Description Icon "
$0.addFormRow(label: "Show", view: showDescriptionIconSwitch)
}))
append(section: descriptionIconFormStackView)
descriptionIconFormStackView.addFormRow(label: "Light", view: descriptionIconLightColorView)
descriptionIconFormStackView.addFormRow(label: "Dark", view: descriptionIconDarkColorView)
append(section: .init().with({
$0.title = "Directional Icon"
$0.addFormRow(label: "Show", view: showDirectionalIconSwitch)
}))
append(section: directionalIconFormStackView)
directionalIconFormStackView.addFormRow(label: "Icon", view: directionIconPickerSelectorView)
directionalIconFormStackView.addFormRow(label: "Size", view: directionIconSizePickerSelectorView)
directionalIconFormStackView.addFormRow(label: "Light", view: directionalIconLightColorView)
directionalIconFormStackView.addFormRow(label: "Dark", view: directionalIconDarkColorView)
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
}
}
isLinkSwitch.onChange = { [weak self] sender in
guard let self else { return }
self.component.accessibilityTraits = sender.isOn ? .link : .button
}
heightTextField
.numberPublisher
.sink { [weak self] number in
guard let self else { return }
if let value = number?.cgFloatValue, value >= 100 && value < self.view.frame.height * 0.65 {
self.component.height = value
self.component.layoutIfNeeded()
}
}.store(in: &subscribers)
widthTextField
.numberPublisher
.sink { [weak self] number in
guard let self else { return }
if let value = number?.cgFloatValue, value >= 100 && value < self.view.frame.width * 0.85 {
self.component.width = value
self.component.layoutIfNeeded()
}
}.store(in: &subscribers)
textWidthTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.textWidth = .value(number.cgFloatValue)
self?.textPercentageTextField.text = ""
} else {
self?.component.textWidth = nil
}
}.store(in: &subscribers)
textPercentageTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.textWidth = .percentage(number.cgFloatValue)
self?.textWidthTextField.text = ""
} else {
self?.component.textWidth = 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
self?.descriptionIconFormStackView.isHidden = !sender.isOn
self?.directionalIconFormStackView.isHidden = sender.isOn
if sender.isOn {
self?.showDirectionalIconSwitch.isOn = false
self?.setDescriptiveIconModel()
} else {
self?.component.descriptiveIconModel = nil
}
}
showDirectionalIconSwitch.onChange = { [weak self] sender in
self?.descriptionIconFormStackView.isHidden = sender.isOn
self?.directionalIconFormStackView.isHidden = !sender.isOn
if sender.isOn {
self?.showDescriptionIconSwitch.isOn = false
self?.setDirectionalIconModel()
} else {
self?.component.directionalIconModel = nil
}
}
eyebrowIsBold.publisher(for: .valueChanged).sink { [weak self] toggle in
self?.setEyebrowModel()
}.store(in: &subscribers)
titleIsBold.publisher(for: .valueChanged).sink { [weak self] toggle in
self?.setTitleModel()
}.store(in: &subscribers)
paddingPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.padding = item
}
eyebrowTextField.textPublisher
.sink { [weak self] text in
self?.setEyebrowModel()
}.store(in: &subscribers)
showBackgroundImageSwitch.onChange = { [weak self] sender in
if let image = self?.backgroundImage, sender.isOn {
self?.component.backgroundImage = image
} else {
self?.component.backgroundImage = nil
}
}
showDropShadowSwitch.onChange = { [weak self] sender in
self?.component.showDropShadow = sender.isOn
}
imageFallbackColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.imageFallbackColor = item
}
maxWidthTextField
.numberPublisher
.sink { [weak self] number in
guard let number else { return }
self?.component.badgeModel?.maxWidth = number.cgFloatValue
}.store(in: &subscribers)
}
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.color = .black
component.surface = .light
component.titleModel = titleModel
component.subTitleModel = subTitleModel
component.padding = .small
//setup UI
backgroundColorPickerSelectorView.text = backgroundColor.rawValue
textAlignmentPickerSelectorView.text = TextAlignment.left.rawValue
surfacePickerSelectorView.text = component.surface.rawValue
otherStandardStylePickerSelectorView.text = subTitleModel.otherStandardStyle.rawValue
titleStandardStylePickerSelectorView.text = titleModel.standardStyle.rawValue
titleTextField.text = titleModel.text
subTitleTextField.text = subTitleModel.text
widthTextField.text = component.width != nil ? "\(component.width!)" : ""
textPositionPickerSelectorView.text = component.textPostion.rawValue
scalingTypePickerSelectorView.text = component.aspectRatio.rawValue
updateOtherTextStyles()
}
//sub models
func setBadgeModel() {
if let text = badgeTextField.text, !text.isEmpty {
component.badgeModel = Tilelet.BadgeModel(text: text, fillColor: badgeFillColorPickerSelectorView.selectedItem, numberOfLines: badgeNumberOfLinesPickerSelectorView.selectedItem.intValue)
} else {
component.badgeModel = nil
}
}
func setTitleModel() {
if let text = titleTextField.text, !text.isEmpty {
var textColor: TitleLockup.TitleTextColor
switch titleColorPickerSelectorView.selectedItem {
case .primary:
textColor = .primary
case .custom:
if let light = titleLightColorView.selectedColor {
let dark = titleDarkColorView.selectedColor ?? light
textColor = .custom(light, dark)
} else {
textColor = .custom(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
}
}
component.titleModel = Tilelet.TitleModel(text: text, textColor: textColor, isBold: titleIsBold.isOn, standardStyle: titleStandardStylePickerSelectorView.selectedItem)
} else {
component.titleModel = nil
}
}
func setSubTitleModel() {
if let text = subTitleTextField.text, !text.isEmpty {
var textColor: TitleLockup.TextColor
switch subtitleColorPickerSelectorView.selectedItem {
case .primary:
textColor = .primary
case .secondary:
textColor = .secondary
case .custom:
if let light = subtitleLightColorView.selectedColor {
let dark = subtitleDarkColorView.selectedColor ?? light
textColor = .custom(light, dark)
} else {
textColor = .custom(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
}
}
component.subTitleModel = Tilelet.SubTitleModel(text: text, otherStandardStyle: otherStandardStylePickerSelectorView.selectedItem, textColor: textColor)
} else {
component.subTitleModel = nil
}
}
func setEyebrowModel() {
if let text = eyebrowTextField.text, !text.isEmpty {
var textColor: TitleLockup.TextColor
switch eyebrowColorPickerSelectorView.selectedItem {
case .primary:
textColor = .primary
case .secondary:
textColor = .secondary
case .custom:
if let light = eyebrowLightColorView.selectedColor {
let dark = eyebrowDarkColorView.selectedColor ?? light
textColor = .custom(light, dark)
} else {
textColor = .custom(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
}
}
component.eyebrowModel = Tilelet.EyebrowModel(text: text, textColor: textColor, isBold: eyebrowIsBold.isOn, standardStyle: otherStandardStylePickerSelectorView.selectedItem)
} else {
component.eyebrowModel = nil
}
}
func setDescriptiveIconModel() {
let light = descriptionIconLightColorView.selectedColor ?? descriptionIconDarkColorView.selectedColor
let dark = descriptionIconDarkColorView.selectedColor ?? descriptionIconLightColorView.selectedColor
if let light, let dark {
component.descriptiveIconModel = .init(colorConfiguration: SurfaceColorConfiguration(light, dark), size: .medium)
} else {
component.descriptiveIconModel = .init(size: .medium)
}
}
func setDirectionalIconModel() {
let light = directionalIconLightColorView.selectedColor ?? directionalIconDarkColorView.selectedColor
let dark = directionalIconDarkColorView.selectedColor ?? directionalIconLightColorView.selectedColor
let iconType = directionIconPickerSelectorView.selectedItem
let iconSize = directionIconSizePickerSelectorView.selectedItem
if let light, let dark {
component.directionalIconModel = .init(iconType: iconType, colorConfiguration: SurfaceColorConfiguration(light, dark), size: iconSize)
} else {
component.directionalIconModel = .init(iconType: iconType, size: iconSize)
}
}
func updateOtherTextStyles() {
let items = component.titleLockup.standardStyleConfiguration.configuration(for: titleStandardStylePickerSelectorView.selectedItem.value)!.allOtherStandardStyles
let otheritems = items.compactMap { Tilelet.SubTitleModel.OtherStandardStyle(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()
}
textPositionPickerSelectorView.onPickerDidSelect = { [weak self] item in
guard let self else { return }
self.component.textPostion = item
}
badgeFillColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.badgeModel?.fillColor = item
}
badgeNumberOfLinesPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.badgeModel?.numberOfLines = item.intValue
}
scalingTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.aspectRatio = item
self?.component.layoutIfNeeded()
self?.showDebug(show: self?.debugViewSwitch.isOn ?? false)
}
backgroundEffectSelectorView.onPickerDidSelect = { [weak self] item in
guard let self else { return }
if let effect = item.effect {
self.component.backgroundEffect = self.getTilelet(effect: effect)
self.gradientColorsFormStackView.isHidden = true
self.gradientColorView1.selectedColor = nil
self.gradientColorView2.selectedColor = nil
} else {
self.colorPickerType = .gradientColor1
self.gradientColorsFormStackView.isHidden = false
}
}
textAlignmentPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.titleLockup.textAlignment = item
}
backgroundColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
guard let self else { return }
if let color = self.getTilelet(backgroundColor: item) {
self.component.color = color
} else {
self.colorPickerType = .backgroundColor
self.present(self.colorPicker, animated: true)
}
}
eyebrowColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.currentSurfaceColorType = .eyebrow
self?.eyebrowColorsFormStackView.isHidden = item != .custom
if item != .custom {
self?.setEyebrowModel()
}
}
titleColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.currentSurfaceColorType = .title
self?.titleColorsFormStackView.isHidden = item != .custom
if item != .custom {
self?.setTitleModel()
}
}
subtitleColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.currentSurfaceColorType = .subtitle
self?.subtitleColorsFormStackView.isHidden = item != .custom
if item != .custom {
self?.setSubTitleModel()
}
}
directionIconPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.setDirectionalIconModel()
}
directionIconSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.setDirectionalIconModel()
}
}
func getTilelet(effect: TileContainer.BackgroundEffect) -> Tilelet.BackgroundEffect {
switch effect {
case .transparency:
Tilelet.BackgroundEffect.transparency
case .gradient(let firstColor, let secondColor):
Tilelet.BackgroundEffect.gradient(firstColor, secondColor)
default:
Tilelet.BackgroundEffect.none
}
}
func getTilelet(backgroundColor: BackgroundColor) -> Tilelet.BackgroundColor? {
switch backgroundColor {
case .primary:
.primary
case .secondary:
.secondary
case .white:
.white
case .black:
.black
case .custom:
nil
}
}
}
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)
}
}
extension TileletViewController: UIColorPickerViewControllerDelegate {
func selectedColorTapped(_ picker: ColorPickerView<ColorPickerType>) {
let selectedColor = picker.selectedColor
if let selectedColor {
colorPicker.selectedColor = selectedColor
}
present(colorPicker, animated: true)
}
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
dismiss(animated: true)
}
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
let color = viewController.selectedColor
var lightColorView: ColorPickerView<ColorPickerType>
var darkColorView: ColorPickerView<ColorPickerType>
switch colorPickerType {
case .contentViewBackgroundColor:
component.contentView.backgroundColor = color
case .backgroundColor:
component.color = .custom(color)
case .gradientColor1:
gradientColorView1.selectedColor = viewController.selectedColor
updateGradientColors()
case .gradientColor2:
gradientColorView2.selectedColor = viewController.selectedColor
updateGradientColors()
case .light, .dark:
var lightColorView: ColorPickerView<ColorPickerType>
var darkColorView: ColorPickerView<ColorPickerType>
switch currentSurfaceColorType {
case .eyebrow:
lightColorView = eyebrowLightColorView
darkColorView = eyebrowDarkColorView
case .title:
lightColorView = titleLightColorView
darkColorView = titleDarkColorView
case .subtitle:
lightColorView = subtitleLightColorView
darkColorView = subtitleDarkColorView
case .directionalIcon:
lightColorView = directionalIconLightColorView
darkColorView = directionalIconDarkColorView
case .descriptionIcon:
lightColorView = descriptionIconLightColorView
darkColorView = descriptionIconDarkColorView
}
if colorPickerType == .light {
lightColorView.selectedColor = viewController.selectedColor
} else {
darkColorView.selectedColor = viewController.selectedColor
}
switch currentSurfaceColorType {
case .eyebrow:
setEyebrowModel()
case .title:
setTitleModel()
case .subtitle:
setSubTitleModel()
case .directionalIcon:
setDirectionalIconModel()
case .descriptionIcon:
setDescriptiveIconModel()
}
}
}
func updateGradientColors(){
if let selectedGradient1Color = gradientColorView1.selectedColor,
let selectedGradient2Color = gradientColorView2.selectedColor{
component.backgroundEffect = .gradient(selectedGradient1Color, selectedGradient2Color)
}
}
}
extension TileletViewController {
enum BackgroundColor: String, CaseIterable {
case primary, secondary, white, black, custom
var color: TileContainer.BackgroundColor? {
switch self {
case .primary:
.primary
case .secondary:
.secondary
case .white:
.white
case .black:
.black
case .custom:
nil
}
}
}
}