mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00
feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
This commit is contained in:

committed by
GitHub

parent
6c7139477e
commit
cc97a16800
1
nativescript-core/application/Readme.md
Normal file
1
nativescript-core/application/Readme.md
Normal file
@ -0,0 +1 @@
|
||||
Contains the application abstraction with all related methods.
|
177
nativescript-core/application/application-common.ts
Normal file
177
nativescript-core/application/application-common.ts
Normal file
@ -0,0 +1,177 @@
|
||||
// Require globals first so that snapshot takes __extends function.
|
||||
import "../globals";
|
||||
import { Observable, EventData } from "../data/observable";
|
||||
import { View } from "../ui/core/view";
|
||||
import {
|
||||
trace as profilingTrace,
|
||||
time,
|
||||
uptime,
|
||||
level as profilingLevel,
|
||||
} from "../profiling";
|
||||
|
||||
const events = new Observable();
|
||||
let launched = false;
|
||||
function setLaunched() {
|
||||
launched = true;
|
||||
events.off("launch", setLaunched);
|
||||
}
|
||||
events.on("launch", setLaunched);
|
||||
|
||||
if (profilingLevel() > 0) {
|
||||
events.on("displayed", () => {
|
||||
const duration = uptime();
|
||||
const end = time();
|
||||
const start = end - duration;
|
||||
profilingTrace(`Displayed in ${duration.toFixed(2)}ms`, start, end);
|
||||
});
|
||||
}
|
||||
|
||||
export function hasLaunched(): boolean {
|
||||
return launched;
|
||||
}
|
||||
|
||||
export { Observable };
|
||||
|
||||
import {
|
||||
AndroidApplication,
|
||||
CssChangedEventData,
|
||||
DiscardedErrorEventData,
|
||||
iOSApplication,
|
||||
LoadAppCSSEventData,
|
||||
UnhandledErrorEventData
|
||||
} from "./application";
|
||||
|
||||
import { CLASS_PREFIX, pushToRootViewCssClasses, removeFromRootViewCssClasses } from "../css/system-classes";
|
||||
import { DeviceOrientation, SystemAppearance } from "../ui/enums/enums";
|
||||
|
||||
export { UnhandledErrorEventData, DiscardedErrorEventData, CssChangedEventData, LoadAppCSSEventData };
|
||||
|
||||
export const launchEvent = "launch";
|
||||
export const suspendEvent = "suspend";
|
||||
export const displayedEvent = "displayed";
|
||||
export const resumeEvent = "resume";
|
||||
export const exitEvent = "exit";
|
||||
export const lowMemoryEvent = "lowMemory";
|
||||
export const uncaughtErrorEvent = "uncaughtError";
|
||||
export const discardedErrorEvent = "discardedError";
|
||||
export const orientationChangedEvent = "orientationChanged";
|
||||
export const systemAppearanceChangedEvent = "systemAppearanceChanged";
|
||||
|
||||
const ORIENTATION_CSS_CLASSES = [
|
||||
`${CLASS_PREFIX}${DeviceOrientation.portrait}`,
|
||||
`${CLASS_PREFIX}${DeviceOrientation.landscape}`,
|
||||
`${CLASS_PREFIX}${DeviceOrientation.unknown}`
|
||||
];
|
||||
|
||||
const SYSTEM_APPEARANCE_CSS_CLASSES = [
|
||||
`${CLASS_PREFIX}${SystemAppearance.light}`,
|
||||
`${CLASS_PREFIX}${SystemAppearance.dark}`
|
||||
];
|
||||
|
||||
let cssFile: string = "./app.css";
|
||||
|
||||
let resources: any = {};
|
||||
|
||||
export function getResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
export function setResources(res: any) {
|
||||
resources = res;
|
||||
}
|
||||
|
||||
export let android = undefined;
|
||||
export let ios = undefined;
|
||||
|
||||
export const on: typeof events.on = events.on.bind(events);
|
||||
export const off: typeof events.off = events.off.bind(events);
|
||||
export const notify: typeof events.notify = events.notify.bind(events);
|
||||
export const hasListeners: typeof events.hasListeners = events.hasListeners.bind(events);
|
||||
|
||||
let app: iOSApplication | AndroidApplication;
|
||||
export function setApplication(instance: iOSApplication | AndroidApplication): void {
|
||||
app = instance;
|
||||
}
|
||||
|
||||
export function livesync(rootView: View, context?: ModuleContext) {
|
||||
events.notify(<EventData>{ eventName: "livesync", object: app });
|
||||
const liveSyncCore = global.__onLiveSyncCore;
|
||||
let reapplyAppStyles = false;
|
||||
|
||||
// ModuleContext is available only for Hot Module Replacement
|
||||
if (context && context.path) {
|
||||
const styleExtensions = ["css", "scss"];
|
||||
const appStylesFullFileName = getCssFileName();
|
||||
const appStylesFileName = appStylesFullFileName.substring(0, appStylesFullFileName.lastIndexOf(".") + 1);
|
||||
reapplyAppStyles = styleExtensions.some(ext => context.path === appStylesFileName.concat(ext));
|
||||
}
|
||||
|
||||
// Handle application styles
|
||||
if (rootView && reapplyAppStyles) {
|
||||
rootView._onCssStateChange();
|
||||
} else if (liveSyncCore) {
|
||||
liveSyncCore(context);
|
||||
}
|
||||
}
|
||||
|
||||
export function setCssFileName(cssFileName: string) {
|
||||
cssFile = cssFileName;
|
||||
events.notify(<CssChangedEventData>{ eventName: "cssChanged", object: app, cssFile: cssFileName });
|
||||
}
|
||||
|
||||
export function getCssFileName(): string {
|
||||
return cssFile;
|
||||
}
|
||||
|
||||
export function loadAppCss(): void {
|
||||
try {
|
||||
events.notify(<LoadAppCSSEventData>{ eventName: "loadAppCss", object: app, cssFile: getCssFileName() });
|
||||
} catch (e) {
|
||||
throw new Error(`The file ${getCssFileName()} couldn't be loaded! ` +
|
||||
`You may need to register it inside ./app/vendor.ts.`);
|
||||
}
|
||||
}
|
||||
|
||||
function applyCssClass(rootView: View, cssClass: string) {
|
||||
pushToRootViewCssClasses(cssClass);
|
||||
rootView.cssClasses.add(cssClass);
|
||||
}
|
||||
|
||||
function removeCssClass(rootView: View, cssClass: string) {
|
||||
removeFromRootViewCssClasses(cssClass);
|
||||
rootView.cssClasses.delete(cssClass);
|
||||
}
|
||||
|
||||
export function orientationChanged(rootView: View, newOrientation: "portrait" | "landscape" | "unknown"): void {
|
||||
if (!rootView) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newOrientationCssClass = `${CLASS_PREFIX}${newOrientation}`;
|
||||
if (!rootView.cssClasses.has(newOrientationCssClass)) {
|
||||
ORIENTATION_CSS_CLASSES.forEach(cssClass => removeCssClass(rootView, cssClass));
|
||||
applyCssClass(rootView, newOrientationCssClass);
|
||||
rootView._onCssStateChange();
|
||||
}
|
||||
}
|
||||
|
||||
export function systemAppearanceChanged(rootView: View, newSystemAppearance: "dark" | "light"): void {
|
||||
if (!rootView) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newSystemAppearanceCssClass = `${CLASS_PREFIX}${newSystemAppearance}`;
|
||||
if (!rootView.cssClasses.has(newSystemAppearanceCssClass)) {
|
||||
SYSTEM_APPEARANCE_CSS_CLASSES.forEach(cssClass => removeCssClass(rootView, cssClass));
|
||||
applyCssClass(rootView, newSystemAppearanceCssClass);
|
||||
rootView._onCssStateChange();
|
||||
}
|
||||
}
|
||||
|
||||
global.__onUncaughtError = function (error: NativeScriptError) {
|
||||
events.notify(<UnhandledErrorEventData>{ eventName: uncaughtErrorEvent, object: app, android: error, ios: error, error: error });
|
||||
};
|
||||
|
||||
global.__onDiscardedError = function (error: NativeScriptError) {
|
||||
events.notify(<DiscardedErrorEventData>{ eventName: discardedErrorEvent, object: app, error: error });
|
||||
};
|
484
nativescript-core/application/application.android.ts
Normal file
484
nativescript-core/application/application.android.ts
Normal file
@ -0,0 +1,484 @@
|
||||
// Definitions.
|
||||
import {
|
||||
AndroidActivityBackPressedEventData,
|
||||
AndroidActivityBundleEventData,
|
||||
AndroidActivityEventData,
|
||||
AndroidActivityNewIntentEventData,
|
||||
AndroidActivityRequestPermissionsEventData,
|
||||
AndroidActivityResultEventData,
|
||||
AndroidApplication as AndroidApplicationDefinition,
|
||||
ApplicationEventData,
|
||||
CssChangedEventData,
|
||||
OrientationChangedEventData,
|
||||
SystemAppearanceChangedEventData
|
||||
} from ".";
|
||||
|
||||
import {
|
||||
displayedEvent, hasListeners, livesync, lowMemoryEvent, notify, Observable, on,
|
||||
orientationChanged, orientationChangedEvent, setApplication, suspendEvent,
|
||||
systemAppearanceChanged, systemAppearanceChangedEvent
|
||||
} from "./application-common";
|
||||
|
||||
import { profile } from "../profiling";
|
||||
|
||||
// First reexport so that app module is initialized.
|
||||
export * from "./application-common";
|
||||
|
||||
// Types.
|
||||
import { NavigationEntry, View, AndroidActivityCallbacks } from "../ui/frame";
|
||||
|
||||
const ActivityCreated = "activityCreated";
|
||||
const ActivityDestroyed = "activityDestroyed";
|
||||
const ActivityStarted = "activityStarted";
|
||||
const ActivityPaused = "activityPaused";
|
||||
const ActivityResumed = "activityResumed";
|
||||
const ActivityStopped = "activityStopped";
|
||||
const SaveActivityState = "saveActivityState";
|
||||
const ActivityResult = "activityResult";
|
||||
const ActivityBackPressed = "activityBackPressed";
|
||||
const ActivityNewIntent = "activityNewIntent";
|
||||
const ActivityRequestPermissions = "activityRequestPermissions";
|
||||
|
||||
export class AndroidApplication extends Observable implements AndroidApplicationDefinition {
|
||||
public static activityCreatedEvent = ActivityCreated;
|
||||
public static activityDestroyedEvent = ActivityDestroyed;
|
||||
public static activityStartedEvent = ActivityStarted;
|
||||
public static activityPausedEvent = ActivityPaused;
|
||||
public static activityResumedEvent = ActivityResumed;
|
||||
public static activityStoppedEvent = ActivityStopped;
|
||||
public static saveActivityStateEvent = SaveActivityState;
|
||||
public static activityResultEvent = ActivityResult;
|
||||
public static activityBackPressedEvent = ActivityBackPressed;
|
||||
public static activityNewIntentEvent = ActivityNewIntent;
|
||||
public static activityRequestPermissionsEvent = ActivityRequestPermissions;
|
||||
|
||||
private _orientation: "portrait" | "landscape" | "unknown";
|
||||
private _systemAppearance: "light" | "dark";
|
||||
public paused: boolean;
|
||||
public nativeApp: android.app.Application;
|
||||
public context: android.content.Context;
|
||||
public foregroundActivity: androidx.appcompat.app.AppCompatActivity;
|
||||
public startActivity: androidx.appcompat.app.AppCompatActivity;
|
||||
public packageName: string;
|
||||
// we are using these property to store the callbacks to avoid early GC collection which would trigger MarkReachableObjects
|
||||
private callbacks: any = {};
|
||||
|
||||
public init(nativeApp: android.app.Application) {
|
||||
if (this.nativeApp === nativeApp) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.nativeApp) {
|
||||
throw new Error("application.android already initialized.");
|
||||
}
|
||||
|
||||
this.nativeApp = nativeApp;
|
||||
this.packageName = nativeApp.getPackageName();
|
||||
this.context = nativeApp.getApplicationContext();
|
||||
|
||||
// we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC
|
||||
this.callbacks.lifecycleCallbacks = initLifecycleCallbacks();
|
||||
this.callbacks.componentCallbacks = initComponentCallbacks();
|
||||
this.nativeApp.registerActivityLifecycleCallbacks(this.callbacks.lifecycleCallbacks);
|
||||
this.nativeApp.registerComponentCallbacks(this.callbacks.componentCallbacks);
|
||||
|
||||
this._registerPendingReceivers();
|
||||
}
|
||||
|
||||
private _registeredReceivers = {};
|
||||
private _pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>();
|
||||
private _registerPendingReceivers() {
|
||||
this._pendingReceiverRegistrations.forEach(func => func(this.context));
|
||||
this._pendingReceiverRegistrations.length = 0;
|
||||
}
|
||||
|
||||
get orientation(): "portrait" | "landscape" | "unknown" {
|
||||
if (!this._orientation) {
|
||||
const resources = this.context.getResources();
|
||||
const configuration = <android.content.res.Configuration>resources.getConfiguration();
|
||||
|
||||
this._orientation = getOrientationValue(configuration);
|
||||
}
|
||||
|
||||
return this._orientation;
|
||||
}
|
||||
|
||||
set orientation(value: "portrait" | "landscape" | "unknown") {
|
||||
this._orientation = value;
|
||||
}
|
||||
|
||||
get systemAppearance(): "light" | "dark" {
|
||||
if (!this._systemAppearance) {
|
||||
const resources = this.context.getResources();
|
||||
const configuration = <android.content.res.Configuration>resources.getConfiguration();
|
||||
|
||||
this._systemAppearance = getSystemAppearanceValue(configuration);
|
||||
}
|
||||
|
||||
return this._systemAppearance;
|
||||
}
|
||||
|
||||
set systemAppearance(value: "light" | "dark") {
|
||||
this._systemAppearance = value;
|
||||
}
|
||||
|
||||
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||
ensureBroadCastReceiverClass();
|
||||
const that = this;
|
||||
const registerFunc = function (context: android.content.Context) {
|
||||
const receiver: android.content.BroadcastReceiver = new BroadcastReceiverClass(onReceiveCallback);
|
||||
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
|
||||
that._registeredReceivers[intentFilter] = receiver;
|
||||
};
|
||||
|
||||
if (this.context) {
|
||||
registerFunc(this.context);
|
||||
}
|
||||
else {
|
||||
this._pendingReceiverRegistrations.push(registerFunc);
|
||||
}
|
||||
}
|
||||
|
||||
public unregisterBroadcastReceiver(intentFilter: string) {
|
||||
const receiver = this._registeredReceivers[intentFilter];
|
||||
if (receiver) {
|
||||
this.context.unregisterReceiver(receiver);
|
||||
this._registeredReceivers[intentFilter] = undefined;
|
||||
delete this._registeredReceivers[intentFilter];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface AndroidApplication {
|
||||
on(eventNames: string, callback: (data: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "activityCreated", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
|
||||
on(event: "activityDestroyed", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "activityStarted", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "activityPaused", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "activityResumed", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "activityStopped", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
|
||||
on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any);
|
||||
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
|
||||
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
|
||||
on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any);
|
||||
}
|
||||
|
||||
const androidApp = new AndroidApplication();
|
||||
export { androidApp as android };
|
||||
|
||||
setApplication(androidApp);
|
||||
|
||||
let mainEntry: NavigationEntry;
|
||||
let started = false;
|
||||
const createRootFrame = { value: true };
|
||||
|
||||
export function _start(entry?: NavigationEntry | string) {
|
||||
if (started) {
|
||||
throw new Error("Application is already started.");
|
||||
}
|
||||
|
||||
started = true;
|
||||
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
|
||||
if (!androidApp.nativeApp) {
|
||||
const nativeApp = getNativeApplication();
|
||||
androidApp.init(nativeApp);
|
||||
}
|
||||
}
|
||||
|
||||
export function _shouldCreateRootFrame(): boolean {
|
||||
return createRootFrame.value;
|
||||
}
|
||||
|
||||
export function run(entry?: NavigationEntry | string) {
|
||||
createRootFrame.value = false;
|
||||
_start(entry);
|
||||
}
|
||||
|
||||
export function addCss(cssText: string, attributeScoped?: boolean): void {
|
||||
notify(<CssChangedEventData>{ eventName: "cssChanged", object: androidApp, cssText: cssText });
|
||||
if (!attributeScoped) {
|
||||
const rootView = getRootView();
|
||||
if (rootView) {
|
||||
rootView._onCssStateChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const CALLBACKS = "_callbacks";
|
||||
|
||||
export function _resetRootView(entry?: NavigationEntry | string) {
|
||||
const activity = androidApp.foregroundActivity;
|
||||
if (!activity) {
|
||||
throw new Error("Cannot find android activity.");
|
||||
}
|
||||
|
||||
createRootFrame.value = false;
|
||||
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
|
||||
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
|
||||
if (!callbacks) {
|
||||
throw new Error("Cannot find android activity callbacks.");
|
||||
}
|
||||
callbacks.resetActivityContent(activity);
|
||||
}
|
||||
|
||||
export function getMainEntry() {
|
||||
return mainEntry;
|
||||
}
|
||||
|
||||
export function getRootView(): View {
|
||||
// Use start activity as a backup when foregroundActivity is still not set
|
||||
// in cases when we are getting the root view before activity.onResumed event is fired
|
||||
const activity = androidApp.foregroundActivity || androidApp.startActivity;
|
||||
if (!activity) {
|
||||
return undefined;
|
||||
}
|
||||
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
|
||||
|
||||
return callbacks ? callbacks.getRootView() : undefined;
|
||||
}
|
||||
|
||||
export function getNativeApplication(): android.app.Application {
|
||||
// Try getting it from module - check whether application.android.init has been explicitly called
|
||||
let nativeApp = androidApp.nativeApp;
|
||||
if (!nativeApp) {
|
||||
// check whether the com.tns.NativeScriptApplication type exists
|
||||
if (!nativeApp && com.tns.NativeScriptApplication) {
|
||||
nativeApp = com.tns.NativeScriptApplication.getInstance();
|
||||
}
|
||||
|
||||
// the getInstance might return null if com.tns.NativeScriptApplication exists but is not the starting app type
|
||||
if (!nativeApp) {
|
||||
// TODO: Should we handle the case when a custom application type is provided and the user has not explicitly initialized the application module?
|
||||
const clazz = java.lang.Class.forName("android.app.ActivityThread");
|
||||
if (clazz) {
|
||||
const method = clazz.getMethod("currentApplication", null);
|
||||
if (method) {
|
||||
nativeApp = method.invoke(null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// we cannot work without having the app instance
|
||||
if (!nativeApp) {
|
||||
throw new Error("Failed to retrieve native Android Application object. If you have a custom android.app.Application type implemented make sure that you've called the '<application-module>.android.init' method.");
|
||||
}
|
||||
}
|
||||
|
||||
return nativeApp;
|
||||
}
|
||||
|
||||
export function orientation(): "portrait" | "landscape" | "unknown" {
|
||||
return androidApp.orientation;
|
||||
}
|
||||
|
||||
export function systemAppearance(): "dark" | "light" {
|
||||
return androidApp.systemAppearance;
|
||||
}
|
||||
|
||||
global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
|
||||
if (androidApp && androidApp.paused) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rootView = getRootView();
|
||||
livesync(rootView, context);
|
||||
};
|
||||
|
||||
function getOrientationValue(configuration: android.content.res.Configuration): "portrait" | "landscape" | "unknown" {
|
||||
const orientation = configuration.orientation;
|
||||
|
||||
switch (orientation) {
|
||||
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
|
||||
return "landscape";
|
||||
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
|
||||
return "portrait";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
// https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#configuration_changes
|
||||
function getSystemAppearanceValue(configuration: android.content.res.Configuration): "dark" | "light" {
|
||||
const systemAppearance = configuration.uiMode & android.content.res.Configuration.UI_MODE_NIGHT_MASK;
|
||||
|
||||
switch (systemAppearance) {
|
||||
case android.content.res.Configuration.UI_MODE_NIGHT_YES:
|
||||
return "dark";
|
||||
case android.content.res.Configuration.UI_MODE_NIGHT_NO:
|
||||
case android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED:
|
||||
return "light";
|
||||
}
|
||||
}
|
||||
|
||||
function initLifecycleCallbacks() {
|
||||
const setThemeOnLaunch = profile("setThemeOnLaunch", (activity: androidx.appcompat.app.AppCompatActivity) => {
|
||||
// Set app theme after launch screen was used during startup
|
||||
const activityInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), android.content.pm.PackageManager.GET_META_DATA);
|
||||
if (activityInfo.metaData) {
|
||||
const setThemeOnLaunch = activityInfo.metaData.getInt("SET_THEME_ON_LAUNCH", -1);
|
||||
if (setThemeOnLaunch !== -1) {
|
||||
activity.setTheme(setThemeOnLaunch);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const notifyActivityCreated = profile("notifyActivityCreated", function (activity: androidx.appcompat.app.AppCompatActivity, savedInstanceState: android.os.Bundle) {
|
||||
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: ActivityCreated, object: androidApp, activity, bundle: savedInstanceState });
|
||||
});
|
||||
|
||||
const subscribeForGlobalLayout = profile("subscribeForGlobalLayout", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
const rootView = activity.getWindow().getDecorView().getRootView();
|
||||
// store the listener not to trigger GC collection before collecting the method
|
||||
global.onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
|
||||
onGlobalLayout() {
|
||||
notify({ eventName: displayedEvent, object: androidApp, activity });
|
||||
let viewTreeObserver = rootView.getViewTreeObserver();
|
||||
viewTreeObserver.removeOnGlobalLayoutListener(global.onGlobalLayoutListener);
|
||||
}
|
||||
});
|
||||
rootView.getViewTreeObserver().addOnGlobalLayoutListener(global.onGlobalLayoutListener);
|
||||
});
|
||||
|
||||
const lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({
|
||||
onActivityCreated: profile("onActivityCreated", function (activity: androidx.appcompat.app.AppCompatActivity, savedInstanceState: android.os.Bundle) {
|
||||
setThemeOnLaunch(activity);
|
||||
|
||||
if (!androidApp.startActivity) {
|
||||
androidApp.startActivity = activity;
|
||||
}
|
||||
|
||||
notifyActivityCreated(activity, savedInstanceState);
|
||||
|
||||
if (hasListeners(displayedEvent)) {
|
||||
subscribeForGlobalLayout(activity);
|
||||
}
|
||||
}),
|
||||
|
||||
onActivityDestroyed: profile("onActivityDestroyed", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
if (activity === androidApp.foregroundActivity) {
|
||||
androidApp.foregroundActivity = undefined;
|
||||
}
|
||||
|
||||
if (activity === androidApp.startActivity) {
|
||||
androidApp.startActivity = undefined;
|
||||
}
|
||||
|
||||
androidApp.notify(<AndroidActivityEventData>{ eventName: ActivityDestroyed, object: androidApp, activity: activity });
|
||||
// TODO: This is a temporary workaround to force the V8's Garbage Collector, which will force the related Java Object to be collected.
|
||||
gc();
|
||||
}),
|
||||
|
||||
onActivityPaused: profile("onActivityPaused", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
if ((<any>activity).isNativeScriptActivity) {
|
||||
androidApp.paused = true;
|
||||
notify(<ApplicationEventData>{ eventName: suspendEvent, object: androidApp, android: activity });
|
||||
}
|
||||
|
||||
androidApp.notify(<AndroidActivityEventData>{ eventName: ActivityPaused, object: androidApp, activity: activity });
|
||||
}),
|
||||
|
||||
onActivityResumed: profile("onActivityResumed", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
androidApp.foregroundActivity = activity;
|
||||
|
||||
androidApp.notify(<AndroidActivityEventData>{ eventName: ActivityResumed, object: androidApp, activity: activity });
|
||||
}),
|
||||
|
||||
onActivitySaveInstanceState: profile("onActivitySaveInstanceState", function (activity: androidx.appcompat.app.AppCompatActivity, outState: android.os.Bundle) {
|
||||
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: SaveActivityState, object: androidApp, activity: activity, bundle: outState });
|
||||
}),
|
||||
|
||||
onActivityStarted: profile("onActivityStarted", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
androidApp.notify(<AndroidActivityEventData>{ eventName: ActivityStarted, object: androidApp, activity: activity });
|
||||
}),
|
||||
|
||||
onActivityStopped: profile("onActivityStopped", function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
androidApp.notify(<AndroidActivityEventData>{ eventName: ActivityStopped, object: androidApp, activity: activity });
|
||||
})
|
||||
});
|
||||
|
||||
return lifecycleCallbacks;
|
||||
}
|
||||
|
||||
function initComponentCallbacks() {
|
||||
let componentCallbacks = new android.content.ComponentCallbacks2({
|
||||
onLowMemory: profile("onLowMemory", function () {
|
||||
gc();
|
||||
java.lang.System.gc();
|
||||
notify(<ApplicationEventData>{ eventName: lowMemoryEvent, object: this, android: this });
|
||||
}),
|
||||
|
||||
onTrimMemory: profile("onTrimMemory", function (level: number) {
|
||||
// TODO: This is skipped for now, test carefully for OutOfMemory exceptions
|
||||
}),
|
||||
|
||||
onConfigurationChanged: profile("onConfigurationChanged", function (newConfiguration: android.content.res.Configuration) {
|
||||
const rootView = getRootView();
|
||||
const newOrientation = getOrientationValue(newConfiguration);
|
||||
|
||||
if (androidApp.orientation !== newOrientation) {
|
||||
androidApp.orientation = newOrientation;
|
||||
orientationChanged(rootView, newOrientation);
|
||||
|
||||
notify(<OrientationChangedEventData>{
|
||||
eventName: orientationChangedEvent,
|
||||
android: androidApp.nativeApp,
|
||||
newValue: androidApp.orientation,
|
||||
object: androidApp
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const newSystemAppearance = getSystemAppearanceValue(newConfiguration);
|
||||
|
||||
if (androidApp.systemAppearance !== newSystemAppearance) {
|
||||
androidApp.systemAppearance = newSystemAppearance;
|
||||
systemAppearanceChanged(rootView, newSystemAppearance);
|
||||
|
||||
notify(<SystemAppearanceChangedEventData>{
|
||||
eventName: systemAppearanceChangedEvent,
|
||||
android: androidApp.nativeApp,
|
||||
newValue: androidApp.systemAppearance,
|
||||
object: androidApp
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return componentCallbacks;
|
||||
}
|
||||
|
||||
let BroadcastReceiverClass;
|
||||
function ensureBroadCastReceiverClass() {
|
||||
if (BroadcastReceiverClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
class BroadcastReceiver extends android.content.BroadcastReceiver {
|
||||
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
|
||||
|
||||
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||
super();
|
||||
this._onReceiveCallback = onReceiveCallback;
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public onReceive(context: android.content.Context, intent: android.content.Intent) {
|
||||
if (this._onReceiveCallback) {
|
||||
this._onReceiveCallback(context, intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BroadcastReceiverClass = BroadcastReceiver;
|
||||
}
|
||||
|
||||
declare namespace com {
|
||||
namespace tns {
|
||||
class NativeScriptApplication extends android.app.Application {
|
||||
static getInstance(): NativeScriptApplication;
|
||||
}
|
||||
}
|
||||
}
|
678
nativescript-core/application/application.d.ts
vendored
Normal file
678
nativescript-core/application/application.d.ts
vendored
Normal file
@ -0,0 +1,678 @@
|
||||
/**
|
||||
* Contains the application abstraction with all related methods.
|
||||
* @module "application"
|
||||
*/ /** */
|
||||
|
||||
/// <reference path="../tns-core-modules.d.ts" />
|
||||
|
||||
import { NavigationEntry, View, Observable, EventData } from "../ui/frame";
|
||||
|
||||
/**
|
||||
* String value used when hooking to launch event.
|
||||
*/
|
||||
export const launchEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to displayed event.
|
||||
*/
|
||||
export const displayedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to uncaughtError event.
|
||||
*/
|
||||
export const uncaughtErrorEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to discardedError event.
|
||||
*/
|
||||
export const discardedErrorEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to suspend event.
|
||||
*/
|
||||
export const suspendEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to resume event.
|
||||
*/
|
||||
export const resumeEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to exit event.
|
||||
*/
|
||||
export const exitEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to lowMemory event.
|
||||
*/
|
||||
export const lowMemoryEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to orientationChanged event.
|
||||
*/
|
||||
export const orientationChangedEvent: string;
|
||||
|
||||
/**
|
||||
* Event data containing information for the application events.
|
||||
*/
|
||||
export interface ApplicationEventData extends EventData {
|
||||
/**
|
||||
* Gets the native iOS event arguments. Valid only when running on iOS.
|
||||
*/
|
||||
ios?: any;
|
||||
|
||||
/**
|
||||
* Gets the native Android event arguments. Valid only when running on Android.
|
||||
*/
|
||||
android?: any;
|
||||
|
||||
/**
|
||||
* The name of the event.
|
||||
*/
|
||||
eventName: string;
|
||||
|
||||
/**
|
||||
* The instance that has raised the event.
|
||||
*/
|
||||
object: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information for launch event.
|
||||
*/
|
||||
export interface LaunchEventData extends ApplicationEventData {
|
||||
/**
|
||||
* The root view for this Window on iOS or Activity for Android.
|
||||
* If not set a new Frame will be created as a root view in order to maintain backwards compatibility.
|
||||
*/
|
||||
root?: View;
|
||||
|
||||
savedInstanceState?: any /* android.os.Bundle */;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information for orientation changed event.
|
||||
*/
|
||||
export interface OrientationChangedEventData extends ApplicationEventData {
|
||||
/**
|
||||
* New orientation value.
|
||||
*/
|
||||
newValue: "portrait" | "landscape" | "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information for system appearance changed event.
|
||||
*/
|
||||
export interface SystemAppearanceChangedEventData extends ApplicationEventData {
|
||||
/**
|
||||
* New system appearance value.
|
||||
*/
|
||||
newValue: "light" | "dark";
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information about unhandled application errors.
|
||||
*/
|
||||
export interface UnhandledErrorEventData extends ApplicationEventData {
|
||||
ios?: NativeScriptError;
|
||||
android?: NativeScriptError;
|
||||
error: NativeScriptError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information about discarded application errors.
|
||||
*/
|
||||
export interface DiscardedErrorEventData extends ApplicationEventData {
|
||||
error: NativeScriptError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event data containing information about application css change.
|
||||
*/
|
||||
export interface CssChangedEventData extends EventData {
|
||||
cssFile?: string;
|
||||
cssText?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get main entry specified when calling start function.
|
||||
*/
|
||||
export function getMainEntry(): NavigationEntry;
|
||||
|
||||
/**
|
||||
* Get current application root view.
|
||||
*/
|
||||
export function getRootView(): View;
|
||||
|
||||
/**
|
||||
* Get application level static resources.
|
||||
*/
|
||||
export function getResources(): any;
|
||||
|
||||
/**
|
||||
* Set application level static resources.
|
||||
*/
|
||||
export function setResources(resources: any): void;
|
||||
|
||||
/**
|
||||
* Sets css file name for the application.
|
||||
*/
|
||||
export function setCssFileName(cssFile: string): void;
|
||||
|
||||
/**
|
||||
* Gets css file name for the application.
|
||||
*/
|
||||
export function getCssFileName(): string;
|
||||
|
||||
/**
|
||||
* Loads immediately the app.css.
|
||||
* By default the app.css file is loaded shortly after "loaded".
|
||||
* For the Android snapshot the CSS can be parsed during the snapshot generation,
|
||||
* as the CSS does not depend on runtime APIs, and loadAppCss will be called explicitly.
|
||||
*/
|
||||
export function loadAppCss();
|
||||
|
||||
/**
|
||||
* Adds new values to the application styles.
|
||||
* @param cssText - A valid styles which will be added to the current application styles.
|
||||
* @param attributeScoped - Whether the styles are attribute scoped. Adding attribute scoped styles will not perform a full application styling refresh.
|
||||
*/
|
||||
export function addCss(cssText: string, attributeScoped?: boolean): void;
|
||||
|
||||
/**
|
||||
* This event is raised when application css is changed.
|
||||
*/
|
||||
export function on(event: "cssChanged", callback: (args: CssChangedEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Event raised then livesync operation is performed.
|
||||
*/
|
||||
export function on(event: "livesync", callback: (args: EventData) => void);
|
||||
|
||||
/**
|
||||
* Removes listener for the specified event name.
|
||||
*/
|
||||
export function off(eventNames: string, callback?: any, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Call this method to run the application. Important: All code after this method call will not be executed!
|
||||
* Compared to start this method won't create Frame as root view.
|
||||
*/
|
||||
export function run(entry?: NavigationEntry | string);
|
||||
|
||||
/**
|
||||
* Call this method to change the root view of your application. Important: Your application must already be running.
|
||||
* This method won't create Frame as root view.
|
||||
*/
|
||||
export function _resetRootView(entry?: NavigationEntry | string);
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export function _shouldCreateRootFrame(): boolean;
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export function _start(entry?: NavigationEntry | string);
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "onLaunch"). Optionally could be used more events separated by `,` (e.g. "onLaunch", "onSuspend").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
export function on(eventNames: string, callback: (data: any) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Shortcut alias to the removeEventListener method.
|
||||
* @param eventNames - String corresponding to events (e.g. "onLaunch").
|
||||
* @param callback - Callback function which will be removed.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
export function off(eventNames: string, callback?: any, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Notifies all the registered listeners for the event provided in the data.eventName.
|
||||
* @param data The data associated with the event.
|
||||
*/
|
||||
export function notify(data: any): void;
|
||||
|
||||
/**
|
||||
* Checks whether a listener is registered for the specified event name.
|
||||
* @param eventName The name of the event to check for.
|
||||
*/
|
||||
export function hasListeners(eventName: string): boolean;
|
||||
|
||||
/**
|
||||
* This event is raised on application launchEvent.
|
||||
*/
|
||||
export function on(event: "launch", callback: (args: LaunchEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised after the application has performed most of its startup actions.
|
||||
* Its intent is to be suitable for measuring app startup times.
|
||||
* @experimental
|
||||
*/
|
||||
export function on(event: "displayed", callback: (args: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is suspended.
|
||||
*/
|
||||
export function on(event: "suspend", callback: (args: ApplicationEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is resumed after it has been suspended.
|
||||
*/
|
||||
export function on(event: "resume", callback: (args: ApplicationEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Application is about to exit.
|
||||
*/
|
||||
export function on(event: "exit", callback: (args: ApplicationEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when there is low memory on the target device.
|
||||
*/
|
||||
export function on(event: "lowMemory", callback: (args: ApplicationEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when an uncaught error occurs while the application is running.
|
||||
*/
|
||||
export function on(event: "uncaughtError", callback: (args: UnhandledErrorEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when an discarded error occurs while the application is running.
|
||||
*/
|
||||
export function on(event: "discardedError", callback: (args: DiscardedErrorEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the orientation of the application changes.
|
||||
*/
|
||||
export function on(event: "orientationChanged", callback: (args: OrientationChangedEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the operating system appearance changes
|
||||
* between light and dark theme (for Android);
|
||||
* between light and dark mode (for iOS) and vice versa.
|
||||
*/
|
||||
export function on(event: "systemAppearanceChanged", callback: (args: SystemAppearanceChangedEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Gets the orientation of the application.
|
||||
* Available values: "portrait", "landscape", "unknown".
|
||||
*/
|
||||
export function orientation(): "portrait" | "landscape" | "unknown";
|
||||
|
||||
/**
|
||||
* Gets the operating system appearance.
|
||||
* Available values: "dark", "light", null.
|
||||
* Null for iOS <= 11.
|
||||
*/
|
||||
export function systemAppearance(): "dark" | "light" | null;
|
||||
|
||||
/**
|
||||
* This is the Android-specific application object instance.
|
||||
* Encapsulates methods and properties specific to the Android platform.
|
||||
* Will be undefined when TargetOS is iOS.
|
||||
*/
|
||||
export let android: AndroidApplication;
|
||||
|
||||
/**
|
||||
* This is the iOS-specific application object instance.
|
||||
* Encapsulates methods and properties specific to the iOS platform.
|
||||
* Will be undefined when TargetOS is Android.
|
||||
*/
|
||||
export let ios: iOSApplication;
|
||||
|
||||
/**
|
||||
* Data for the Android activity events.
|
||||
*/
|
||||
export interface AndroidActivityEventData {
|
||||
/**
|
||||
* The activity.
|
||||
*/
|
||||
activity: any /* androidx.appcompat.app.AppCompatActivity */;
|
||||
|
||||
/**
|
||||
* The name of the event.
|
||||
*/
|
||||
eventName: string;
|
||||
|
||||
/**
|
||||
* The instance that has raised the event.
|
||||
*/
|
||||
object: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the Android activity events with bundle.
|
||||
*/
|
||||
export interface AndroidActivityBundleEventData extends AndroidActivityEventData {
|
||||
/**
|
||||
* The bundle.
|
||||
*/
|
||||
bundle: any /* android.os.Bundle */;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the Android activity onRequestPermissions callback
|
||||
*/
|
||||
export interface AndroidActivityRequestPermissionsEventData extends AndroidActivityEventData {
|
||||
/**
|
||||
* The request code.
|
||||
*/
|
||||
requestCode: number;
|
||||
|
||||
/**
|
||||
* The Permissions.
|
||||
*/
|
||||
permissions: Array<string>;
|
||||
|
||||
/**
|
||||
* The Granted.
|
||||
*/
|
||||
grantResults: Array<number>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the Android activity result event.
|
||||
*/
|
||||
export interface AndroidActivityResultEventData extends AndroidActivityEventData {
|
||||
/**
|
||||
* The request code.
|
||||
*/
|
||||
requestCode: number;
|
||||
|
||||
/**
|
||||
* The result code.
|
||||
*/
|
||||
resultCode: number;
|
||||
|
||||
/**
|
||||
* The intent.
|
||||
*/
|
||||
intent: any /* android.content.Intent */;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the Android activity newIntent event.
|
||||
*/
|
||||
export interface AndroidActivityNewIntentEventData extends AndroidActivityEventData {
|
||||
/**
|
||||
* The intent.
|
||||
*/
|
||||
intent: any /* android.content.Intent */;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the Android activity back pressed event.
|
||||
*/
|
||||
export interface AndroidActivityBackPressedEventData extends AndroidActivityEventData {
|
||||
/**
|
||||
* In the event handler, set this value to true if you want to cancel the back navigation and do something else instead.
|
||||
*/
|
||||
cancel: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The abstraction of an Android-specific application object.
|
||||
*/
|
||||
export class AndroidApplication extends Observable {
|
||||
/**
|
||||
* The [android Application](http://developer.android.com/reference/android/app/Application.html) object instance provided to the init of the module.
|
||||
*/
|
||||
nativeApp: any /* android.app.Application */;
|
||||
|
||||
/**
|
||||
* The application's [android Context](http://developer.android.com/reference/android/content/Context.html) object instance.
|
||||
*/
|
||||
context: any /* android.content.Context */;
|
||||
|
||||
/**
|
||||
* The currently active (loaded) [android Activity](http://developer.android.com/reference/android/app/Activity.html). This property is automatically updated upon Activity events.
|
||||
*/
|
||||
foregroundActivity: any /* androidx.appcompat.app.AppCompatActivity */;
|
||||
|
||||
/**
|
||||
* The main (start) Activity for the application.
|
||||
*/
|
||||
startActivity: any /* androidx.appcompat.app.AppCompatActivity */;
|
||||
|
||||
/**
|
||||
* Gets the orientation of the application.
|
||||
* Available values: "portrait", "landscape", "unknown".
|
||||
*/
|
||||
orientation: "portrait" | "landscape" | "unknown";
|
||||
|
||||
/**
|
||||
* Gets the system appearance.
|
||||
* Available values: "dark", "light".
|
||||
*/
|
||||
systemAppearance: "dark" | "light";
|
||||
|
||||
/**
|
||||
* The name of the application package.
|
||||
*/
|
||||
packageName: string;
|
||||
|
||||
/**
|
||||
* True if the main application activity is not running (suspended), false otherwise.
|
||||
*/
|
||||
paused: boolean;
|
||||
|
||||
/**
|
||||
* Initialized the android-specific application object with the native android.app.Application instance.
|
||||
* This is useful when creating custom application types.
|
||||
* @param nativeApp - the android.app.Application instance that started the app.
|
||||
*/
|
||||
init: (nativeApp) => void;
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (data: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityCreated.
|
||||
*/
|
||||
on(event: "activityCreated", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityDestroyed.
|
||||
*/
|
||||
on(event: "activityDestroyed", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityStarted.
|
||||
*/
|
||||
on(event: "activityStarted", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityPaused.
|
||||
*/
|
||||
on(event: "activityPaused", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityResumed.
|
||||
*/
|
||||
on(event: "activityResumed", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityStopped.
|
||||
*/
|
||||
on(event: "activityStopped", callback: (args: AndroidActivityEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application SaveActivityState.
|
||||
*/
|
||||
on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on android application ActivityResult.
|
||||
*/
|
||||
on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised on the back button is pressed in an android application.
|
||||
*/
|
||||
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Android app was launched by an Intent with data.
|
||||
*/
|
||||
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* This event is raised when the Android activity requests permissions.
|
||||
*/
|
||||
on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityCreated event.
|
||||
*/
|
||||
public static activityCreatedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityDestroyed event.
|
||||
*/
|
||||
public static activityDestroyedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityStarted event.
|
||||
*/
|
||||
public static activityStartedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityPaused event.
|
||||
*/
|
||||
public static activityPausedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityResumed event.
|
||||
*/
|
||||
public static activityResumedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityStopped event.
|
||||
*/
|
||||
public static activityStoppedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to saveActivityState event.
|
||||
*/
|
||||
public static saveActivityStateEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityResult event.
|
||||
*/
|
||||
public static activityResultEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityBackPressed event.
|
||||
*/
|
||||
public static activityBackPressedEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to activityNewIntent event.
|
||||
*/
|
||||
public static activityNewIntentEvent: string;
|
||||
|
||||
/**
|
||||
* String value used when hooking to requestPermissions event.
|
||||
*/
|
||||
public static activityRequestPermissionsEvent: string;
|
||||
|
||||
/**
|
||||
* Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread.
|
||||
* For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29'
|
||||
* @param intentFilter A string containing the intent filter.
|
||||
* @param onReceiveCallback A callback function that will be called each time the receiver receives a broadcast.
|
||||
*/
|
||||
registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: any /* android.content.Context */, intent: any /* android.content.Intent */) => void): void;
|
||||
|
||||
/**
|
||||
* Unregister a previously registered BroadcastReceiver.
|
||||
* For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)'
|
||||
* @param intentFilter A string containing the intent filter with which the receiver was originally registered.
|
||||
*/
|
||||
unregisterBroadcastReceiver(intentFilter: string): void;
|
||||
}
|
||||
|
||||
/* tslint:disable */
|
||||
/**
|
||||
* The abstraction of an iOS-specific application object.
|
||||
*/
|
||||
export interface iOSApplication {
|
||||
/* tslint:enable */
|
||||
/**
|
||||
* The root view controller for the application.
|
||||
*/
|
||||
rootController: any /* UIViewController */;
|
||||
|
||||
/* tslint:enable */
|
||||
/**
|
||||
* The key window.
|
||||
*/
|
||||
window: any /* UIWindow */;
|
||||
|
||||
/**
|
||||
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
|
||||
*/
|
||||
delegate: any /* typeof UIApplicationDelegate */;
|
||||
|
||||
/**
|
||||
* Gets or sets the orientation of the application.
|
||||
* Available values: "portrait", "landscape", "unknown".
|
||||
*/
|
||||
orientation: "portrait" | "landscape" | "unknown";
|
||||
|
||||
/**
|
||||
* Gets the system appearance.
|
||||
* Available values: "dark", "light", null.
|
||||
* Null for iOS <= 11.
|
||||
*/
|
||||
systemAppearance: "dark" | "light" | null;
|
||||
|
||||
/**
|
||||
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
|
||||
*/
|
||||
nativeApp: any /* UIApplication */;
|
||||
|
||||
/**
|
||||
* Adds an observer to the default notification center for the specified notification.
|
||||
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
||||
* @param notificationName A string containing the name of the notification.
|
||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||
*/
|
||||
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: any /* NSNotification */) => void): any;
|
||||
|
||||
/**
|
||||
* Removes the observer for the specified notification from the default notification center.
|
||||
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
||||
* @param observer The observer that was returned from the addNotificationObserver method.
|
||||
* @param notificationName A string containing the name of the notification.
|
||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||
*/
|
||||
removeNotificationObserver(observer: any, notificationName: string): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export interface RootViewControllerImpl {
|
||||
contentController: any;
|
||||
}
|
||||
|
||||
export function getNativeApplication(): any;
|
||||
|
||||
/**
|
||||
* Indicates if the application is allready launched. See also the `application.on("launch", handler)` event.
|
||||
*/
|
||||
export function hasLaunched(): boolean;
|
||||
|
||||
export interface LoadAppCSSEventData extends EventData {
|
||||
cssFile: string;
|
||||
}
|
516
nativescript-core/application/application.ios.ts
Normal file
516
nativescript-core/application/application.ios.ts
Normal file
@ -0,0 +1,516 @@
|
||||
import {
|
||||
ApplicationEventData,
|
||||
CssChangedEventData,
|
||||
iOSApplication as IOSApplicationDefinition,
|
||||
LaunchEventData,
|
||||
LoadAppCSSEventData,
|
||||
OrientationChangedEventData,
|
||||
SystemAppearanceChangedEventData
|
||||
} from ".";
|
||||
|
||||
import {
|
||||
displayedEvent, exitEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on,
|
||||
orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent,
|
||||
systemAppearanceChanged, systemAppearanceChangedEvent
|
||||
} from "./application-common";
|
||||
|
||||
// First reexport so that app module is initialized.
|
||||
export * from "./application-common";
|
||||
|
||||
// TODO: Remove this and get it from global to decouple builder for angular
|
||||
import { Builder } from "../ui/builder";
|
||||
import {
|
||||
CLASS_PREFIX,
|
||||
getRootViewCssClasses,
|
||||
pushToRootViewCssClasses,
|
||||
resetRootViewCssClasses
|
||||
} from "../css/system-classes";
|
||||
|
||||
import { ios as iosView, View } from "../ui/core/view";
|
||||
import { Frame, NavigationEntry } from "../ui/frame";
|
||||
import { device } from "../platform/platform";
|
||||
import { profile } from "../profiling";
|
||||
import { ios } from "../utils/utils";
|
||||
|
||||
const IOS_PLATFORM = "ios";
|
||||
|
||||
const getVisibleViewController = ios.getVisibleViewController;
|
||||
const majorVersion = ios.MajorVersion;
|
||||
|
||||
// NOTE: UIResponder with implementation of window - related to https://github.com/NativeScript/ios-runtime/issues/430
|
||||
// TODO: Refactor the UIResponder to use Typescript extends when this issue is resolved:
|
||||
// https://github.com/NativeScript/ios-runtime/issues/1012
|
||||
const Responder = (<any>UIResponder).extend({
|
||||
get window() {
|
||||
return iosApp ? iosApp.window : undefined;
|
||||
},
|
||||
set window(setWindow) {
|
||||
// NOOP
|
||||
}
|
||||
}, {
|
||||
protocols: [UIApplicationDelegate]
|
||||
}
|
||||
);
|
||||
|
||||
class NotificationObserver extends NSObject {
|
||||
private _onReceiveCallback: (notification: NSNotification) => void;
|
||||
|
||||
public static initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
||||
const observer = <NotificationObserver>super.new();
|
||||
observer._onReceiveCallback = onReceiveCallback;
|
||||
|
||||
return observer;
|
||||
}
|
||||
|
||||
public onReceive(notification: NSNotification): void {
|
||||
this._onReceiveCallback(notification);
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"onReceive": { returns: interop.types.void, params: [NSNotification] }
|
||||
};
|
||||
}
|
||||
|
||||
let displayedOnce = false;
|
||||
let displayedLinkTarget;
|
||||
let displayedLink;
|
||||
class CADisplayLinkTarget extends NSObject {
|
||||
onDisplayed(link: CADisplayLink) {
|
||||
link.invalidate();
|
||||
const ios = UIApplication.sharedApplication;
|
||||
const object = iosApp;
|
||||
displayedOnce = true;
|
||||
notify(<ApplicationEventData>{ eventName: displayedEvent, object, ios });
|
||||
displayedLinkTarget = null;
|
||||
displayedLink = null;
|
||||
}
|
||||
public static ObjCExposedMethods = {
|
||||
"onDisplayed": { returns: interop.types.void, params: [CADisplayLink] }
|
||||
};
|
||||
}
|
||||
|
||||
class IOSApplication implements IOSApplicationDefinition {
|
||||
private _backgroundColor = majorVersion <= 12 ? UIColor.whiteColor : UIColor.systemBackgroundColor;
|
||||
private _delegate: typeof UIApplicationDelegate;
|
||||
private _window: UIWindow;
|
||||
private _observers: Array<NotificationObserver>;
|
||||
private _orientation: "portrait" | "landscape" | "unknown";
|
||||
private _rootView: View;
|
||||
private _systemAppearance: "light" | "dark";
|
||||
|
||||
constructor() {
|
||||
this._observers = new Array<NotificationObserver>();
|
||||
this.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, this.didFinishLaunchingWithOptions.bind(this));
|
||||
this.addNotificationObserver(UIApplicationDidBecomeActiveNotification, this.didBecomeActive.bind(this));
|
||||
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
|
||||
this.addNotificationObserver(UIApplicationWillTerminateNotification, this.willTerminate.bind(this));
|
||||
this.addNotificationObserver(UIApplicationDidReceiveMemoryWarningNotification, this.didReceiveMemoryWarning.bind(this));
|
||||
this.addNotificationObserver(UIApplicationDidChangeStatusBarOrientationNotification, this.didChangeStatusBarOrientation.bind(this));
|
||||
}
|
||||
|
||||
get orientation(): "portrait" | "landscape" | "unknown" {
|
||||
if (!this._orientation) {
|
||||
const statusBarOrientation = UIApplication.sharedApplication.statusBarOrientation;
|
||||
this._orientation = this.getOrientationValue(statusBarOrientation);
|
||||
}
|
||||
|
||||
return this._orientation;
|
||||
}
|
||||
|
||||
get rootController(): UIViewController {
|
||||
return this._window.rootViewController;
|
||||
}
|
||||
|
||||
get systemAppearance(): "light" | "dark" | null {
|
||||
|
||||
// userInterfaceStyle is available on UITraitCollection since iOS 12.
|
||||
if (majorVersion <= 11) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this._systemAppearance) {
|
||||
const userInterfaceStyle = this.rootController.traitCollection.userInterfaceStyle;
|
||||
this._systemAppearance = getSystemAppearanceValue(userInterfaceStyle);
|
||||
}
|
||||
|
||||
return this._systemAppearance;
|
||||
}
|
||||
|
||||
get nativeApp(): UIApplication {
|
||||
return UIApplication.sharedApplication;
|
||||
}
|
||||
|
||||
get window(): UIWindow {
|
||||
return this._window;
|
||||
}
|
||||
|
||||
get delegate(): typeof UIApplicationDelegate {
|
||||
return this._delegate;
|
||||
}
|
||||
|
||||
set delegate(value: typeof UIApplicationDelegate) {
|
||||
if (this._delegate !== value) {
|
||||
this._delegate = value;
|
||||
}
|
||||
}
|
||||
|
||||
get rootView(): View {
|
||||
return this._rootView;
|
||||
}
|
||||
|
||||
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
||||
const observer = NotificationObserver.initWithCallback(onReceiveCallback);
|
||||
NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
||||
this._observers.push(observer);
|
||||
|
||||
return observer;
|
||||
}
|
||||
|
||||
public removeNotificationObserver(observer: any, notificationName: string) {
|
||||
const index = this._observers.indexOf(observer);
|
||||
if (index >= 0) {
|
||||
this._observers.splice(index, 1);
|
||||
NSNotificationCenter.defaultCenter.removeObserverNameObject(observer, notificationName, null);
|
||||
}
|
||||
}
|
||||
|
||||
@profile
|
||||
private didFinishLaunchingWithOptions(notification: NSNotification) {
|
||||
if (!displayedOnce) {
|
||||
displayedLinkTarget = CADisplayLinkTarget.new();
|
||||
displayedLink = CADisplayLink.displayLinkWithTargetSelector(displayedLinkTarget, "onDisplayed");
|
||||
displayedLink.addToRunLoopForMode(NSRunLoop.mainRunLoop, NSDefaultRunLoopMode);
|
||||
displayedLink.addToRunLoopForMode(NSRunLoop.mainRunLoop, UITrackingRunLoopMode);
|
||||
}
|
||||
|
||||
this._window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen.bounds);
|
||||
// TODO: Expose Window module so that it can we styled from XML & CSS
|
||||
this._window.backgroundColor = this._backgroundColor;
|
||||
|
||||
this.notifyAppStarted(notification);
|
||||
}
|
||||
|
||||
public notifyAppStarted(notification?: NSNotification) {
|
||||
const args: LaunchEventData = {
|
||||
eventName: launchEvent,
|
||||
object: this,
|
||||
ios: notification && notification.userInfo && notification.userInfo.objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") || null
|
||||
};
|
||||
|
||||
notify(args);
|
||||
notify(<LoadAppCSSEventData>{ eventName: "loadAppCss", object: <any>this, cssFile: getCssFileName() });
|
||||
|
||||
// this._window will be undefined when NS app is embedded in a native one
|
||||
if (this._window) {
|
||||
this.setWindowContent(args.root);
|
||||
} else {
|
||||
this._window = UIApplication.sharedApplication.delegate.window;
|
||||
}
|
||||
}
|
||||
|
||||
@profile
|
||||
private didBecomeActive(notification: NSNotification) {
|
||||
const ios = UIApplication.sharedApplication;
|
||||
const object = this;
|
||||
notify(<ApplicationEventData>{ eventName: resumeEvent, object, ios });
|
||||
const rootView = this._rootView;
|
||||
if (rootView && !rootView.isLoaded) {
|
||||
rootView.callLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
private didEnterBackground(notification: NSNotification) {
|
||||
notify(<ApplicationEventData>{ eventName: suspendEvent, object: this, ios: UIApplication.sharedApplication });
|
||||
const rootView = this._rootView;
|
||||
if (rootView && rootView.isLoaded) {
|
||||
rootView.callUnloaded();
|
||||
}
|
||||
}
|
||||
|
||||
private willTerminate(notification: NSNotification) {
|
||||
notify(<ApplicationEventData>{ eventName: exitEvent, object: this, ios: UIApplication.sharedApplication });
|
||||
const rootView = this._rootView;
|
||||
if (rootView && rootView.isLoaded) {
|
||||
rootView.callUnloaded();
|
||||
}
|
||||
}
|
||||
|
||||
private didChangeStatusBarOrientation(notification: NSNotification) {
|
||||
const statusBarOrientation = UIApplication.sharedApplication.statusBarOrientation;
|
||||
const newOrientation = this.getOrientationValue(statusBarOrientation);
|
||||
|
||||
if (this._orientation !== newOrientation) {
|
||||
this._orientation = newOrientation;
|
||||
orientationChanged(getRootView(), newOrientation);
|
||||
|
||||
notify(<OrientationChangedEventData>{
|
||||
eventName: orientationChangedEvent,
|
||||
ios: this,
|
||||
newValue: this._orientation,
|
||||
object: this
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private didReceiveMemoryWarning(notification: NSNotification) {
|
||||
notify(<ApplicationEventData>{ eventName: lowMemoryEvent, object: this, ios: UIApplication.sharedApplication });
|
||||
}
|
||||
|
||||
private getOrientationValue(orientation: number): "portrait" | "landscape" | "unknown" {
|
||||
switch (orientation) {
|
||||
case UIInterfaceOrientation.LandscapeRight:
|
||||
case UIInterfaceOrientation.LandscapeLeft:
|
||||
return "landscape";
|
||||
case UIInterfaceOrientation.PortraitUpsideDown:
|
||||
case UIInterfaceOrientation.Portrait:
|
||||
return "portrait";
|
||||
case UIInterfaceOrientation.Unknown:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public _onLivesync(context?: ModuleContext): void {
|
||||
// Handle application root module
|
||||
const isAppRootModuleChanged = context && context.path && context.path.includes(getMainEntry().moduleName) && context.type !== "style";
|
||||
|
||||
// Set window content when:
|
||||
// + Application root module is changed
|
||||
// + View did not handle the change
|
||||
// Note:
|
||||
// The case when neither app root module is changed, nor livesync is handled on View,
|
||||
// then changes will not apply until navigate forward to the module.
|
||||
if (isAppRootModuleChanged || (this._rootView && !this._rootView._onLivesync(context))) {
|
||||
this.setWindowContent();
|
||||
}
|
||||
}
|
||||
|
||||
public setWindowContent(view?: View): void {
|
||||
if (this._rootView) {
|
||||
// if we already have a root view, we reset it.
|
||||
this._rootView._onRootViewReset();
|
||||
}
|
||||
const rootView = createRootView(view);
|
||||
const controller = getViewController(rootView);
|
||||
|
||||
this._rootView = rootView;
|
||||
|
||||
if (createRootFrame.value) {
|
||||
// Don't setup as styleScopeHost
|
||||
rootView._setupUI({});
|
||||
} else {
|
||||
// setup view as styleScopeHost
|
||||
rootView._setupAsRootView({});
|
||||
}
|
||||
setViewControllerView(rootView);
|
||||
const haveController = this._window.rootViewController !== null;
|
||||
this._window.rootViewController = controller;
|
||||
if (!haveController) {
|
||||
this._window.makeKeyAndVisible();
|
||||
}
|
||||
|
||||
setupRootViewCssClasses(rootView);
|
||||
rootView.on(iosView.traitCollectionColorAppearanceChangedEvent, () => {
|
||||
const userInterfaceStyle = controller.traitCollection.userInterfaceStyle;
|
||||
const newSystemAppearance = getSystemAppearanceValue(userInterfaceStyle);
|
||||
|
||||
if (this._systemAppearance !== newSystemAppearance) {
|
||||
this._systemAppearance = newSystemAppearance;
|
||||
systemAppearanceChanged(rootView, newSystemAppearance);
|
||||
|
||||
notify(<SystemAppearanceChangedEventData>{
|
||||
eventName: systemAppearanceChangedEvent,
|
||||
ios: this,
|
||||
newValue: this._systemAppearance,
|
||||
object: this
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const iosApp = new IOSApplication();
|
||||
|
||||
export { iosApp as ios };
|
||||
setApplication(iosApp);
|
||||
|
||||
// attach on global, so it can be overwritten in NativeScript Angular
|
||||
(<any>global).__onLiveSyncCore = function (context?: ModuleContext) {
|
||||
iosApp._onLivesync(context);
|
||||
};
|
||||
|
||||
let mainEntry: NavigationEntry;
|
||||
|
||||
function createRootView(v?: View) {
|
||||
let rootView = v;
|
||||
if (!rootView) {
|
||||
// try to navigate to the mainEntry (if specified)
|
||||
if (!mainEntry) {
|
||||
throw new Error("Main entry is missing. App cannot be started. Verify app bootstrap.");
|
||||
} else {
|
||||
if (createRootFrame.value) {
|
||||
const frame = rootView = new Frame();
|
||||
frame.navigate(mainEntry);
|
||||
} else {
|
||||
rootView = Builder.createViewFromEntry(mainEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rootView;
|
||||
}
|
||||
|
||||
export function getMainEntry() {
|
||||
return mainEntry;
|
||||
}
|
||||
|
||||
export function getRootView() {
|
||||
return iosApp.rootView;
|
||||
}
|
||||
|
||||
// NOTE: for backwards compatibility. Remove for 4.0.0.
|
||||
const createRootFrame = { value: true };
|
||||
let started: boolean = false;
|
||||
export function _start(entry?: string | NavigationEntry) {
|
||||
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
|
||||
started = true;
|
||||
|
||||
if (!iosApp.nativeApp) {
|
||||
// Normal NativeScript app will need UIApplicationMain.
|
||||
UIApplicationMain(0, null, null, iosApp && iosApp.delegate ? NSStringFromClass(<any>iosApp.delegate) : NSStringFromClass(Responder));
|
||||
} else {
|
||||
// TODO: this rootView should be held alive until rootController dismissViewController is called.
|
||||
const rootView = createRootView();
|
||||
if (rootView) {
|
||||
// Attach to the existing iOS app
|
||||
const window = iosApp.nativeApp.keyWindow || (iosApp.nativeApp.windows.count > 0 && iosApp.nativeApp.windows[0]);
|
||||
if (window) {
|
||||
const rootController = window.rootViewController;
|
||||
if (rootController) {
|
||||
const controller = getViewController(rootView);
|
||||
rootView._setupAsRootView({});
|
||||
let embedderDelegate = NativeScriptEmbedder.sharedInstance().delegate;
|
||||
if (embedderDelegate) {
|
||||
embedderDelegate.presentNativeScriptApp(controller);
|
||||
} else {
|
||||
let visibleVC = getVisibleViewController(rootController);
|
||||
visibleVC.presentViewControllerAnimatedCompletion(controller, true, null);
|
||||
}
|
||||
|
||||
// Mind root view CSS classes in future work
|
||||
// on embedding NativeScript applications
|
||||
setupRootViewCssClasses(rootView);
|
||||
rootView.on(iosView.traitCollectionColorAppearanceChangedEvent, () => {
|
||||
const userInterfaceStyle = controller.traitCollection.userInterfaceStyle;
|
||||
const newSystemAppearance = getSystemAppearanceValue(userInterfaceStyle);
|
||||
|
||||
if (this._systemAppearance !== newSystemAppearance) {
|
||||
this._systemAppearance = newSystemAppearance;
|
||||
|
||||
notify(<SystemAppearanceChangedEventData>{
|
||||
eventName: systemAppearanceChangedEvent,
|
||||
ios: this,
|
||||
newValue: this._systemAppearance,
|
||||
object: this
|
||||
});
|
||||
}
|
||||
});
|
||||
iosApp.notifyAppStarted();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function run(entry?: string | NavigationEntry) {
|
||||
createRootFrame.value = false;
|
||||
_start(entry);
|
||||
}
|
||||
|
||||
export function addCss(cssText: string, attributeScoped?: boolean): void {
|
||||
notify(<CssChangedEventData>{ eventName: "cssChanged", object: <any>iosApp, cssText: cssText });
|
||||
if (!attributeScoped) {
|
||||
const rootView = getRootView();
|
||||
if (rootView) {
|
||||
rootView._onCssStateChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function _resetRootView(entry?: NavigationEntry | string) {
|
||||
createRootFrame.value = false;
|
||||
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
|
||||
iosApp.setWindowContent();
|
||||
}
|
||||
|
||||
export function getNativeApplication(): UIApplication {
|
||||
return iosApp.nativeApp;
|
||||
}
|
||||
|
||||
function getSystemAppearanceValue(userInterfaceStyle: number): "dark" | "light" {
|
||||
switch (userInterfaceStyle) {
|
||||
case UIUserInterfaceStyle.Dark:
|
||||
return "dark";
|
||||
case UIUserInterfaceStyle.Light:
|
||||
case UIUserInterfaceStyle.Unspecified:
|
||||
return "light";
|
||||
}
|
||||
}
|
||||
|
||||
function getViewController(rootView: View): UIViewController {
|
||||
let viewController: UIViewController = rootView.viewController || rootView.ios;
|
||||
|
||||
if (!(viewController instanceof UIViewController)) {
|
||||
// We set UILayoutViewController dynamically to the root view if it doesn't have a view controller
|
||||
// At the moment the root view doesn't have its native view created. We set it in the setViewControllerView func
|
||||
viewController = iosView.UILayoutViewController.initWithOwner(new WeakRef(rootView)) as UIViewController;
|
||||
rootView.viewController = viewController;
|
||||
}
|
||||
|
||||
return viewController;
|
||||
}
|
||||
|
||||
function setViewControllerView(view: View): void {
|
||||
const viewController: UIViewController = view.viewController || view.ios;
|
||||
const nativeView = view.ios || view.nativeViewProtected;
|
||||
|
||||
if (!nativeView || !viewController) {
|
||||
throw new Error("Root should be either UIViewController or UIView");
|
||||
}
|
||||
|
||||
if (viewController instanceof iosView.UILayoutViewController) {
|
||||
viewController.view.addSubview(nativeView);
|
||||
}
|
||||
}
|
||||
|
||||
function setupRootViewCssClasses(rootView: View): void {
|
||||
resetRootViewCssClasses();
|
||||
|
||||
const deviceType = device.deviceType.toLowerCase();
|
||||
pushToRootViewCssClasses(`${CLASS_PREFIX}${IOS_PLATFORM}`);
|
||||
pushToRootViewCssClasses(`${CLASS_PREFIX}${deviceType}`);
|
||||
pushToRootViewCssClasses(`${CLASS_PREFIX}${iosApp.orientation}`);
|
||||
|
||||
if (majorVersion >= 13) {
|
||||
pushToRootViewCssClasses(`${CLASS_PREFIX}${iosApp.systemAppearance}`);
|
||||
}
|
||||
|
||||
const rootViewCssClasses = getRootViewCssClasses();
|
||||
rootViewCssClasses.forEach(c => rootView.cssClasses.add(c));
|
||||
}
|
||||
|
||||
export function orientation(): "portrait" | "landscape" | "unknown" {
|
||||
return iosApp.orientation;
|
||||
}
|
||||
|
||||
export function systemAppearance(): "dark" | "light" {
|
||||
return iosApp.systemAppearance;
|
||||
}
|
||||
|
||||
global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
|
||||
if (!started) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rootView = getRootView();
|
||||
livesync(rootView, context);
|
||||
};
|
6
nativescript-core/application/package.json
Normal file
6
nativescript-core/application/package.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "application",
|
||||
"main": "application",
|
||||
"types": "application.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
Reference in New Issue
Block a user