vds_ios_sample/VDSSample/Protocols/PickerBase.swift
Matt Bruce 5ed2384db3 refactored to have a scrolling viewcontroller
updated checkboxgroup

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-24 11:08:05 -05:00

92 lines
2.3 KiB
Swift

//
// PickerBase.swift
// VDSSample
//
// Created by Matt Bruce on 8/1/22.
//
import Foundation
import UIKit
import VDS
protocol PickerViewable {
associatedtype EnumType: RawRepresentable
var items: [EnumType] { get set }
var onPickerDidSelect: ((EnumType) -> Void)? { get set }
}
class PickerBase<EnumType: RawRepresentable>: NSObject, PickerViewable, UIPickerViewDataSource, UIPickerViewDelegate {
var items: [EnumType]
var onPickerDidSelect: ((EnumType) -> Void)?
init(items: [EnumType]) {
self.items = items
super.init()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
items.count + 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int ) -> String? {
guard row > 0, let item = items[row-1].rawValue as? String else { return "" }
return item
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
guard row - 1 >= 0 else { return }
onPickerDidSelect?(items[row-1])
pickerView.isHidden = true
}
}
class PickerSelectorView: UIStackView {
var label = UILabel()
var button = UIButton(type: .system).with { instance in
instance.configuration = .filled()
instance.setTitle("Select", for: .normal)
}
init(title: String){
super.init(frame: .zero)
self.axis = .horizontal
self.distribution = .fillEqually
self.alignment = .fill
label.text = title
addArrangedSubview(label)
addArrangedSubview(button)
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class SurfacePicker: PickerBase<Surface> {
init(){
super.init(items: [.light, .dark])
}
}
class TextPositionPicker: PickerBase<TextPosition> {
init(){
super.init(items: [.left, .right])
}
}
class TextSizePicker: PickerBase<TypographicalStyle.FontSize> {
init(){
super.init(items: [.small, .large])
}
}
class FontCategoryPicker: PickerBase<TypographicalStyle.FontCategory> {
init(){
super.init(items: TypographicalStyle.FontCategory.allCases)
}
}