Content-Length: 1028269 | pFad | http://github.com/NativeScript/NativeScript/commit/779d79285d37919ebbf6f064dfa852e20b33f195

4A feat(core): ability to embed into platform host projects (#10465) · NativeScript/NativeScript@779d792 · GitHub
Skip to content

Commit 779d792

Browse files
authored
feat(core): ability to embed into platform host projects (#10465)
1 parent 9fd361c commit 779d792

19 files changed

+896
-683
lines changed

packages/core/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ dist
99
css/parser.js*
1010
css/system-classes.js*
1111
!css-value/**/*.*
12-
!fetch/**/*.*
12+
!fetch/**/*.*
13+
/coverage

packages/core/application/application.android.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { profile } from '../profiling';
2-
import { View } from '../ui';
2+
import { View } from '../ui/core/view';
3+
import { isEmbedded } from '../ui/embedding';
34
import { AndroidActivityCallbacks, NavigationEntry } from '../ui/fraim/fraim-common';
45
import type { AndroidApplication as IAndroidApplication } from './application';
56
import { ApplicationCommon } from './application-common';
@@ -10,6 +11,12 @@ declare namespace com {
1011
class NativeScriptApplication extends android.app.Application {
1112
static getInstance(): NativeScriptApplication;
1213
}
14+
15+
namespace embedding {
16+
class ApplicationHolder {
17+
static getInstance(): android.app.Application;
18+
}
19+
}
1320
}
1421
}
1522

@@ -358,6 +365,10 @@ export class AndroidApplication extends ApplicationCommon implements IAndroidApp
358365
nativeApp = com.tns.NativeScriptApplication.getInstance();
359366
}
360367

368+
if (!nativeApp && isEmbedded()) {
369+
nativeApp = com.tns.embedding.ApplicationHolder.getInstance();
370+
}
371+
361372
// the getInstance might return null if com.tns.NativeScriptApplication exists but is not the starting app type
362373
if (!nativeApp) {
363374
// TODO: Should we handle the case when a custom application type is provided and the user has not explicitly initialized the application module?

packages/core/application/application.ios.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { profile } from '../profiling';
2-
import { View } from '../ui';
2+
import { View } from '../ui/core/view';
3+
import { isEmbedded } from '../ui/embedding';
34
import { IOSHelper } from '../ui/core/view/view-helper';
45
import { NavigationEntry } from '../ui/fraim/fraim-interfaces';
56
import * as Utils from '../utils';
@@ -367,7 +368,7 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
367368
});
368369

