Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(android): refactor to make less calls to native #9119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions packages/core/ui/core/view/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ const statePressed = 16842919; // android.R.attr.state_pressed
const stateEnabled = 16842910; // android.R.attr.state_enabled
const styleAnimationDialog = 16973826; // android.R.style.Animation_Dialog

const VERTICAL_GRAVITY_MASK = 112; // android.view.Gravity.VERTICAL_GRAVITY_MASK
const HORIZONTAL_GRAVITY_MASK = 7; // android.view.Gravity.HORIZONTAL_GRAVITY_MASK
const GRAVITY_LEFT = 3; // android.view.Gravity.LEFT
const GRAVITY_RIGHT = 5; // android.view.Gravity.RIGHT
const GRAVITY_TOP = 48; // android.view.Gravity.TOP
const GRAVITY_BOTTOM = 80; // android.view.Gravity.BOTTOM
const GRAVITY_CENTER_HORIZONTAL = 1; // android.view.Gravity.CENTER_HORIZONTAL
const GRAVITY_FILL_HORIZONTAL = 7; // android.view.Gravity.FILL_HORIZONTAL
const GRAVITY_CENTER_VERTICAL = 16; // android.view.Gravity.CENTER_VERTICAL
const GRAVITY_FILL_VERTICAL = 112; // android.view.Gravity.FILL_VERTICAL

const sdkVersion = lazy(() => parseInt(Device.sdkVersion));

const modalMap = new Map<number, DialogOptions>();
Expand Down Expand Up @@ -932,30 +943,32 @@ export class View extends ViewCommon {
[horizontalAlignmentProperty.setNative](value: CoreTypes.HorizontalAlignmentType) {
const nativeView = this.nativeViewProtected;
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
const gravity = lp.gravity;
const weight = lp.weight;

This comment was marked as abuse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I would not. I forgot a if lp.gravity which makes 2 calls to native accesser. Need to fix this. The idea is to make sure we don't access those native variables multiple times => multiple jni calls

// Set only if params gravity exists.
if (lp.gravity !== undefined) {
if (gravity !== undefined) {
switch (value) {
case 'left':
lp.gravity = android.view.Gravity.LEFT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
if (lp.weight < 0) {
lp.gravity = GRAVITY_LEFT | (gravity & VERTICAL_GRAVITY_MASK);
if (weight < 0) {
lp.weight = -2;
}
break;
case 'center':
lp.gravity = android.view.Gravity.CENTER_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
if (lp.weight < 0) {
lp.gravity = GRAVITY_CENTER_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
if (weight < 0) {
lp.weight = -2;
}
break;
case 'right':
lp.gravity = android.view.Gravity.RIGHT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
if (lp.weight < 0) {
lp.gravity = GRAVITY_RIGHT | (gravity & VERTICAL_GRAVITY_MASK);
if (weight < 0) {
lp.weight = -2;
}
break;
case 'stretch':
lp.gravity = android.view.Gravity.FILL_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
if (lp.weight < 0) {
lp.gravity = GRAVITY_FILL_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
if (weight < 0) {
lp.weight = -1;
}
break;
Expand All @@ -970,30 +983,32 @@ export class View extends ViewCommon {
[verticalAlignmentProperty.setNative](value: CoreTypes.VerticalAlignmentType) {
const nativeView = this.nativeViewProtected;
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
const gravity = lp.gravity;
const height = lp.height;

This comment was marked as abuse.

// Set only if params gravity exists.
if (lp.gravity !== undefined) {
if (gravity !== undefined) {
switch (value) {
case 'top':
lp.gravity = android.view.Gravity.TOP | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
if (lp.height < 0) {
lp.gravity = GRAVITY_TOP | (gravity & HORIZONTAL_GRAVITY_MASK);
if (height < 0) {
lp.height = -2;
}
break;
case 'middle':
lp.gravity = android.view.Gravity.CENTER_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
if (lp.height < 0) {
lp.gravity = GRAVITY_CENTER_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
if (height < 0) {
lp.height = -2;
}
break;
case 'bottom':
lp.gravity = android.view.Gravity.BOTTOM | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
if (lp.height < 0) {
lp.gravity = GRAVITY_BOTTOM | (gravity & HORIZONTAL_GRAVITY_MASK);
if (height < 0) {
lp.height = -2;
}
break;
case 'stretch':
lp.gravity = android.view.Gravity.FILL_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
if (lp.height < 0) {
lp.gravity = GRAVITY_FILL_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
if (height < 0) {
lp.height = -1;
}
break;
Expand Down