vds_ios_sample/VDSSample/ViewControllers/CarouselViewController.swift

182 lines
6.8 KiB
Swift

//
// CarouselViewController.swift
// VDSSample
//
// Created by Kanamarlapudi, Vasavi on 29/05/24.
//
import Foundation
import UIKit
import VDS
import Combine
import VDSTokens
class CarouselViewController: BaseViewController<Carousel> {
let label = Label()
lazy var scalingTypePickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Tilelet.AspectRatio.allCases)
}()
lazy var paginationDisplayPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Carousel.PaginationDisplay.allCases)
}()
lazy var peekPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Carousel.Peek.allCases)
}()
lazy var gutterPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Carousel.Gutter.allCases)
}()
lazy var layoutPickerSelectorView = {
PickerSelectorView(title: "1UP",
picker: self.picker,
items: UIDevice.isIPad ? CarouselScrollbar.Layout.allCases : [CarouselScrollbar.Layout.oneUP, CarouselScrollbar.Layout.twoUP, CarouselScrollbar.Layout.threeUP])
}()
lazy var paginationKindPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: ButtonIcon.Kind.allCases)
}()
lazy var horizAlignmtPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Carousel.Horizontal.allCases)
}()
lazy var vertAlignmtPickerSelectorView = {
PickerSelectorView(title: "",
picker: self.picker,
items: Carousel.Vertical.allCases)
}()
var paginationFloatingSwitch = Toggle()
var paginationInsetField = NumericField()
var widthTextField = NumericField()
var percentageTextField = NumericField()
override func viewDidLoad() {
super.viewDidLoad()
addContentTopView(view: component)
setupPicker()
setupModel()
}
override func setupForm() {
super.setupForm()
//add form rows
addFormRow(label: "onChange", view: label)
addFormRow(label: "Surface", view: surfacePickerSelectorView)
addFormRow(label: "Aspect Ratio", view: scalingTypePickerSelectorView)
addFormRow(label: "Pagination Display", view: paginationDisplayPickerSelectorView)
addFormRow(label: "Peek", view: peekPickerSelectorView)
addFormRow(label: "Gutter", view: gutterPickerSelectorView)
addFormRow(label: "Layout", view: layoutPickerSelectorView)
addFormRow(label: "Pagination Kind", view: paginationKindPickerSelectorView)
addFormRow(label: "Pagination Float", view: paginationFloatingSwitch)
addFormRow(label: "Pagination Inset", view: paginationInsetField)
addFormRow(label: "Width", view: widthTextField)
addFormRow(label: "Percentage (1-100)", view: percentageTextField)
addFormRow(label: "Slot Horizontal Alignment", view: horizAlignmtPickerSelectorView)
addFormRow(label: "Slot Vertical Alignment", view: vertAlignmtPickerSelectorView)
widthTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.width = .value(number.cgFloatValue)
self?.percentageTextField.text = ""
} else {
self?.component.width = nil
}
}.store(in: &subscribers)
percentageTextField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.width = .percentage(number.cgFloatValue)
self?.widthTextField.text = ""
} else {
self?.component.width = nil
}
}.store(in: &subscribers)
paginationInsetField
.numberPublisher
.sink { [weak self] number in
if let number {
self?.component.paginationInset = number.cgFloatValue
} else {
self?.component.paginationInset = UIDevice.isIPad ? VDSLayout.space12X : VDSLayout.space8X
}
}.store(in: &subscribers)
paginationFloatingSwitch.onChange = { [weak self] sender in
guard let self else { return }
self.component.pagination = .init(kind: paginationKindPickerSelectorView.selectedItem, floating: sender.isOn)
}
}
func setupModel() {
//setup UI
scalingTypePickerSelectorView.text = component.aspectRatio.rawValue
paginationDisplayPickerSelectorView.text = component.paginationDisplay.rawValue
peekPickerSelectorView.text = component.peek.rawValue
gutterPickerSelectorView.text = component.gutter.rawValue
layoutPickerSelectorView.text = component.layout.rawValue
paginationKindPickerSelectorView.text = ButtonIcon.Kind.lowContrast.rawValue
paginationFloatingSwitch.isOn = true
paginationInsetField.text = UIDevice.isIPad ? "12px" : "8px"
paginationInsetField.isUserInteractionEnabled = false
component.data = [{},{},{},{},{},{},{},{}]
}
func setupPicker() {
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.surface = item
self?.contentTopView.backgroundColor = item.color
}
scalingTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.aspectRatio = item
}
paginationDisplayPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.paginationDisplay = item
}
peekPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.peek = item
}
gutterPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.gutter = item
}
layoutPickerSelectorView.onPickerDidSelect = { [weak self] item in
self?.component.layout = item
}
paginationKindPickerSelectorView.onPickerDidSelect = { [weak self] item in
guard let self else { return }
self.component.pagination = .init(kind: item, floating: paginationFloatingSwitch.isOn)
}
}
}