1 | import { ListPickerBase, selectedIndexProperty, itemsProperty } from './list-picker-common';
|
2 | import { colorProperty } from '../styling/style-properties';
|
3 | import { Color } from '../../color';
|
4 | export * from './list-picker-common';
|
5 | let Formatter;
|
6 | let ValueChangeListener;
|
7 | function initializeNativeClasses() {
|
8 | if (Formatter) {
|
9 | return;
|
10 | }
|
11 | var FormatterImpl = (function (_super) {
|
12 | __extends(FormatterImpl, _super);
|
13 | function FormatterImpl(owner) {
|
14 | var _this = _super.call(this) || this;
|
15 | _this.owner = owner;
|
16 | return global.__native(_this);
|
17 | }
|
18 | FormatterImpl.prototype.format = function (index) {
|
19 | return this.owner._getItemAsString(index);
|
20 | };
|
21 | var _a;
|
22 | FormatterImpl = __decorate([
|
23 | Interfaces([android.widget.NumberPicker.Formatter]),
|
24 | __metadata("design:paramtypes", [typeof (_a = typeof ListPicker !== "undefined" && ListPicker) === "function" ? _a : Object])
|
25 | ], FormatterImpl);
|
26 | return FormatterImpl;
|
27 | }(java.lang.Object));
|
28 | var ValueChangeListenerImpl = (function (_super) {
|
29 | __extends(ValueChangeListenerImpl, _super);
|
30 | function ValueChangeListenerImpl(owner) {
|
31 | var _this = _super.call(this) || this;
|
32 | _this.owner = owner;
|
33 | return global.__native(_this);
|
34 | }
|
35 | ValueChangeListenerImpl.prototype.onValueChange = function (picker, oldValue, newValue) {
|
36 | selectedIndexProperty.nativeValueChange(this.owner, newValue);
|
37 | this.owner.updateSelectedValue(newValue);
|
38 | };
|
39 | var _a;
|
40 | ValueChangeListenerImpl = __decorate([
|
41 | Interfaces([android.widget.NumberPicker.OnValueChangeListener]),
|
42 | __metadata("design:paramtypes", [typeof (_a = typeof ListPicker !== "undefined" && ListPicker) === "function" ? _a : Object])
|
43 | ], ValueChangeListenerImpl);
|
44 | return ValueChangeListenerImpl;
|
45 | }(java.lang.Object));
|
46 | Formatter = FormatterImpl;
|
47 | ValueChangeListener = ValueChangeListenerImpl;
|
48 | }
|
49 | function getEditText(picker) {
|
50 | for (let i = 0, count = picker.getChildCount(); i < count; i++) {
|
51 | const child = picker.getChildAt(i);
|
52 | if (child instanceof android.widget.EditText) {
|
53 | return child;
|
54 | }
|
55 | }
|
56 | return null;
|
57 | }
|
58 | let selectorWheelPaintField;
|
59 | function getSelectorWheelPaint(picker) {
|
60 | try {
|
61 | selectorWheelPaintField = picker.getClass().getDeclaredField('mSelectorWheelPaint');
|
62 | if (selectorWheelPaintField) {
|
63 | selectorWheelPaintField.setAccessible(true);
|
64 | }
|
65 | }
|
66 | catch (err) {
|
67 |
|
68 | }
|
69 | if (selectorWheelPaintField) {
|
70 | return selectorWheelPaintField.get(picker);
|
71 | }
|
72 | return null;
|
73 | }
|
74 | export class ListPicker extends ListPickerBase {
|
75 | createNativeView() {
|
76 | const picker = new android.widget.NumberPicker(this._context);
|
77 | picker.setDescendantFocusability(android.widget.NumberPicker.FOCUS_BLOCK_DESCENDANTS);
|
78 | picker.setMinValue(0);
|
79 | picker.setMaxValue(0);
|
80 | picker.setValue(0);
|
81 | picker.setWrapSelectorWheel(false);
|
82 | return picker;
|
83 | }
|
84 | initNativeView() {
|
85 | super.initNativeView();
|
86 | initializeNativeClasses();
|
87 | const nativeView = this.nativeViewProtected;
|
88 |
|
89 |
|
90 |
|
91 | this._selectorWheelPaint = getSelectorWheelPaint(nativeView);
|
92 | const formatter = new Formatter(this);
|
93 | nativeView.setFormatter(formatter);
|
94 | nativeView.formatter = formatter;
|
95 | const valueChangedListener = new ValueChangeListener(this);
|
96 | nativeView.setOnValueChangedListener(valueChangedListener);
|
97 | nativeView.valueChangedListener = valueChangedListener;
|
98 | const editText = getEditText(nativeView);
|
99 | if (editText) {
|
100 | nativeView.editText = editText;
|
101 |
|
102 |
|
103 | editText.setFilters([]);
|
104 |
|
105 | editText.setText(' ', android.widget.TextView.BufferType.NORMAL);
|
106 | }
|
107 | }
|
108 | disposeNativeView() {
|
109 | const nativeView = this.nativeViewProtected;
|
110 | if (nativeView?.formatter) {
|
111 | nativeView.formatter.owner = null;
|
112 | }
|
113 | if (nativeView?.valueChangedListener) {
|
114 | nativeView.valueChangedListener.owner = null;
|
115 | }
|
116 | super.disposeNativeView();
|
117 | }
|
118 | _fixNumberPickerRendering() {
|
119 | const nativeView = this.nativeViewProtected;
|
120 |
|
121 | nativeView.setFormatter(null);
|
122 | nativeView.setFormatter(nativeView.formatter);
|
123 | const editText = nativeView.editText;
|
124 | if (editText) {
|
125 | editText.setFilters([]);
|
126 | editText.invalidate();
|
127 | }
|
128 | nativeView.invalidate();
|
129 | }
|
130 | [selectedIndexProperty.getDefault]() {
|
131 | return -1;
|
132 | }
|
133 | [selectedIndexProperty.setNative](value) {
|
134 | if (value >= 0) {
|
135 | this.nativeViewProtected.setValue(value);
|
136 | }
|
137 | }
|
138 | [itemsProperty.getDefault]() {
|
139 | return null;
|
140 | }
|
141 | [itemsProperty.setNative](value) {
|
142 | const maxValue = value && value.length > 0 ? value.length - 1 : 0;
|
143 | this.nativeViewProtected.setMaxValue(maxValue);
|
144 | this._fixNumberPickerRendering();
|
145 |
|
146 | selectedIndexProperty.coerce(this);
|
147 | }
|
148 | [colorProperty.getDefault]() {
|
149 |
|
150 |
|
151 |
|
152 | if (this._selectorWheelPaint) {
|
153 | return this._selectorWheelPaint.getColor();
|
154 | }
|
155 | if (this.nativeView && this.nativeView.getTextColor) {
|
156 | return this.nativeView.getTextColor();
|
157 | }
|
158 | else {
|
159 | return 0;
|
160 | }
|
161 | }
|
162 | [colorProperty.setNative](value) {
|
163 | const color = value instanceof Color ? value.android : value;
|
164 |
|
165 |
|
166 |
|
167 | if (this._selectorWheelPaint) {
|
168 | this._selectorWheelPaint.setColor(color);
|
169 | const editText = this.nativeViewProtected.editText;
|
170 | if (editText) {
|
171 | editText.setTextColor(color);
|
172 | }
|
173 | }
|
174 | else if (this.nativeView && this.nativeView.setTextColor) {
|
175 |
|
176 |
|
177 | this.nativeView.setTextColor(color);
|
178 | }
|
179 | }
|
180 | }
|
181 |
|
\ | No newline at end of file |