feat: expose application orientation (#7602)

This commit is contained in:
Vasil Chimev
2019-08-14 13:47:15 +03:00
committed by GitHub
parent a14bc9f86d
commit e2c3c8c084
15 changed files with 161 additions and 155 deletions

View File

@@ -35,10 +35,10 @@ export { Observable };
import {
AndroidApplication,
CssChangedEventData,
DiscardedErrorEventData,
iOSApplication,
LoadAppCSSEventData,
UnhandledErrorEventData,
DiscardedErrorEventData,
UnhandledErrorEventData
} from "./application";
export { UnhandledErrorEventData, DiscardedErrorEventData, CssChangedEventData, LoadAppCSSEventData };

View File

@@ -42,6 +42,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
public static activityNewIntentEvent = ActivityNewIntent;
public static activityRequestPermissionsEvent = ActivityRequestPermissions;
private _orientation: "portrait" | "landscape" | "unknown";
public paused: boolean;
public nativeApp: android.app.Application;
public context: android.content.Context;
@@ -80,6 +81,22 @@ export class AndroidApplication extends Observable implements AndroidApplication
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();
const orientation = configuration.orientation;
this._orientation = getOrientationValue(orientation);
}
return this._orientation;
}
set orientation(value: "portrait" | "landscape" | "unknown") {
this._orientation = value;
}
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
ensureBroadCastReceiverClass();
const that = this;
@@ -232,6 +249,17 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
livesync(rootView, context);
};
function getOrientationValue(orientation: number): "portrait" | "landscape" | "unknown" {
switch (orientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
return "landscape";
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
return "portrait";
default:
return "unknown";
}
}
function initLifecycleCallbacks() {
const setThemeOnLaunch = profile("setThemeOnLaunch", (activity: androidx.appcompat.app.AppCompatActivity) => {
// Set app theme after launch screen was used during startup
@@ -321,7 +349,6 @@ function initLifecycleCallbacks() {
return lifecycleCallbacks;
}
let currentOrientation: number;
function initComponentCallbacks() {
let componentCallbacks = new android.content.ComponentCallbacks2({
onLowMemory: profile("onLowMemory", function () {
@@ -335,32 +362,19 @@ function initComponentCallbacks() {
}),
onConfigurationChanged: profile("onConfigurationChanged", function (newConfig: android.content.res.Configuration) {
const newOrientation = newConfig.orientation;
if (newOrientation === currentOrientation) {
return;
const newConfigOrientation = newConfig.orientation;
const newOrientation = getOrientationValue(newConfigOrientation);
if (androidApp.orientation !== newOrientation) {
androidApp.orientation = newOrientation;
notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
android: androidApp.nativeApp,
newValue: androidApp.orientation,
object: androidApp
});
}
currentOrientation = newOrientation;
let newValue;
switch (newOrientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
newValue = "landscape";
break;
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
newValue = "portrait";
break;
default:
newValue = "unknown";
break;
}
notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
android: androidApp.nativeApp,
newValue: newValue,
object: androidApp
});
})
});
@@ -399,4 +413,4 @@ declare namespace com {
static getInstance(): NativeScriptApplication;
}
}
}
}

View File

@@ -413,6 +413,12 @@ export class AndroidApplication extends Observable {
*/
startActivity: any /* androidx.appcompat.app.AppCompatActivity */;
/**
* Gets the orientation of the application.
* Available values: "portrait", "landscape", "unknown".
*/
orientation: "portrait" | "landscape" | "unknown";
/**
* The name of the application package.
*/
@@ -581,16 +587,22 @@ export interface iOSApplication {
*/
window: any /* UIWindow */;
/**
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
*/
nativeApp: any /* UIApplication */;
/**
* 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";
/**
* 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:'

View File

@@ -78,9 +78,9 @@ class CADisplayLinkTarget extends NSObject {
class IOSApplication implements IOSApplicationDefinition {
private _delegate: typeof UIApplicationDelegate;
private _currentOrientation = UIDevice.currentDevice.orientation;
private _window: UIWindow;
private _observers: Array<NotificationObserver>;
private _orientation: "portrait" | "landscape" | "unknown";
private _rootView: View;
constructor() {
@@ -90,7 +90,16 @@ class IOSApplication implements IOSApplicationDefinition {
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
this.addNotificationObserver(UIApplicationWillTerminateNotification, this.willTerminate.bind(this));
this.addNotificationObserver(UIApplicationDidReceiveMemoryWarningNotification, this.didReceiveMemoryWarning.bind(this));
this.addNotificationObserver(UIDeviceOrientationDidChangeNotification, this.orientationDidChange.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 {
@@ -197,40 +206,39 @@ class IOSApplication implements IOSApplicationDefinition {
}
}
private didReceiveMemoryWarning(notification: NSNotification) {
notify(<ApplicationEventData>{ eventName: lowMemoryEvent, object: this, ios: UIApplication.sharedApplication });
}
private didChangeStatusBarOrientation(notification: NSNotification) {
const statusBarOrientation = UIApplication.sharedApplication.statusBarOrientation;
const newOrientation = this.getOrientationValue(statusBarOrientation);
private orientationDidChange(notification: NSNotification) {
const orientation = UIDevice.currentDevice.orientation;
if (this._currentOrientation !== orientation) {
this._currentOrientation = orientation;
let newValue: "portrait" | "landscape" | "unknown";
switch (orientation) {
case UIDeviceOrientation.LandscapeRight:
case UIDeviceOrientation.LandscapeLeft:
newValue = "landscape";
break;
case UIDeviceOrientation.Portrait:
case UIDeviceOrientation.PortraitUpsideDown:
newValue = "portrait";
break;
default:
newValue = "unknown";
break;
}
if (this._orientation !== newOrientation) {
this._orientation = newOrientation;
notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
ios: this,
newValue: newValue,
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";

View File

@@ -19,10 +19,6 @@ class Device implements DeviceDefinition {
private _language: string;
private _region: string;
get os(): string {
return platformNames.android;
}
get manufacturer(): string {
if (!this._manufacturer) {
this._manufacturer = android.os.Build.MANUFACTURER;
@@ -31,6 +27,10 @@ class Device implements DeviceDefinition {
return this._manufacturer;
}
get os(): string {
return platformNames.android;
}
get osVersion(): string {
if (!this._osVersion) {
this._osVersion = android.os.Build.VERSION.RELEASE;

View File

@@ -125,4 +125,4 @@ export module screen {
/**
* Gets the current device information.
*/
export const device: Device;
export const device: Device;

View File

@@ -78,7 +78,7 @@ class Device implements DeviceDefinition {
const languages = NSLocale.preferredLanguages;
this._language = languages[0];
}
return this._language;
}
@@ -93,7 +93,7 @@ class Device implements DeviceDefinition {
class MainScreen implements ScreenMetricsDefinition {
private _screen: UIScreen;
private get screen(): UIScreen {
if (!this._screen) {
this._screen = UIScreen.mainScreen;

View File

@@ -215,6 +215,8 @@ export module ios {
}
/**
* @deprecated use application.orientation instead
*
* Gets an information about if current mode is Landscape.
*/
export function isLandscape(): boolean;

View File

@@ -8,7 +8,8 @@ export * from "./utils-common";
let mainScreenScale;
function isOrientationLandscape(orientation: number) {
return orientation === UIDeviceOrientation.LandscapeLeft || orientation === UIDeviceOrientation.LandscapeRight;
return orientation === UIDeviceOrientation.LandscapeLeft /* 3 */ ||
orientation === UIDeviceOrientation.LandscapeRight /* 4 */;
}
export module layout {
@@ -79,11 +80,15 @@ export module ios {
}
export function isLandscape(): boolean {
const device = UIDevice.currentDevice;
console.log("utils.ios.isLandscape() is deprecated; use application.orientation instead");
const deviceOrientation = UIDevice.currentDevice.orientation;
const statusBarOrientation = UIApplication.sharedApplication.statusBarOrientation;
const isDeviceOrientationLandscape = isOrientationLandscape(deviceOrientation);
const isStatusBarOrientationLandscape = isOrientationLandscape(statusBarOrientation);
return isOrientationLandscape(device.orientation) || isStatusBarOrientationLandscape;
return isDeviceOrientationLandscape || isStatusBarOrientationLandscape;
}
export const MajorVersion = NSString.stringWithString(UIDevice.currentDevice.systemVersion).intValue;
@@ -153,7 +158,7 @@ export function openFile(filePath: string): boolean {
}
// Need this so that we can use this function inside the ios module (avoid name clashing).
const openFileAtRootModule = openFile;
const openFileAtRootModule = openFile;
export function GC() {
__collect();