Modified the Application module. Removed the Application class and added its methods to the exports directly.

This commit is contained in:
atanasovg
2014-05-09 19:25:11 +03:00
parent 7aa70fb14c
commit 44c51a0e48
10 changed files with 70 additions and 108 deletions

View File

@ -7,7 +7,7 @@ require("Utils/module_merge").merge(appModule, exports);
var callbacks = android.app.Application.ActivityLifecycleCallbacks;
var initEvents = function () {
var androidApp = appModule.current.android;
var androidApp = exports.android;
var lifecycleCallbacks = new callbacks({
onActivityCreated: function (activity: any, bundle: any) {
if (!androidApp.startActivity) {
@ -87,8 +87,7 @@ var initEvents = function () {
export var init = function (nativeApp: android.app.Application) {
var app = new AndroidApplication(nativeApp);
appModule.current.os = appModule.TargetOS.Android;
appModule.current.android = app;
exports.android = app;
app.init();
}

View File

@ -1,67 +1,42 @@
/**
* Defines the available target operating systems.
* The main entry point event. This method is expected to return an instance of the root UI for the application.
* This will be an Activity extends for Android and a RootViewController for iOS.
*/
export declare enum TargetOS {
/**
* iOS operating system.
*/
iOS,
/**
* Android operating system.
*/
Android
}
export declare function onLaunch(): any;
/**
* The abstraction of an Application object, common for each target OS.
* This method will be called when the Application is suspended.
*/
export declare class Application {
/**
* The target operating system of the application.
*/
public os: TargetOS;
export declare function onSuspend();
/**
* The main entry point event. This method is expected to return an instance of the root UI for the application.
* This will be an Activity extends for Android and a RootViewController for iOS.
*/
public onLaunch: () => any;
/**
* This method will be called when the Application is resumed after it has been suspended.
*/
export declare function onResume();
/**
* This method will be called when the Application is suspended.
*/
public onSuspend: () => any;
/**
* This method will be called when the Application is about to exit.
*/
export declare function onExit();
/**
* This method will be called when the Application is resumed after it has been suspended.
*/
public onResume: () => any;
/**
* This method will be called when there is low memory on the target device.
*/
export declare function onLowMemory();
/**
* This method will be called when the Application is about to exit.
*/
public onExit: () => any;
/**
* 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 declare var android: AndroidApplication;
/**
* This method will be called when there is low memory on the target device.
*/
public onLowMemory: () => any;
/**
* This is the Android-specific application object instance.
* Encapsulates methods and properties specific to the Android platform.
* Will be undefined when TargetOS is iOS.
*/
public 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.
*/
public ios: iOSApplication;
}
/**
* 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 declare var ios: iOSApplication;
/**
* The abstraction of an Android-specific application object.
@ -147,9 +122,4 @@ export declare class iOSApplication {
/**
* Entry point for the module. Initializes the Application singleton and hooks application lifecycle events.
*/
export declare function init(nativeApp: any);
/**
* The current singleton instance of the application object.
*/
export declare var current: Application;
export declare function init(nativeApp: any);

View File

@ -29,8 +29,7 @@ require("Utils/module_merge").merge(appModule, exports);
// TODO: Declarations
export var init = function (nativeApp: any) {
var app = new iOSApplication(nativeApp);
appModule.current.os = appModule.TargetOS.iOS;
appModule.current.ios = app;
exports.ios = app;
app.init();
}
@ -55,10 +54,8 @@ class iOSApplication {
this.window.backgroundColor = UIKit.UIColor.whiteColor();
this.window.makeKeyAndVisible();
var iosApp = appModule.current.ios;
if (appModule.current.onLaunch) {
this.window.rootViewController = appModule.current.onLaunch();
if (appModule.onLaunch) {
this.window.rootViewController = appModule.onLaunch();
} else {
log("Missing TK.UI.Application.current.onLaunch");
}
@ -69,8 +66,8 @@ class iOSApplication {
applicationDidBecomeActive: function (application) {
log("applicationDidBecomeActive: " + application);
if (appModule.current.onResume) {
appModule.current.onResume();
if (appModule.onResume) {
appModule.onResume();
}
},
@ -80,8 +77,8 @@ class iOSApplication {
applicationDidEnterBackground: function (application) {
log("applicationDidEnterBackground: " + application);
if (appModule.current.onSuspend) {
appModule.current.onSuspend();
if (appModule.onSuspend) {
appModule.onSuspend();
}
},
@ -91,15 +88,15 @@ class iOSApplication {
applicationWillTerminate: function (application) {
log("applicationWillTerminate: " + application);
if (appModule.current.onExit) {
appModule.current.onExit();
if (appModule.onExit) {
appModule.onExit();
}
},
applicationDidReceiveMemoryWarning: function (application) {
log("applicationDidReceiveMemoryWarning: " + application);
if (appModule.current.onLowMemory) {
appModule.current.onLowMemory();
if (appModule.onLowMemory) {
appModule.onLowMemory();
}
}
}

View File

@ -1,27 +1,23 @@
import consoleModule = require("Console/console");
export enum TargetOS {
iOS,
Android
}
export class Application {
public os: TargetOS;
// TODO: This is put in the global context, is this the preferred approach
console = new consoleModule.Console();
constructor() {
// TODO: This is put in the global context, is this the preferred approach
console = new consoleModule.Console();
}
public onLaunch: () => any;
public onSuspend: () => any;
public onResume: () => any;
public onExit: () => any;
public onLowMemory: () => any;
// TODO: These fields are declared by the application.d.ts file and intellisense will come from there
public android: any;
public ios: any;
export var onLaunch = function (): any {
}
export var current = new Application();
export var onSuspend = function (): void {
}
export var onResume = function (): void {
}
export var onExit = function (): void {
}
export var onLowMemory = function (): void {
}
export var android = undefined;
export var ios = undefined;

View File

@ -6,7 +6,7 @@ var REQUEST_SELECT_PICTURE: number = 2;
export class CameraManager {
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
var takePictureIntent = new android.content.Intent('android.media.action.IMAGE_CAPTURE');
var androidApp = appModule.current.android;
var androidApp = appModule.android;
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
androidApp.currentActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
@ -16,7 +16,7 @@ export class CameraManager {
// options { useSavedPhotos: true }
public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
var readPictureIntent = new android.content.Intent();
var androidApp = appModule.current.android;
var androidApp = appModule.android;
readPictureIntent.setType('image/*');
readPictureIntent.setAction('android.intent.action.GET_CONTENT');

View File

@ -207,13 +207,13 @@ export class FileSystemAccess {
}
public getDocumentsFolderPath(): string {
var context = appModule.current.android.context;
var context = appModule.android.context;
var dir = context.getFilesDir();
return dir.getAbsolutePath();
}
public getTempFolderPath(): string {
var context = appModule.current.android.context;
var context = appModule.android.context;
var dir = context.getCacheDir();
return dir.getAbsolutePath();
}

View File

@ -13,7 +13,7 @@ export class Image {
}
public loadFromResource(name: string): boolean {
var androidApp = appModule.current.android;
var androidApp = appModule.android;
var res = androidApp.context.getResources();
if (res) {
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);

View File

@ -59,7 +59,7 @@ export class LocationManager {
public static isEnabled(): boolean {
var criteria = new android.location.Criteria();
criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?)
var lm = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
var lm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
return (lm.getBestProvider(criteria, true) != null) ? true : false;
}
@ -80,7 +80,7 @@ export class LocationManager {
this.minimumUpdateTime = 200;
this.isStarted = false;
this.androidLocationManager = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
}
////////////////////////

View File

@ -5,7 +5,7 @@ export class UserPreferences {
private sharedPreferences: any;
constructor() {
this.sharedPreferences = appModule.current.android.context.getSharedPreferences("prefs.db", 0);
this.sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
}
public containsKey(key: string): boolean {

View File

@ -11,7 +11,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
var context = require("Application/application").Application.current.android.context;
var context = require("Application/application").android.context;
if (isImage && options.method && options.method.toLowerCase() == "get") {
var request = com.koushikdutta.ion.Ion.with(context, options.url);