vds_ios_sample/VDSSample/Protocols/PickerBase.swift
2022-08-16 18:16:04 -05:00

70 lines
1.7 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 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)
}
}