Skip to content

Commit e556c6d

Browse files
committed
chore: refactor
1 parent b254dfe commit e556c6d

File tree

6 files changed

+34
-46
lines changed

6 files changed

+34
-46
lines changed

src/button/button.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ export class Button extends ButtonBase {
125125
}
126126
}
127127
[elevationProperty.setNative](value: number) {
128-
if (isPostLollipop()) {
128+
if (isPostLollipop) {
129129
this.createStateListAnimator();
130130
} else {
131131
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
132132
androidx.core.view.ViewCompat.setElevation(this.nativeViewProtected, newValue);
133133
}
134134
}
135135
[dynamicElevationOffsetProperty.setNative](value: number) {
136-
if (isPostLollipop()) {
136+
if (isPostLollipop) {
137137
this.createStateListAnimator();
138138
} else {
139139
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);

src/cardview/cardview.android.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ export class CardView extends CardViewBase {
229229
_borderRadius = 0;
230230

231231
getDefaultElevation(): number {
232-
if (isPostLollipop()) {
232+
if (isPostLollipop) {
233233
return 1; // 1dp @dimen/mtrl_card_elevation
234234
}
235235
return 0; // 1dp @dimen/mtrl_card_elevation
236236
}
237237

238238
getDefaultDynamicElevationOffset(): number {
239-
if (isPostLollipop()) {
239+
if (isPostLollipop) {
240240
return 5; // 5dp @dimen/mtrl_card_dragged_z
241241
}
242242

@@ -247,7 +247,7 @@ export class CardView extends CardViewBase {
247247
initMDCCardView();
248248
initializeOutlineProvider();
249249
const view = new MDCCardView(this._context);
250-
if (isPostLollipop()) {
250+
if (isPostLollipop) {
251251
createStateListAnimator(this, view);
252252
}
253253
view.setClickable(this.isUserInteractionEnabled);
@@ -342,7 +342,7 @@ export class CardView extends CardViewBase {
342342
if (!this.nativeViewProtected) {
343343
return;
344344
}
345-
if (isPostLollipop()) {
345+
if (isPostLollipop) {
346346
createStateListAnimator(this, this.nativeViewProtected);
347347
} else {
348348
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
@@ -353,7 +353,7 @@ export class CardView extends CardViewBase {
353353
if (!this.nativeViewProtected) {
354354
return;
355355
}
356-
if (isPostLollipop()) {
356+
if (isPostLollipop) {
357357
createStateListAnimator(this, this.nativeViewProtected);
358358
} else {
359359
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
@@ -362,7 +362,7 @@ export class CardView extends CardViewBase {
362362
}
363363
[rippleColorProperty.setNative](color: Color) {
364364
const rippleColor = color ? color.android : -1;
365-
// if (isPostLollipopMR1()) {
365+
// if (isPostLollipopMR1) {
366366
// (this.rippleDrawable as android.graphics.drawable.RippleDrawable).setColor(getColorStateList(rippleColor));
367367
// } else {
368368
// this.rippleShape.getPaint().setColor(rippleColor);

src/core/android/utils.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import { VerticalTextAlignment } from '@nativescript-community/text';
2-
import { Application, Color, CoreTypes, Utils, ViewBase, profile } from '@nativescript/core';
2+
import { CoreTypes, Device, Utils, ViewBase, profile } from '@nativescript/core';
33

4-
let isPostLollipopVar: boolean;
5-
export function isPostLollipop() {
6-
if (isPostLollipopVar === undefined) {
7-
isPostLollipopVar = android.os.Build.VERSION.SDK_INT >= 21;
8-
}
9-
return isPostLollipopVar;
10-
}
11-
12-
let isPostLollipopMR1Var: boolean;
13-
export function isPostLollipopMR1() {
14-
if (isPostLollipopMR1Var === undefined) {
15-
isPostLollipopMR1Var = android.os.Build.VERSION.SDK_INT >= 22;
16-
}
17-
return isPostLollipopMR1Var;
18-
}
19-
let isPostMarshmallowVar: boolean;
20-
export function isPostMarshmallow() {
21-
if (isPostMarshmallowVar === undefined) {
22-
isPostMarshmallowVar = android.os.Build.VERSION.SDK_INT >= 23;
23-
}
24-
return isPostMarshmallowVar;
25-
}
4+
export const sdkVersion = parseInt(Device.sdkVersion, 10);
5+
export const isPostLollipop = sdkVersion >= 21;
6+
export const isPostLollipopMR1 = sdkVersion >= 22;
7+
export const isPostMarshmallow = sdkVersion >= 23;
268

279
export const state = {
2810
get selected() {

src/core/index.android.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export function overrideViewBase() {
235235
setRippleDrawable(view: android.view.View, radius = 0) {
236236
if (!this.rippleDrawable) {
237237
this.rippleDrawable = createRippleDrawable(this.getRippleColor(), radius);
238-
if (isPostMarshmallow()) {
238+
if (isPostMarshmallow) {
239239
view.setForeground(this.rippleDrawable);
240240
}
241241
}
@@ -244,7 +244,7 @@ export function overrideViewBase() {
244244
const rippleColor = getRippleColor(color);
245245
const nativeViewProtected = this.nativeViewProtected;
246246
const RippleDrawable = android.graphics.drawable.RippleDrawable;
247-
if (this instanceof Button && isPostMarshmallow()) {
247+
if (this instanceof Button && isPostMarshmallow) {
248248
const foreground = (nativeViewProtected as android.widget.Button).getForeground();
249249
if (foreground instanceof RippleDrawable) {
250250
foreground.setColor(getColorStateList(rippleColor));
@@ -261,7 +261,7 @@ export function overrideViewBase() {
261261
if (!rippleDrawable) {
262262
this.setRippleDrawable(nativeViewProtected, Length.toDevicePixels(this.style.borderTopLeftRadius));
263263
} else {
264-
if (isPostLollipop()) {
264+
if (isPostLollipop) {
265265
(rippleDrawable as android.graphics.drawable.RippleDrawable).setColor(getColorStateList(rippleColor));
266266
} else if ((rippleDrawable as any).rippleShape) {
267267
(rippleDrawable as any).rippleShape.getPaint().setColor(rippleColor);
@@ -301,7 +301,7 @@ export function overrideViewBase() {
301301
}
302302

303303
[elevationProperty.setNative](value: number) {
304-
if (isPostLollipop()) {
304+
if (isPostLollipop) {
305305
this.createStateListAnimator();
306306
} else {
307307
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
@@ -320,7 +320,7 @@ export function overrideViewBase() {
320320
}
321321
[dynamicElevationOffsetProperty.setNative](value: number) {
322322
this.nativeViewProtected.setClickable(this.isUserInteractionEnabled);
323-
if (isPostLollipop()) {
323+
if (isPostLollipop) {
324324
this.createStateListAnimator();
325325
} else {
326326
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);

src/floatingactionbutton/floatingactionbutton.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ export class FloatingActionButton extends FloatingActionButtonBase {
4747
}
4848
}
4949
[elevationProperty.setNative](value: number) {
50-
if (isPostLollipop()) {
50+
if (isPostLollipop) {
5151
this.createStateListAnimator();
5252
} else {
5353
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
5454
androidx.core.view.ViewCompat.setElevation(this.nativeViewProtected, newValue);
5555
}
5656
}
5757
[dynamicElevationOffsetProperty.setNative](value: number) {
58-
if (isPostLollipop()) {
58+
if (isPostLollipop) {
5959
this.createStateListAnimator();
6060
} else {
6161
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);

src/ripple/ripple.android.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getRippleColor, rippleColorProperty, themer } from '@nativescript-community/ui-material-core';
2-
import { Background, Color, backgroundInternalProperty } from '@nativescript/core';
3-
import { createRippleDrawable, getAttrColor, getColorStateList, isPostLollipopMR1, isPostMarshmallow } from '@nativescript-community/ui-material-core/android/utils';
2+
import { createRippleDrawable, getColorStateList, isPostLollipopMR1, isPostMarshmallow } from '@nativescript-community/ui-material-core/android/utils';
3+
import { Background, Color, Length, backgroundInternalProperty } from '@nativescript/core';
44
import { RippleBase } from './ripple-common';
55

66
let MDStackLayout: typeof org.nativescript.widgets.StackLayout;
77

88
const DEFAULT_STROKE_VALUE = -1;
99
function initMDStackLayout() {
1010
if (!MDStackLayout) {
11-
if (isPostLollipopMR1()) {
11+
if (isPostLollipopMR1) {
1212
MDStackLayout = org.nativescript.widgets.StackLayout;
1313
} else {
1414
initializePreLollipopStackLayout();
@@ -213,19 +213,25 @@ export class Ripple extends RippleBase {
213213
}
214214
return getRippleColor(themer.getAccentColor());
215215
}
216-
setRippleDrawable(view: android.view.View, radius = 0) {
216+
setRippleDrawable(view: android.view.View, topLeftRadius = 0, topRightRadius = 0, bottomRightRadius = 0, bottomLeftRadius = 0) {
217217
if (!this.rippleDrawable) {
218-
this.rippleDrawable = createRippleDrawable(this.getRippleColor(), radius);
219-
if (isPostMarshmallow()) {
218+
this.rippleDrawable = createRippleDrawable(this.getRippleColor(), topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius);
219+
if (isPostMarshmallow) {
220220
view.setForeground(this.rippleDrawable);
221221
}
222222
}
223223
}
224224
[rippleColorProperty.setNative](color: Color) {
225225
if (!this.rippleDrawable) {
226-
this.setRippleDrawable(this.nativeViewProtected);
226+
this.setRippleDrawable(
227+
this.nativeViewProtected,
228+
Length.toDevicePixels(this.style.borderTopLeftRadius),
229+
Length.toDevicePixels(this.style.borderTopRightRadius),
230+
Length.toDevicePixels(this.style.borderBottomRightRadius),
231+
Length.toDevicePixels(this.style.borderBottomLeftRadius)
232+
);
227233
} else {
228-
if (isPostLollipopMR1()) {
234+
if (isPostLollipopMR1) {
229235
(this.rippleDrawable as android.graphics.drawable.RippleDrawable).setColor(getColorStateList(color.android));
230236
} else {
231237
(this.rippleDrawable as any).rippleShape.getPaint().setColor(getRippleColor(color));

0 commit comments

Comments
 (0)