Application orientation changed event

This commit is contained in:
vakrilov
2015-07-29 11:43:12 +03:00
parent 5b2679717e
commit 6ebd488735
19 changed files with 274 additions and 66 deletions

View File

@@ -14,6 +14,7 @@ export var resumeEvent = "resume";
export var exitEvent = "exit";
export var lowMemoryEvent = "lowMemory";
export var uncaughtErrorEvent = "uncaughtError";
export var orientationChangedEvent = "orientationChanged";
export var cssFile: string = "app.css"

View File

@@ -3,6 +3,7 @@ import dts = require("application");
import frame = require("ui/frame");
import types = require("utils/types");
import observable = require("data/observable");
import enums = require("ui/enums");
global.moduleMerge(appModule, exports);
@@ -206,6 +207,8 @@ export class AndroidApplication extends observable.Observable implements dts.And
exports.notify({ eventName: dts.launchEvent, object: this, android: intent });
setupOrientationListener(this);
/* In the onLaunch event we expect the following setup, which ensures a root frame:
* var frame = require("ui/frame");
* var rootFrame = new frame.Frame();
@@ -315,3 +318,36 @@ exports.start = function () {
}
exports.android = new AndroidApplication();
var currentOrientation: number;
function setupOrientationListener(androidApp: AndroidApplication) {
androidApp.registerBroadcastReceiver(android.content.Intent.ACTION_CONFIGURATION_CHANGED, onConfigurationChanged);
currentOrientation = androidApp.context.getResources().getConfiguration().orientation
}
function onConfigurationChanged(context: android.content.Context, intent: android.content.Intent) {
var orientation = context.getResources().getConfiguration().orientation;
if (currentOrientation !== orientation) {
currentOrientation = orientation;
var newValue;
switch (orientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
newValue = enums.DeviceOrientation.landscape;
break;
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
newValue = enums.DeviceOrientation.portrait;
break;
default:
newValue = enums.DeviceOrientation.unknown;
break;
}
exports.notify(<dts.OrientationChangedEventData> {
eventName: dts.orientationChangedEvent,
android: context,
newValue: newValue,
object: exports.android,
});
}
}

View File

@@ -45,6 +45,11 @@ declare module "application" {
*/
export var lowMemoryEvent: string;
/**
* String value used when hooking to orientationChanged event.
*/
export var orientationChangedEvent: string;
/**
* Event data containing information for the application events.
*/
@@ -70,6 +75,16 @@ declare module "application" {
object: any;
}
/**
* Event data containing information for orientation changed event.
*/
export interface OrientationChangedEventData extends ApplicationEventData {
/**
* New orientation value.
*/
newValue: string;
}
/**
* 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:
@@ -146,6 +161,14 @@ declare module "application" {
*/
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.
@@ -188,6 +211,11 @@ declare module "application" {
*/
export function on(event: "uncaughtError", callback: (args: ApplicationEventData) => void, thisArg?: any);
/**
* This event is raised the orientation of the current device has changed.
*/
export function on(event: "orientationChanged", callback: (args: OrientationChangedEventData) => void, thisArg?: any);
/**
* This is the Android-specific application object instance.
* Encapsulates methods and properties specific to the Android platform.

View File

@@ -4,7 +4,7 @@ import utils = require("utils/utils");
import types = require("utils/types");
import view = require("ui/core/view");
import definition = require("application");
import enums = require("ui/enums");
global.moduleMerge(appModule, exports);
export var mainModule: string;
@@ -61,10 +61,11 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
return;
}
}
var app: IOSApplication = exports.ios;
setupOrientationListener(app);
this.window.content = topFrame;
this.window.rootViewController = topFrame.ios.controller;
var app: IOSApplication = exports.ios;
app.rootController = this.window.rootViewController;
this.window.makeKeyAndVisible();
return true;
@@ -193,4 +194,40 @@ exports.start = function () {
definition.notify({ eventName: definition.uncaughtErrorEvent, object: <any>definition.ios, ios: error });
}
}
}
var currentOrientation: number;
function setupOrientationListener(iosApp: IOSApplication) {
iosApp.addNotificationObserver(UIDeviceOrientationDidChangeNotification, onOreintationDidChange)
currentOrientation = UIDevice.currentDevice().orientation;
}
function onOreintationDidChange(notification: NSNotification) {
var orientation = UIDevice.currentDevice().orientation;
if (currentOrientation !== orientation) {
currentOrientation = orientation;
var newValue;
switch (orientation) {
case UIDeviceOrientation.UIDeviceOrientationLandscapeRight:
case UIDeviceOrientation.UIDeviceOrientationLandscapeLeft:
newValue = enums.DeviceOrientation.landscape;
break;
case UIDeviceOrientation.UIDeviceOrientationPortrait:
case UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown:
newValue = enums.DeviceOrientation.portrait;
break;
default:
newValue = enums.DeviceOrientation.unknown;
break;
}
exports.notify(<definition.OrientationChangedEventData>{
eventName: definition.orientationChangedEvent,
ios: exports.ios,
newValue: newValue,
object: exports.ios,
});
}
}