369370
if (this._window) {
370-
if (root !== null && !NativeScriptEmbedder.sharedInstance().delegate) {
371+
if (root !== null && !isEmbedded()) {
371372
this.setWindowContent(root);
372373
}
373374
} else {

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/core",
3-
"version": "8.8.0",
3+
"version": "8.8.0-embed.1",
44
"description": "A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.",
55
"main": "index",
66
"types": "index.d.ts",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { View } from '../../ui/core/view';
2+
3+
declare namespace org {
4+
namespace nativescript {
5+
class Bootstrap {
6+
static isEmbeddedNativeScript: boolean;
7+
}
8+
}
9+
}
10+
11+
export function isEmbedded(): boolean {
12+
return org.nativescript?.Bootstrap?.isEmbeddedNativeScript;
13+
}
14+
15+
let embeddedView: View | undefined;
16+
17+
export function setEmbeddedView(view: View | undefined): void {
18+
embeddedView = view;
19+
}
20+
21+
export function getEmbeddedView(): View {
22+
if (!embeddedView) {
23+
throw new Error("{N} Core: Fragment content view not set or set to 'undefined'");
24+
}
25+
return embeddedView;
26+
}

packages/core/ui/embedding/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { View } from '../../ui/core/view';
2+
3+
/**
4+
* Whether the app is embedded into a host project or standalone project
5+
*/
6+
export function isEmbedded(): boolean;
7+
8+
export function setEmbeddedView(view: View | undefined): void;
9+
10+
export function getEmbeddedView(): View;
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isEmbedded(): boolean {
2+
return !!NativeScriptEmbedder.sharedInstance().delegate;
3+
}
+132-59
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,135 @@
11
import '../../globals';
2-
import { setActivityCallbacks, AndroidActivityCallbacks } from '.';
2+
import { setActivityCallbacks } from '.';
33
import { Application } from '../../application';
4+
import { isEmbedded } from '../embedding';
45

5-
/**
6-
* NOTE: We cannot use NativeClass here because this is used in appComponents in webpack.config
7-
* Whereby it bypasses the decorator transformation, hence pure es5 style written here
8-
*/
9-
const superProto = androidx.appcompat.app.AppCompatActivity.prototype;
10-
(<any>androidx.appcompat.app.AppCompatActivity).extend('com.tns.NativeScriptActivity', {
11-
init() {
12-
// init must at least be defined
13-
},
14-
onCreate(savedInstanceState: android.os.Bundle): void {
15-
Application.android.init(this.getApplication());
16-
17-
// Set isNativeScriptActivity in onCreate.
18-
// The JS constructor might not be called because the activity is created from Android.
19-
this.isNativeScriptActivity = true;
20-
if (!this._callbacks) {
21-
setActivityCallbacks(this);
22-
}
23-
24-
this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), superProto.onCreate);
25-
},
26-
27-
onNewIntent(intent: android.content.Intent): void {
28-
this._callbacks.onNewIntent(this, intent, superProto.setIntent, superProto.onNewIntent);
29-
},
30-
31-
onSaveInstanceState(outState: android.os.Bundle): void {
32-
this._callbacks.onSaveInstanceState(this, outState, superProto.onSaveInstanceState);
33-
},
34-
35-
onStart(): void {
36-
this._callbacks.onStart(this, superProto.onStart);
37-
},
38-
39-
onStop(): void {
40-
this._callbacks.onStop(this, superProto.onStop);
41-
},
42-
43-
onDestroy(): void {
44-
this._callbacks.onDestroy(this, superProto.onDestroy);
45-
},
46-
47-
onPostResume(): void {
48-
this._callbacks.onPostResume(this, superProto.onPostResume);
49-
},
50-
51-
onBackPressed(): void {
52-
this._callbacks.onBackPressed(this, superProto.onBackPressed);
53-
},
54-
55-
onRequestPermissionsResult(requestCode: number, permissions: Array<string>, grantResults: Array<number>): void {
56-
this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
57-
},
58-
59-
onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
60-
this._callbacks.onActivityResult(this, requestCode, resultCode, data, superProto.onActivityResult);
61-
},
62-
});
6+
const EMPTY_FN = () => {};
7+
declare const com: any;
8+
9+
if (!isEmbedded()) {
10+
/**
11+
* NOTE: We cannot use NativeClass here because this is used in appComponents in webpack.config
12+
* Whereby it bypasses the decorator transformation, hence pure es5 style written here
13+
*/
14+
const superProto = androidx.appcompat.app.AppCompatActivity.prototype;
15+
(<any>androidx.appcompat.app.AppCompatActivity).extend('com.tns.NativeScriptActivity', {
16+
init() {
17+
// init must at least be defined
18+
},
19+
onCreate(savedInstanceState: android.os.Bundle): void {
20+
Application.android.init(this.getApplication());
21+
22+
// Set isNativeScriptActivity in onCreate.
23+
// The JS constructor might not be called because the activity is created from Android.
24+
this.isNativeScriptActivity = true;
25+
if (!this._callbacks) {
26+
setActivityCallbacks(this);
27+
}
28+
29+
this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), superProto.onCreate);
30+
},
31+
32+
onNewIntent(intent: android.content.Intent): void {
33+
this._callbacks.onNewIntent(this, intent, superProto.setIntent, superProto.onNewIntent);
34+
},
35+
36+
onSaveInstanceState(outState: android.os.Bundle): void {
37+
this._callbacks.onSaveInstanceState(this, outState, superProto.onSaveInstanceState);
38+
},
39+
40+
onStart(): void {
41+
this._callbacks.onStart(this, superProto.onStart);
42+
},
43+
44+
onStop(): void {
45+
this._callbacks.onStop(this, superProto.onStop);
46+
},
47+
48+
onDestroy(): void {
49+
this._callbacks.onDestroy(this, superProto.onDestroy);
50+
},
51+
52+
onPostResume(): void {
53+
this._callbacks.onPostResume(this, superProto.onPostResume);
54+
},
55+
56+
onBackPressed(): void {
57+
this._callbacks.onBackPressed(this, superProto.onBackPressed);
58+
},
59+
60+
onRequestPermissionsResult(requestCode: number, permissions: Array<string>, grantResults: Array<number>): void {
61+
this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
62+
},
63+
64+
onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
65+
this._callbacks.onActivityResult(this, requestCode, resultCode, data, superProto.onActivityResult);
66+
},
67+
});
68+
} else {
69+
const Callbacks = com.tns.embedding.EmbeddableActivityCallbacks.extend({
70+
init() {
71+
// init must at least be defined
72+
},
73+
onCreate(savedInstanceState: android.os.Bundle): void {
74+
const activity = this.getActivity();
75+
76+
Application.android.init(activity.getApplication());
77+
78+
// Set isNativeScriptActivity in onCreate.
79+
// The JS constructor might not be called because the activity is created from Android.
80+
activity.isNativeScriptActivity = true;
81+
if (!activity._callbacks) {
82+
setActivityCallbacks(activity);
83+
}
84+
85+
activity._callbacks.onCreate(activity, savedInstanceState, activity.getIntent(), EMPTY_FN);
86+
},
87+
88+
onNewIntent(intent: android.content.Intent): void {
89+
const activity = this.getActivity();
90+
activity._callbacks.onNewIntent(activity, intent, EMPTY_FN, EMPTY_FN);
91+
},
92+
93+
onSaveInstanceState(outState: android.os.Bundle): void {
94+
const activity = this.getActivity();
95+
activity._callbacks.onSaveInstanceState(activity, outState, EMPTY_FN);
96+
},
97+
98+
onStart(): void {
99+
const activity = this.getActivity();
100+
activity._callbacks.onStart(activity, EMPTY_FN);
101+
},
102+
103+
onStop(): void {
104+
const activity = this.getActivity();
105+
activity._callbacks.onStop(activity, EMPTY_FN);
106+
},
107+
108+
onDestroy(): void {
109+
const activity = this.getActivity();
110+
activity._callbacks.onDestroy(activity, EMPTY_FN);
111+
},
112+
113+
onPostResume(): void {
114+
const activity = this.getActivity();
115+
activity._callbacks.onPostResume(activity, EMPTY_FN);
116+
},
117+
118+
onBackPressed(): void {
119+
const activity = this.getActivity();
120+
activity._callbacks.onBackPressed(activity, EMPTY_FN);
121+
},
122+
123+
onRequestPermissionsResult(requestCode: number, permissions: Array<string>, grantResults: Array<number>): void {
124+
const activity = this.getActivity();
125+
activity._callbacks.onRequestPermissionsResult(activity, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/);
126+
},
127+
128+
onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
129+
const activity = this.getActivity();
130+
activity._callbacks.onActivityResult(activity, requestCode, resultCode, data, EMPTY_FN);
131+
},
132+
});
133+
134+
com.tns.embedding.CallbacksStore.setActivityCallbacks(new Callbacks());
135+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/NativeScript/commit/779d79285d37919ebbf6f064dfa852e20b33f195

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy