Skip to content

Commit f769e47

Browse files
committed
fix: avoid possible null nativeView and optimize calls
1 parent 3f37612 commit f769e47

27 files changed

+433
-374
lines changed

package-lock.json

+32-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"prepack": "npm run build.native"
3434
},
3535
"devDependencies": {
36-
"@nativescript/core": "^7.0.0",
37-
"@nativescript/types": "^7.0.0",
36+
"@nativescript/core": "^8.0.0",
37+
"@nativescript/types": "^8.0.0",
38+
"@types/node": "^14.6.4",
3839
"prompt": "^1.0.0",
3940
"rimraf": "^2.6.3",
4041
"semver": "^5.6.0",
4142
"tslint": "^5.12.1",
42-
"typescript": "~3.4.5",
43-
"@types/node": "^14.6.4"
43+
"typescript": "~4.2.4"
4444
},
4545
"homepage": "https://github.com/nativescript-rtl/ui",
4646
"author": "xlmnxp",

src/absolute-layout/absolute-layout.android.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export declare class AbsoluteLayout extends Common {
44
initNativeView(): void;
55
addChild(view: View): void;
66
removeChild(view: View): void;
7-
private _updateDirection;
7+
protected _updateDirection;
88
}

src/absolute-layout/absolute-layout.android.ts

