application module events added

This commit is contained in:
Vladimir Enchev
2015-06-01 15:10:47 +03:00
parent 79caa9185c
commit c6549343bc
6 changed files with 231 additions and 109 deletions

View File

@ -3,6 +3,10 @@ import definition = require("application");
import fs = require("file-system");
import fileSystemAccess = require("file-system/file-system-access");
import styleScope = require("ui/styling/style-scope");
import observable = require("data/observable");
var events = new observable.Observable();
require("utils/module-merge").merge(events, exports);
export var cssFile: string = "app.css"

View File

@ -43,6 +43,9 @@ var initEvents = function () {
if (exports.onExit) {
exports.onExit();
}
exports.notify({ eventName: "onExit", object: androidApp, android: activity, ios: undefined });
androidApp.startActivity = undefined;
}
@ -58,6 +61,9 @@ var initEvents = function () {
if (exports.onSuspend) {
exports.onSuspend();
}
exports.notify({ eventName: "onSuspend", object: androidApp, android: activity, ios: undefined });
}
if (androidApp.onActivityPaused) {
@ -69,6 +75,9 @@ var initEvents = function () {
if (exports.onResume) {
exports.onResume();
}
exports.notify({ eventName: "onResume", object: androidApp, android: activity, ios: undefined });
}
if (androidApp.onActivityResumed) {
@ -183,6 +192,8 @@ class AndroidApplication implements dts.AndroidApplication {
exports.onLaunch(intent);
}
exports.notify({ eventName: "onLaunch", object: this, android: intent, ios: undefined });
/* In the onLaunch event we expect the following setup, which ensures a root frame:
* var frame = require("ui/frame");
* var rootFrame = new frame.Frame();
@ -224,6 +235,8 @@ global.__onUncaughtError = function (error: Error) {
}
exports.onUncaughtError(nsError);
exports.notify({ eventName: "onUncaughtError", object: appModule.android, android: error, ios: undefined });
}
exports.start = function () {

View File

@ -3,6 +3,7 @@
*/
declare module "application" {
import cssSelector = require("ui/styling/css-selector");
import observable = require("data/observable");
/**
* An extended JavaScript Error which will have the nativeError property initialized in case the error is caused by executing platform-specific code.
@ -14,6 +15,21 @@ declare module "application" {
nativeError: any;
}
/**
* Event data containing information for the application events.
*/
export interface ApplicationEventData extends observable.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 main page path (without the file extension) for the application starting from the application root.
* For example if you have page called "main.js" in a folder called "subFolder" and your root folder is "app" you can specify mainModule like this:
@ -82,6 +98,56 @@ declare module "application" {
*/
export function onLowMemory();
/**
* 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);
/**
* 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: ApplicationEventData): 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 launch.
*/
export function on(event: "onLaunch", callback: (args: any) => void, thisArg?: any);
/**
* This event is raised when an uncaught error occurs while the application is running.
*/
export function on(event: "onUncaughtError", callback: (args: any) => void, thisArg?: any);
/**
* This event is raised when the Application is suspended.
*/
export function on(event: "onSuspend", callback: (args: any) => void, thisArg?: any);
/**
* This event is raised when the Application is resumed after it has been suspended.
*/
export function on(event: "onResume", callback: (args: any) => void, thisArg?: any);
/**
* This event is raised when the Application is about to exit.
*/
export function on(event: "onExit", callback: (args: any) => void, thisArg?: any);
/**
* This event is raised when there is low memory on the target device.
*/
export function on(event: "onLowMemory", callback: (args: any) => void, thisArg?: any);
/**
* This is the Android-specific application object instance.
* Encapsulates methods and properties specific to the Android platform.
@ -178,12 +244,12 @@ declare module "application" {
onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void
}
/* tslint:disable */
/* tslint:disable */
/**
* The abstraction of an iOS-specific application object.
*/
export interface iOSApplication {
/* tslint:enable */
/* tslint:enable */
/**
* The root view controller for the application.
*/

View File

@ -50,6 +50,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
exports.onLaunch();
}
exports.notify({ eventName: "onLaunch", object: this, android: undefined, ios: launchOptions });
var topFrame = frame.topmost();
if (!topFrame) {
if (mainModule) {
@ -74,6 +76,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
if (exports.onResume) {
exports.onResume();
}
exports.notify({ eventName: "onResume", object: this, android: undefined, ios: application });
}
applicationWillResignActive(application: UIApplication) {
@ -84,6 +88,8 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
if (exports.onSuspend) {
exports.onSuspend();
}
exports.notify({ eventName: "onSuspend", object: this, android: undefined, ios: application });
}
applicationWillEnterForeground(application: UIApplication) {
@ -94,12 +100,16 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
if (exports.onExit) {
exports.onExit();
}
exports.notify({ eventName: "onExit", object: this, android: undefined, ios: application });
}
applicationDidReceiveMemoryWarning(application: UIApplication) {
if (exports.onLowMemory) {
exports.onLowMemory();
}
exports.notify({ eventName: "onLowMemory", object: this, android: undefined, ios: application });
}
applicationOpenURLSourceApplicationAnnotation(application: UIApplication, url: NSURL, sourceApplication: string, annotation: any): boolean {
@ -147,5 +157,7 @@ exports.start = function () {
}
exports.onUncaughtError(error);
definition.notify({ eventName: "onUncaughtError", object: <any>definition.ios, android: undefined, ios: error });
}
}

View File

@ -1,3 +1,28 @@
import application = require("application");
application.mainModule = "app/mainPage";
application.on("onLaunch", function (args) {
console.log("onLaunch: " + args);
});
application.on("onUncaughtError", function (args) {
console.log("onUncaughtError: " + args);
});
application.on("onSuspend", function (args) {
console.log("onSuspend: " + args);
});
application.on("onResume", function (args) {
console.log("onResume: " + args);
});
application.on("onExit", function (args) {
console.log("onExit: " + args);
});
application.on("onLowMemory", function (args) {
console.log("onLowMemory: " + args);
});
application.start();

View File

@ -382,7 +382,7 @@ var NativeActivity = {
return this[ANDROID_FRAME];
},
onCreate: function(savedInstanceState: android.os.Bundle) {
onCreate: function (savedInstanceState: android.os.Bundle) {
trace.write("NativeScriptActivity.onCreate(); savedInstanceState: " + savedInstanceState, trace.categories.NativeLifecycle);
// Find the frame for this activity.
@ -418,7 +418,7 @@ var NativeActivity = {
this.frame._onActivityCreated(isRestart);
},
onActivityResult: function(requestCode: number, resultCode: number, data: android.content.Intent) {
onActivityResult: function (requestCode: number, resultCode: number, data: android.content.Intent) {
this.super.onActivityResult(requestCode, resultCode, data);
trace.write("NativeScriptActivity.onActivityResult();", trace.categories.NativeLifecycle);
@ -428,7 +428,7 @@ var NativeActivity = {
}
},
onAttachFragment: function(fragment: android.app.Fragment) {
onAttachFragment: function (fragment: android.app.Fragment) {
trace.write("NativeScriptActivity.onAttachFragment() : " + fragment.getTag(), trace.categories.NativeLifecycle);
this.super.onAttachFragment(fragment);
@ -439,19 +439,19 @@ var NativeActivity = {
}
},
onStart: function() {
onStart: function () {
this.super.onStart();
trace.write("NativeScriptActivity.onStart();", trace.categories.NativeLifecycle);
this.frame.onLoaded();
},
onStop: function() {
onStop: function () {
this.super.onStop();
trace.write("NativeScriptActivity.onStop();", trace.categories.NativeLifecycle);
this.frame.onUnloaded();
},
onDestroy: function() {
onDestroy: function () {
// TODO: Implement uninitialized(detached) routine
var frame = this.frame;
frame._onDetached(true);
@ -467,7 +467,7 @@ var NativeActivity = {
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
},
onOptionsItemSelected: function(menuItem: android.view.IMenuItem) {
onOptionsItemSelected: function (menuItem: android.view.IMenuItem) {
if (!this.androidFrame.hasListeners(frameCommon.Frame.androidOptionSelectedEvent)) {
return false;
}
@ -483,20 +483,22 @@ var NativeActivity = {
return data.handled;
},
onBackPressed: function() {
onBackPressed: function () {
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
if (!frameCommon.goBack()) {
this.super.onBackPressed();
}
},
onLowMemory: function() {
onLowMemory: function () {
gc();
java.lang.System.gc();
this.super.onLowMemory();
application.notify(<application.ApplicationEventData>{ eventName: "onLowMemory", object: this, android: undefined, ios: undefined });
},
onTrimMemory: function(level: number) {
onTrimMemory: function (level: number) {
gc();
java.lang.System.gc();
this.super.onTrimMemory(level);
@ -668,7 +670,7 @@ function findPageForFragment(fragment: android.app.Fragment, frame: Frame) {
}
function startActivity(activity: android.app.Activity, entry: definition.NavigationEntry) {
var intent = new android.content.Intent(activity, (<any>com).tns.NativeScriptActivity.class);
var intent = new android.content.Intent(activity,(<any>com).tns.NativeScriptActivity.class);
intent.setAction(android.content.Intent.ACTION_DEFAULT);
// TODO: Put the navigation context (if any) in the intent
activity.startActivity(intent);