105 lines
2.5 KiB
Swift
105 lines
2.5 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 {
|
|
private var label = UILabel()
|
|
var button = UIButton(type: .system).with { instance in
|
|
instance.configuration = .filled()
|
|
instance.setTitle("Select", for: .normal)
|
|
}
|
|
|
|
var text: String = "" {
|
|
didSet {
|
|
label.text = text
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
class UsePicker: PickerBase<Use> {
|
|
init(){
|
|
super.init(items: [.primary, .secondary])
|
|
}
|
|
}
|
|
|