+29-31
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { View } from "@nativescript/core/ui";
88
export class AbsoluteLayout extends Common {
99
public initNativeView(): void {
1010
super.initNativeView();
11-
this._updateDirection();
11+
this.scheduleDirectionUpdate();
1212
}
1313

1414
[isRtlProperty.setNative](isRtl: boolean): void {
1515
this.isRtl = isRtl;
16-
this._updateDirection();
16+
this.scheduleDirectionUpdate();
1717
}
1818

1919
[directionProperty.setNative](direction: string) {
@@ -23,51 +23,49 @@ export class AbsoluteLayout extends Common {
2323
this.isRtl = false;
2424
}
2525

26-
this._updateDirection();
26+
this.scheduleDirectionUpdate();
2727
}
2828

2929
public addChild(view: View): void {
3030
super.addChild(view);
3131
if (view.nativeViewProtected) {
32-
this._updateDirection();
32+
this.scheduleDirectionUpdate();
3333
}
3434
}
3535

3636
public removeChild(view: View): void {
3737
super.removeChild(view);
3838
if (view.nativeViewProtected) {
39-
this._updateDirection();
39+
this.scheduleDirectionUpdate();
4040
}
4141
}
4242

43-
private _updateDirection(): void {
44-
setTimeout(() => {
45-
if (this.isRtl) {
46-
this.nativeViewProtected.setRotationY(180);
47-
for (
48-
let viewIndex = 0;
49-
viewIndex < this["getChildrenCount"]();
50-
viewIndex++
51-
) {
52-
let NSView: View = this["getChildAt"](viewIndex);
53-
let isRtl: boolean = NSView["isRtl"] || false;
54-
if (isRtl) {
55-
NSView.nativeView.setRotationY(0);
56-
} else {
57-
NSView.nativeView.setRotationY(180);
58-
}
59-
}
60-
} else {
61-
this.nativeViewProtected.setRotationY(0);
62-
for (
63-
let viewIndex = 0;
64-
viewIndex < this["getChildrenCount"]();
65-
viewIndex++
66-
) {
67-
let NSView: View = this["getChildAt"](viewIndex);
43+
protected _updateDirection(): void {
44+
if (this.isRtl) {
45+
this.nativeViewProtected.setRotationY(180);
46+
for (
47+
let viewIndex = 0;
48+
viewIndex < this["getChildrenCount"]();
49+
viewIndex++
50+
) {
51+
let NSView: View = this["getChildAt"](viewIndex);
52+
let isRtl: boolean = NSView["isRtl"] || false;
53+
if (isRtl) {
6854
NSView.nativeView.setRotationY(0);
55+
} else {
56+
NSView.nativeView.setRotationY(180);
6957
}
7058
}
71-
}, 1);
59+
} else {
60+
this.nativeViewProtected.setRotationY(0);
61+
for (
62+
let viewIndex = 0;
63+
viewIndex < this["getChildrenCount"]();
64+
viewIndex++
65+
) {
66+
let NSView: View = this["getChildAt"](viewIndex);
67+
NSView.nativeView.setRotationY(0);
68+
}
69+
}
7270
}
7371
}

src/absolute-layout/absolute-layout.common.ts

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ import { Property, CssProperty, Style } from "@nativescript/core/ui";
33

44
export class Common extends AbsoluteLayout {
55
public isRtl: boolean;
6+
private _directionScheduled = false;
7+
scheduleDirectionUpdate() {
8+
if (this._directionScheduled) {
9+
return;
10+
}
11+
this._directionScheduled = true;
12+
setTimeout(() => {
13+
this._directionScheduled = false;
14+
if (!this.nativeViewProtected) {
15+
return;
16+
}
17+
this._updateDirection();
18+
}, 1);
19+
}
20+
protected _updateDirection() {
21+
// overriden
22+
};
623
}
724

825
export const isRtlProperty = new Property<Common, boolean>({

src/absolute-layout/absolute-layout.ios.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export declare class AbsoluteLayout extends Common {
44
initNativeView(): void;
55
addChild(view: View): void;
66
removeChild(view: View): void;
7-
private _updateDirection;
7+
protected _updateDirection;
88
}

src/absolute-layout/absolute-layout.ios.ts

+31-34
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,59 @@ import { Screen } from "@nativescript/core/platform";
55
export class AbsoluteLayout extends Common {
66
public initNativeView(): void {
77
super.initNativeView();
8-
this._updateDirection();
8+
this.scheduleDirectionUpdate();
99
}
1010

1111
[isRtlProperty.setNative](isRtl: boolean): void {
1212
this.isRtl = isRtl;
13-
this._updateDirection();
13+
this.scheduleDirectionUpdate();
1414
}
1515

1616
public addChild(view: View): void {
1717
super.addChild(view);
1818
if (view.nativeViewProtected) {
19-
this._updateDirection();
19+
this.scheduleDirectionUpdate();
2020
}
2121
}
2222

2323
public removeChild(view: View): void {
2424
super.removeChild(view);
2525
if (view.nativeViewProtected) {
26-
this._updateDirection();
26+
this.scheduleDirectionUpdate();
2727
}
2828
}
2929

30-
private _updateDirection(): void {
30+
protected _updateDirection(): void {
3131
let ZeroRotation = CATransform3DRotate(CATransform3DIdentity, 0.0, 0.0, 0.0, 0.0);
3232
let RotationInYAxis180Deg = CATransform3DRotate(CATransform3DIdentity, (180 * Math.PI) / 180.0, 0.0, 1.0, 0.0);
33-
34-
setTimeout(() => {
35-
if (this.isRtl) {
36-
this.nativeViewProtected.layer.transform = RotationInYAxis180Deg;
37-
for (
38-
let viewIndex = 0;
39-
viewIndex < this["getChildrenCount"]();
40-
viewIndex++
41-
) {
42-
let NSView: View = this["getChildAt"](viewIndex);
43-
let isRtl: boolean = NSView["isRtl"] || false;
44-
if (isRtl) {
45-
NSView.nativeView.layer.transform = ZeroRotation;
46-
} else {
47-
NSView.nativeView.layer.transform = RotationInYAxis180Deg;
48-
}
49-
NSView.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
50-
}
51-
this.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
52-
} else {
53-
this.nativeViewProtected.layer.transform = ZeroRotation;
54-
for (
55-
let viewIndex = 0;
56-
viewIndex < this["getChildrenCount"]();
57-
viewIndex++
58-
) {
59-
let NSView: View = this["getChildAt"](viewIndex);
33+
if (this.isRtl) {
34+
this.nativeViewProtected.layer.transform = RotationInYAxis180Deg;
35+
for (
36+
let viewIndex = 0;
37+
viewIndex < this["getChildrenCount"]();
38+
viewIndex++
39+
) {
40+
let NSView: View = this["getChildAt"](viewIndex);
41+
let isRtl: boolean = NSView["isRtl"] || false;
42+
if (isRtl) {
6043
NSView.nativeView.layer.transform = ZeroRotation;
61-
NSView.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
44+
} else {
45+
NSView.nativeView.layer.transform = RotationInYAxis180Deg;
6246
}
47+
NSView.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
6348
}
64-
}, 1);
49+
this.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
50+
} else {
51+
this.nativeViewProtected.layer.transform = ZeroRotation;
52+
for (
53+
let viewIndex = 0;
54+
viewIndex < this["getChildrenCount"]();
55+
viewIndex++
56+
) {
57+
let NSView: View = this["getChildAt"](viewIndex);
58+
NSView.nativeView.layer.transform = ZeroRotation;
59+
NSView.nativeViewProtected.layer.rasterizationScale = Screen.mainScreen.scale;
60+
}
61+
}
6562
}
6663
}

src/dock-layout/dock-layout.android.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export declare class DockLayout extends Common {
44
initNativeView(): void;
55
addChild(view: View): void;
66
removeChild(view: View): void;
7-
private _updateDirection;
7+
protected _updateDirection;
88
}

0 commit comments

Comments
 (0)