|
| 1 | +import SwiftCrossUI |
| 2 | +import UIKit |
| 3 | + |
| 4 | +protocol Picker: BaseWidget { |
| 5 | + func setOptions(to options: [String]) |
| 6 | + func setChangeHandler(to onChange: @escaping (Int?) -> Void) |
| 7 | + func setSelectedOption(to index: Int?) |
| 8 | +} |
| 9 | + |
| 10 | +@available(tvOS, unavailable) |
| 11 | +final class UIPickerViewPicker: WrapperWidget<UIPickerView>, Picker, UIPickerViewDataSource, |
| 12 | + UIPickerViewDelegate |
| 13 | +{ |
| 14 | + private var options: [String] = [] |
| 15 | + private var onSelect: ((Int?) -> Void)? |
| 16 | + |
| 17 | + init() { |
| 18 | + super.init(child: UIPickerView()) |
| 19 | + |
| 20 | + child.dataSource = self |
| 21 | + child.delegate = self |
| 22 | + |
| 23 | + child.selectRow(0, inComponent: 0, animated: false) |
| 24 | + } |
| 25 | + |
| 26 | + func setOptions(to options: [String]) { |
| 27 | + self.options = options |
| 28 | + child.reloadComponent(0) |
| 29 | + } |
| 30 | + |
| 31 | + func setChangeHandler(to onChange: @escaping (Int?) -> Void) { |
| 32 | + onSelect = onChange |
| 33 | + } |
| 34 | + |
| 35 | + func setSelectedOption(to index: Int?) { |
| 36 | + child.selectRow( |
| 37 | + (index ?? -1) + 1, inComponent: 0, animated: false) |
| 38 | + } |
| 39 | + |
| 40 | + func numberOfComponents(in _: UIPickerView) -> Int { |
| 41 | + 1 |
| 42 | + } |
| 43 | + |
| 44 | + func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int { |
| 45 | + options.count + 1 |
| 46 | + } |
| 47 | + |
| 48 | + // For some reason, if compiling for tvOS, the compiler complains if I even attempt |
| 49 | + // to define these methods. |
| 50 | + #if !os(tvOS) |
| 51 | + func pickerView( |
| 52 | + _: UIPickerView, |
| 53 | + titleForRow row: Int, |
| 54 | + forComponent _: Int |
| 55 | + ) -> String? { |
| 56 | + switch row { |
| 57 | + case 0: |
| 58 | + "" |
| 59 | + case 1...options.count: |
| 60 | + options[row - 1] |
| 61 | + default: |
| 62 | + nil |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + func pickerView( |
| 67 | + _: UIPickerView, |
| 68 | + didSelectRow row: Int, |
| 69 | + inComponent _: Int |
| 70 | + ) { |
| 71 | + onSelect?(row > 0 ? row - 1 : nil) |
| 72 | + } |
| 73 | + #endif |
| 74 | +} |
| 75 | + |
| 76 | +final class UITableViewPicker: WrapperWidget<UITableView>, Picker, UITableViewDelegate, |
| 77 | + UITableViewDataSource |
| 78 | +{ |
| 79 | + private static let reuseIdentifier = |
| 80 | + "__SwiftCrossUI_UIKitBackend_UITableViewPicker.reuseIdentifier" |
| 81 | + |
| 82 | + private var options: [String] = [] |
| 83 | + private var onSelect: ((Int?) -> Void)? |
| 84 | + |
| 85 | + init() { |
| 86 | + super.init(child: UITableView(frame: .zero, style: .plain)) |
| 87 | + |
| 88 | + child.delegate = self |
| 89 | + child.dataSource = self |
| 90 | + |
| 91 | + child.register(UITableViewCell.self, forCellReuseIdentifier: Self.reuseIdentifier) |
| 92 | + } |
| 93 | + |
| 94 | + func setOptions(to options: [String]) { |
| 95 | + self.options = options |
| 96 | + child.reloadData() |
| 97 | + } |
| 98 | + |
| 99 | + func setChangeHandler(to onChange: @escaping (Int?) -> Void) { |
| 100 | + onSelect = onChange |
| 101 | + } |
| 102 | + |
| 103 | + func setSelectedOption(to index: Int?) { |
| 104 | + if let index { |
| 105 | + child.selectRow( |
| 106 | + at: IndexPath(row: index, section: 0), animated: true, scrollPosition: .middle) |
| 107 | + } else { |
| 108 | + child.selectRow(at: nil, animated: false, scrollPosition: .none) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int { |
| 113 | + options.count |
| 114 | + } |
| 115 | + |
| 116 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 117 | + let cell = tableView.dequeueReusableCell( |
| 118 | + withIdentifier: Self.reuseIdentifier, for: indexPath) |
| 119 | + |
| 120 | + cell.textLabel!.text = options[indexPath.row] |
| 121 | + |
| 122 | + return cell |
| 123 | + } |
| 124 | + |
| 125 | + func tableView( |
| 126 | + _: UITableView, |
| 127 | + didSelectRowAt indexPath: IndexPath |
| 128 | + ) { |
| 129 | + onSelect?(indexPath.row) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +extension UIKitBackend { |
| 134 | + public func createPicker() -> Widget { |
| 135 | + #if targetEnvironment(macCatalyst) |
| 136 | + if UIDevice.current.userInterfaceIdiom == .mac { |
| 137 | + return UITableViewPicker() |
| 138 | + } else { |
| 139 | + return UIPickerViewPicker() |
| 140 | + } |
| 141 | + #elseif os(tvOS) |
| 142 | + return UITableViewPicker() |
| 143 | + #else |
| 144 | + return UIPickerViewPicker() |
| 145 | + #endif |
| 146 | + } |
| 147 | + |
| 148 | + public func updatePicker( |
| 149 | + _ picker: Widget, |
| 150 | + options: [String], |
| 151 | + environment _: EnvironmentValues, |
| 152 | + onChange: @escaping (Int?) -> Void |
| 153 | + ) { |
| 154 | + let pickerWidget = picker as! any Picker |
| 155 | + pickerWidget.setChangeHandler(to: onChange) |
| 156 | + pickerWidget.setOptions(to: options) |
| 157 | + } |
| 158 | + |
| 159 | + public func setSelectedOption(ofPicker picker: Widget, to selectedOption: Int?) { |
| 160 | + let pickerWidget = picker as! any Picker |
| 161 | + pickerWidget.setSelectedOption(to: selectedOption) |
| 162 | + } |
| 163 | +} |
0 commit comments