8000 fix(android-list-picker): NoSuchFieldException on api29 (#7790) · NativeScript/NativeScript@997d6de · GitHub
[go: up one dir, main page]

Skip to content

Commit 997d6de

Browse files
authored
fix(android-list-picker): NoSuchFieldException on api29 (#7790)
1 parent c6efd08 commit 997d6de

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

tns-core-modules/ui/list-picker/list-picker.android.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { ListPickerBase, selectedIndexProperty, itemsProperty, colorProperty, Color } from "./list-picker-common";
22
import { ItemsSource } from ".";
3+
import { device } from "../../platform";
4+
import lazy from "../../utils/lazy";
35

46
export * from "./list-picker-common";
57

8+
const sdkVersion = lazy(() => parseInt(device.sdkVersion));
9+
610
interface Formatter {
711
new (owner: ListPicker): android.widget.NumberPicker.Formatter;
812
}
@@ -90,7 +94,14 @@ export class ListPicker extends ListPickerBase {
9094
super.initNativeView();
9195
initializeNativeClasses();
9296
const nativeView = this.nativeViewProtected;
93-
this._selectorWheelPaint = getSelectorWheelPaint(nativeView);
97+
98+
// api28 and lower uses reflection to retrieve and manipulate
99+
// android.graphics.Paint object; this is no longer allowed on newer api levels but
100+
// equivalent public methods are exposed on api29+ directly on the native widget
101+
if (sdkVersion() < 29) {
102+
this._selectorWheelPaint = getSelectorWheelPaint(nativeView);
103+
}
104+
94105
const formatter = new Formatter(this);
95106
nativeView.setFormatter(formatter);
96107
(<any>nativeView).formatter = formatter;
@@ -153,28 +164,33 @@ export class ListPicker extends ListPickerBase {
153164
selectedIndexProperty.coerce(this);
154165
}
155166

156-
[colorProperty.getDefault](): { wheelColor: number, textColor: number } {
157-
const editText = (<any>this.nativeViewProtected).editText;
167+
[colorProperty.getDefault](): number {
168+
// api28 and lower uses reflection to retrieve and manipulate
169+
// android.graphics.Paint object; this is no longer allowed on newer api levels but
170+
// equivalent public methods are exposed on api29+ directly on the native widget
171+
if (this._selectorWheelPaint) {
172+
return this._selectorWheelPaint.getColor();
173+
}
158174

159-
return {
160-
wheelColor: this._selectorWheelPaint.getColor(),
161-
textColor: editText ? editText.getTextColors().getDefaultColor() : -1
162-
};
175+
return this.nativeView.getTextColor();
163176
}
164-
[colorProperty.setNative](value: { wheelColor: number, textColor: number } | Color) {
165-
let color: number;
166-
let wheelColor: number;
167-
if (value instanceof Color) {
168-
color = wheelColor = value.android;
177+
[colorProperty.setNative](value: number | Color) {
178+
const color = value instanceof Color ? value.android : value;
179+
180+
// api28 and lower uses reflection to retrieve and manipulate
181+
// android.graphics.Paint object; this is no longer allowed on newer api levels but
182+
// equivalent public methods are exposed on api29+ directly on the native widget
183+
if (this._selectorWheelPaint) {
184+
this._selectorWheelPaint.setColor(color);
185+
186+
const editText = (<any>this.nativeViewProtected).editText;
187+
if (editText) {
188+
editText.setTextColor(color);
189+
}
169190
} else {
170-
color = value.textColor;
171-
wheelColor = value.wheelColor;
172-
}
173-
174-
this._selectorWheelPaint.setColor(wheelColor);
175-
const editText = (<any>this.nativeViewProtected).editText;
176-
if (editText) {
177-
editText.setTextColor(color);
191+
// api29 and higher native implementation sets
192+
// both wheel color and input text color with single call
193+
this.nativeView.setTextColor(color);
178194
}
179195
}
180196
}

0 commit comments

Comments
 (0)
0