FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor: user interface orientation · NativeScript/NativeScript@0b9bd9d · GitHub

Commit 0b9bd9d

Browse files
committed
refactor: user interface orientation
Move orientation from device to application. iOS: Use `UIApplication.sharedApplication.statusBarOrientation` instead of `UIDevice.currentDevice.orientation`. Implements #1437.
1 parent 3e7c24a commit 0b9bd9d

14 files changed

Lines changed: 120 additions & 186 deletions

‎tests/app/application/application-tests-common.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
// >> application-require
2-
import * as app from "tns-core-modules/application";
1+
import * as app from "tns-core-modules/application";
32
import * as platform from "tns-core-modules/platform";
4-
// << application-require
53

6-
// >> application-app-check
4+
import * as TKUnit from "../tk-unit";
5+
76
if (app.android) {
8-
console.log("We are running on Android device!");
7+
console.log("We are running on an Android device!");
98
} else if (app.ios) {
10-
console.log("We are running on iOS device");
9+
console.log("We are running on an iOS device!");
1110
}
12-
// << application-app-check
13-
14-
import * as TKUnit from "../tk-unit";
1511

1612
export function testInitialized() {
1713
if (platform.device.os === platform.platformNames.android) {
18-
// we have the android defined
1914
TKUnit.assert(app.android, "Application module not properly intialized");
2015
} else if (platform.device.os === platform.platformNames.ios) {
2116
TKUnit.assert(app.ios, "Application module not properly intialized");

‎tests/app/application/application-tests.android.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ if (app.android) {
3737
}
3838
// << application-app-android-broadcast
3939

40-
export var testAndroidApplicationInitialized = function () {
40+
export function testAndroidApplicationInitialized() {
4141
TKUnit.assert(app.android, "Android application not initialized.");
4242
TKUnit.assert(app.android.context, "Android context not initialized.");
4343
TKUnit.assert(app.android.foregroundActivity, "Android foregroundActivity not initialized.");
44-
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is true");
44+
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is false.");
4545
TKUnit.assert(app.android.startActivity, "Android startActivity not initialized.");
4646
TKUnit.assert(app.android.nativeApp, "Android nativeApp not initialized.");
47+
TKUnit.assert(app.android.orientation, "Android orientation not initialized.");
4748
TKUnit.assert(app.android.packageName, "Android packageName not initialized.");
48-
};
49+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
/* tslint:disable */
2-
//@private
31
import * as android from "./application-tests.android";
4-
import * as iOS from "./application-tests.ios";
2+
import * as iOS from "./application-tests.ios";

‎tests/app/application/application-tests.ios.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* tslint:disable:no-unused-variable */
22
import * as app from "tns-core-modules/application";
3+
import * as TKUnit from "../tk-unit";
34

45
export * from "./application-tests-common";
56

@@ -39,3 +40,12 @@ if (app.ios) {
3940
}
4041

4142
// << application-ios-delegate
43+
44+
export function testIOSApplicationInitialized() {
45+
TKUnit.assert(app.ios, "iOS application not initialized.");
46+
TKUnit.assert(app.ios.delegate, "iOS delegate not initialized.");
47+
TKUnit.assert(app.ios.nativeApp, "iOS nativeApp not initialized.");
48+
TKUnit.assert(app.ios.orientation, "iOS orientation not initialized.");
49+
TKUnit.assert(app.ios.window, "iOS window not initialized.");
50+
TKUnit.assert(app.ios.rootController, "iOS root controller not initialized.");
51+
}

‎tests/app/application/application.md‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎tests/app/platform/platform-tests.ts‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ export function test_platform() {
1313
}
1414

1515
export function test_device_screen() {
16-
console.log("Device model: " + platformModule.device.model);
17-
console.log("Device manufacturer: " + platformModule.device.manufacturer);
18-
console.log("Device orientation: " + platformModule.device.orientation);
19-
console.log("Device type: " + platformModule.device.deviceType);
20-
console.log("Device UUID: " + platformModule.device.uuid);
16+
TKUnit.assert(platformModule.device.model, "Device model not initialized.");
17+
TKUnit.assert(platformModule.device.manufacturer, "Device manufacturer not initialized.");
18+
TKUnit.assert(platformModule.device.deviceType, "Device type not initialized.");
19+
TKUnit.assert(platformModule.device.uuid, "Device UUID not initialized.");
2120

22-
console.log("Preferred language: " + platformModule.device.language);
23-
console.log("Preferred region: " + platformModule.device.region);
21+
TKUnit.assert(platformModule.device.language, "Preferred language not initialized.");
22+
TKUnit.assert(platformModule.device.region, "Preferred region not initialized.");
2423

25-
console.log("OS: " + platformModule.device.os);
26-
console.log("OS version: " + platformModule.device.osVersion);
27-
console.log("SDK version: " + platformModule.device.sdkVersion);
24+
TKUnit.assert(platformModule.device.os, "OS not initialized.");
25+
TKUnit.assert(platformModule.device.osVersion, "OS version not initialized.");
26+
TKUnit.assert(platformModule.device.sdkVersion, "SDK version not initialized.");
2827

29-
console.log("Screen width (px): " + platformModule.screen.mainScreen.widthPixels);
30-
console.log("Screen height (px): " + platformModule.screen.mainScreen.heightPixels);
31-
console.log("Screen width (DIPs): " + platformModule.screen.mainScreen.widthDIPs);
32-
console.log("Screen height (DIPs): " + platformModule.screen.mainScreen.heightDIPs);
33-
console.log("Screen scale: " + platformModule.screen.mainScreen.scale);
28+
TKUnit.assert(platformModule.screen.mainScreen.widthPixels, "Screen width (px) not initialized.");
29+
TKUnit.assert(platformModule.screen.mainScreen.heightPixels, "Screen height (px) not initialized.");
30+
TKUnit.assert(platformModule.screen.mainScreen.widthDIPs, "Screen width (DIPs) not initialized.");
31+
TKUnit.assert(platformModule.screen.mainScreen.heightDIPs, "Screen height (DIPs) not initialized.");
32+
TKUnit.assert(platformModule.screen.mainScreen.scale, "Screen scale not initialized.");
3433
}
3534

3635
export function test_IsAndroid_IsIOS() {

‎tns-core-modules/application/application-common.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ export function addCss(cssText: string): void {
125125
}
126126

127127
on(orientationChangedEvent, (args: OrientationChangedEventData) => {
128-
if (device.orientation !== args.newValue) {
129-
device.orientation = args.newValue;
128+
if (app.orientation !== args.newValue) {
129+
app.orientation = args.newValue;
130130
}
131131
});
132132

‎tns-core-modules/application/application.android.ts‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
4141
public static activityNewIntentEvent = ActivityNewIntent;
4242
public static activityRequestPermissionsEvent = ActivityRequestPermissions;
4343

44+
private _orientation: "portrait" | "landscape" | "unknown";
4445
public paused: boolean;
4546
public nativeApp: android.app.Application;
4647
public context: android.content.Context;
@@ -79,6 +80,22 @@ export class AndroidApplication extends Observable implements AndroidApplication
7980
this._pendingReceiverRegistrations.length = 0;
8081
}
8182

83+
get orientation(): "portrait" | "landscape" | "unknown" {
84+
if (!this._orientation) {
85+
const resources = this.context.getResources();
86+
const configuration = <android.content.res.Configuration>resources.getConfiguration();
87+
const orientation = configuration.orientation;
88+
89+
this._orientation = getOrientationValue(orientation);
90+
}
91+
92+
return this._orientation;
93+
}
94+
95+
set orientation(value: "portrait" | "landscape" | "unknown") {
96+
this._orientation = value;
97+
}
98+
8299
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
83100
ensureBroadCastReceiverClass();
84101
const that = this;
@@ -223,6 +240,17 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
223240
livesync(rootView, context);
224241
};
225242

243+
function getOrientationValue(orientation: number): "portrait" | "landscape" | "unknown" {
244+
switch (orientation) {
245+
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
246+
return "landscape";
247+
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
248+
return "portrait";
249+
default:
250+
return "unknown";
251+
}
252+
}
253+
226254
function initLifecycleCallbacks() {
227255
const setThemeOnLaunch = profile("setThemeOnLaunch", (activity: androidx.appcompat.app.AppCompatActivity) => {
228256
// Set app theme after launch screen was used during startup
@@ -312,7 +340,6 @@ function initLifecycleCallbacks() {
312340
return lifecycleCallbacks;
313341
}
314342

315-
let currentOrientation: number;
316343
function initComponentCallbacks() {
317344
let componentCallbacks = new android.content.ComponentCallbacks2({
318345
onLowMemory: profile("onLowMemory", function () {
@@ -326,32 +353,19 @@ function initComponentCallbacks() {
326353
}),
327354

328355
onConfigurationChanged: profile("onConfigurationChanged", function (newConfig: android.content.res.Configuration) {
329-
const newOrientation = newConfig.orientation;
330-
if (newOrientation === currentOrientation) {
331-
return;
356+
const newConfigOrientation = newConfig.orientation;
357+
const newOrientation = getOrientationValue(newConfigOrientation);
358+
359+
if (androidApp.orientation !== newOrientation) {
360+
androidApp.orientation = newOrientation;
361+
362+
notify(<OrientationChangedEventData>{
363+
eventName: orientationChangedEvent,
364+
android: androidApp.nativeApp,
365+
newValue: androidApp.orientation,
366+
object: androidApp
367+
});
332368
}
333-
334-
currentOrientation = newOrientation;
335-
let newValue;
336-
337-
switch (newOrientation) {
338-
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
339-
newValue = "landscape";
340-
break;
341-
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
342-
newValue = "portrait";
343-
break;
344-
default:
345-
newValue = "unknown";
346-
break;
347-
}
348-
349-
notify(<OrientationChangedEventData>{
350-
eventName: orientationChangedEvent,
351-
android: androidApp.nativeApp,
352-
newValue: newValue,
353-
object: androidApp
354-
});
355369
})
356370
});
357371

@@ -390,4 +404,4 @@ declare namespace com {
390404
static getInstance(): NativeScriptApplication;
391405
}
392406
}
393-
}
407+
}

‎tns-core-modules/application/application.d.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ export class AndroidApplication extends Observable {
409409
*/
410410
startActivity: any /* androidx.appcompat.app.AppCompatActivity */;
411411

412+
/**
413+
* Gets or sets the orientation of the application.
414+
* Available values: "portrait", "landscape", "unknown".
415+
*/
416+
orientation: "portrait" | "landscape" | "unknown";
417+
412418
/**
413419
* The name of the application package.
414420
*/
@@ -578,14 +584,20 @@ export interface iOSApplication {
578584
window: any /* UIWindow */;
579585

580586
/**
581-
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
587+
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
582588
*/
583-
nativeApp: any /* UIApplication */;
589+
delegate: any /* typeof UIApplicationDelegate */;
584590

585591
/**
586-
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
592+
* Gets or sets the orientation of the application.
593+
* Available values: "portrait", "landscape", "unknown".
587594
*/
588-
delegate: any /* typeof UIApplicationDelegate */;
595+
orientation: "portrait" | "landscape" | "unknown";
596+
597+
/**
598+
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
599+
*/
600+
nativeApp: any /* UIApplication */;
589601

590602
/**
591603
* Adds an observer to the default notification center for the specified notification.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL