UNPKG

7.51 kBJavaScriptView Raw
1import { ListPickerBase, selectedIndexProperty, itemsProperty } from './list-picker-common';
2import { colorProperty } from '../styling/style-properties';
3import { Color } from '../../color';
4export * from './list-picker-common';
5let Formatter;
6let ValueChangeListener;
7function initializeNativeClasses() {
8 if (Formatter) {
9 return;
10 }
11 var FormatterImpl = /** @class */ (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 = /** @class */ (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}
49function 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}
58let selectorWheelPaintField;
59function getSelectorWheelPaint(picker) {
60 try {
61 selectorWheelPaintField = picker.getClass().getDeclaredField('mSelectorWheelPaint');
62 if (selectorWheelPaintField) {
63 selectorWheelPaintField.setAccessible(true);
64 }
65 }
66 catch (err) {
67 // mSelectorWheelPaint is not supported on api level
68 }
69 if (selectorWheelPaintField) {
70 return selectorWheelPaintField.get(picker);
71 }
72 return null;
73}
74export 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 // api28 and lower uses reflection to retrieve and manipulate
89 // android.graphics.Paint object; this is no longer allowed on newer api levels but
90 // equivalent public methods are exposed on api29+ directly on the native widget
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 //Fix the disappearing selected item.
102 //HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
103 editText.setFilters([]);
104 //Since the Android NumberPicker has to always have at least one item, i.e. minValue=maxValue=value=0, we don't want this zero showing up when this.items is empty.
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 //HACK: Force the stubborn NumberPicker to render correctly when we have 0 or 1 items.
121 nativeView.setFormatter(null);
122 nativeView.setFormatter(nativeView.formatter); //Force the NumberPicker to call our Formatter
123 const editText = nativeView.editText;
124 if (editText) {
125 editText.setFilters([]);
126 editText.invalidate(); //Force the EditText to redraw
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 // Coerce selected index after we have set items to native view.
146 selectedIndexProperty.coerce(this);
147 }
148 [colorProperty.getDefault]() {
149 // api28 and lower uses reflection to retrieve and manipulate
150 // android.graphics.Paint object; this is no longer allowed on newer api levels but
151 // equivalent public methods are exposed on api29+ directly on the native widget
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 // api28 and lower uses reflection to retrieve and manipulate
165 // android.graphics.Paint object; this is no longer allowed on newer api levels but
166 // equivalent public methods are exposed on api29+ directly on the native widget
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 // api29 and higher native implementation sets
176 // both wheel color and input text color with single call
177 this.nativeView.setTextColor(color);
178 }
179 }
180}
181//# sourceMappingURL=index.android.js.map
\No newline at end of file