From e6d64799d80940c17a199efa4f3734d7bc91261f Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Mon, 12 Dec 2016 09:51:45 +0200 Subject: [PATCH] Fix: Android platform.screen.mainScreen props are not invalidated after orientation change Resolves #3270 --- .../file-name-resolver-tests.ts | 2 +- .../application/application-common.ts | 29 +++++++++++++-- .../application/application.android.ts | 1 + .../application/application.ios.ts | 1 + .../file-system/file-name-resolver.d.ts | 5 --- .../file-system/file-name-resolver.ts | 29 ++++++++------- tns-core-modules/platform/platform.android.ts | 11 ++++++ tns-core-modules/platform/platform.ios.ts | 5 +++ tns-core-modules/ui/frame/frame-common.ts | 36 +++++++++++++++++++ 9 files changed, 95 insertions(+), 24 deletions(-) diff --git a/tests/app/file-name-resolver-tests/file-name-resolver-tests.ts b/tests/app/file-name-resolver-tests/file-name-resolver-tests.ts index ed400faf2..2a6981126 100644 --- a/tests/app/file-name-resolver-tests/file-name-resolver-tests.ts +++ b/tests/app/file-name-resolver-tests/file-name-resolver-tests.ts @@ -208,7 +208,7 @@ export function test_findFileMatch_with_multiple_matches_loads_by_priority() { } function testTemplate(candidates: Array, context: resolver.PlatformContext, expected: string) { - var result = resolver.findFileMatch("test", ".xml", candidates, context); + var result = resolver._findFileMatch("test", ".xml", candidates, context); TKUnit.assertEqual(result, expected, "File path"); } diff --git a/tns-core-modules/application/application-common.ts b/tns-core-modules/application/application-common.ts index 8740cf24e..a891ded6a 100644 --- a/tns-core-modules/application/application-common.ts +++ b/tns-core-modules/application/application-common.ts @@ -8,6 +8,7 @@ import * as fileSystemModule from "file-system"; import * as styleScopeModule from "ui/styling/style-scope"; import * as fileResolverModule from "file-system/file-name-resolver"; import * as builderModule from "ui/builder"; +import * as platformModule from "platform"; import "../bundle-entry-points"; var builder: typeof builderModule; @@ -17,6 +18,20 @@ function ensureBuilder() { } } +var platform: typeof platformModule; +function ensurePlatform() { + if (!platform) { + platform = require("platform"); + } +} + +var fileNameResolver: typeof fileResolverModule; +function ensureFileNameResolver() { + if (!fileNameResolver) { + fileNameResolver = require("file-system/file-name-resolver"); + } +} + var styleScope: typeof styleScopeModule = undefined; var events = new observable.Observable(); @@ -113,10 +128,10 @@ export function __onLiveSync() { } try { - var fileResolver: typeof fileResolverModule = require("file-system/file-name-resolver"); + ensureFileNameResolver(); // Clear file resolver cache to respect newly added files. - fileResolver.clearCache(); + fileNameResolver.clearCache(); // Reload app.css in case it was changed. loadCss(); @@ -135,4 +150,12 @@ export function __onLiveSyncCore() { // Reload current page. reloadPage(); } -global.__onLiveSyncCore = __onLiveSyncCore; \ No newline at end of file +global.__onLiveSyncCore = __onLiveSyncCore; + +export function _onOrientationChanged(){ + ensurePlatform(); + platform.screen.mainScreen._invalidate(); + + ensureFileNameResolver(); + fileNameResolver._invalidateResolverInstance(); +} \ No newline at end of file diff --git a/tns-core-modules/application/application.android.ts b/tns-core-modules/application/application.android.ts index 91d57b0dd..a39db2978 100644 --- a/tns-core-modules/application/application.android.ts +++ b/tns-core-modules/application/application.android.ts @@ -142,6 +142,7 @@ function initComponentCallbacks() { break; } + appModule._onOrientationChanged(); typedExports.notify({ eventName: typedExports.orientationChangedEvent, android: androidApp.nativeApp, diff --git a/tns-core-modules/application/application.ios.ts b/tns-core-modules/application/application.ios.ts index b895875c2..042de4333 100644 --- a/tns-core-modules/application/application.ios.ts +++ b/tns-core-modules/application/application.ios.ts @@ -201,6 +201,7 @@ class IOSApplication implements definition.iOSApplication { break; } + common._onOrientationChanged(); typedExports.notify({ eventName: typedExports.orientationChangedEvent, ios: this, diff --git a/tns-core-modules/file-system/file-name-resolver.d.ts b/tns-core-modules/file-system/file-name-resolver.d.ts index 5b18d44e1..96fd30827 100644 --- a/tns-core-modules/file-system/file-name-resolver.d.ts +++ b/tns-core-modules/file-system/file-name-resolver.d.ts @@ -17,9 +17,4 @@ declare module "file-system/file-name-resolver" { export function resolveFileName(path: string, ext: string): string; export function clearCache(): void; - - //@private - export function findFileMatch(path: string, ext: string, candidates: Array, context: PlatformContext): string - //@endprivate - } \ No newline at end of file diff --git a/tns-core-modules/file-system/file-name-resolver.ts b/tns-core-modules/file-system/file-name-resolver.ts index cb5f5693c..755de8493 100644 --- a/tns-core-modules/file-system/file-name-resolver.ts +++ b/tns-core-modules/file-system/file-name-resolver.ts @@ -1,9 +1,13 @@ -import * as definition from "file-system/file-name-resolver"; +import * as definition from "file-system/file-name-resolver"; import * as fs from "file-system"; import * as types from "utils/types"; import * as trace from "trace"; import * as platform from "platform"; -import * as appModule from "application"; + +declare module "file-system/file-name-resolver" { + export function _findFileMatch(path: string, ext: string, candidates: Array, context: PlatformContext): string + export function _invalidateResolverInstance(): void; +} var MIN_WH: string = "minWH"; var MIN_W: string = "minW"; @@ -75,7 +79,7 @@ var minHeightQualifier: QualifierSpec = { } } -var paltformQualifier: QualifierSpec = { +var platformQualifier: QualifierSpec = { isMatch: function (value: string): boolean { return value === "android" || value === "ios"; @@ -104,7 +108,7 @@ var supportedQualifiers: Array = [ minWidthQualifier, minHeightQualifier, orientationQualifier, - paltformQualifier + platformQualifier ]; export class FileNameResolver implements definition.FileNameResolver { @@ -136,7 +140,7 @@ export class FileNameResolver implements definition.FileNameResolver { ext = "." + ext; var candidates = this.getFileCandidatesFromFolder(path, ext); - result = findFileMatch(path, ext, candidates, this._context); + result = _findFileMatch(path, ext, candidates, this._context); if (trace.enabled) { trace.write("Resolved file name for \"" + path + ext + "\" result: " + (result ? result : "no match found"), trace.categories.Navigation); @@ -171,7 +175,7 @@ export class FileNameResolver implements definition.FileNameResolver { } } -export function findFileMatch(path: string, ext: string, candidates: Array, context: definition.PlatformContext): string { +export function _findFileMatch(path: string, ext: string, candidates: Array, context: definition.PlatformContext): string { var bestValue = -1 var result: string = null; @@ -226,18 +230,9 @@ function checkQualifier(value: string, context: definition.PlatformContext) { return -1; } -var appEventAttached: boolean = false; var resolverInstance: FileNameResolver; export function resolveFileName(path: string, ext: string): string { - if (!appEventAttached) { - var app: typeof appModule = require("application"); - app.on(app.orientationChangedEvent, (data) => { - resolverInstance = undefined; - }); - appEventAttached = true; - } - if (!resolverInstance) { resolverInstance = new FileNameResolver({ width: platform.screen.mainScreen.widthDIPs, @@ -254,4 +249,8 @@ export function clearCache(): void { if (resolverInstance) { resolverInstance.clearCache(); } +} + +export function _invalidateResolverInstance(): void { + resolverInstance = undefined; } \ No newline at end of file diff --git a/tns-core-modules/platform/platform.android.ts b/tns-core-modules/platform/platform.android.ts index 919a4e284..e79ef3e6b 100644 --- a/tns-core-modules/platform/platform.android.ts +++ b/tns-core-modules/platform/platform.android.ts @@ -2,6 +2,12 @@ import * as definition from "platform"; import * as utils from "utils/utils"; +declare module "platform" { + export interface ScreenMetrics { + _invalidate(): void; + } +} + const MIN_TABLET_PIXELS = 600; export module platformNames { @@ -100,6 +106,11 @@ class Device implements definition.Device { class MainScreen implements definition.ScreenMetrics { private _metrics: android.util.DisplayMetrics; + + public _invalidate(): void { + this._metrics = null; + } + private get metrics(): android.util.DisplayMetrics { if (!this._metrics) { this._metrics = new android.util.DisplayMetrics(); diff --git a/tns-core-modules/platform/platform.ios.ts b/tns-core-modules/platform/platform.ios.ts index 15896110b..31e6c9a6b 100644 --- a/tns-core-modules/platform/platform.ios.ts +++ b/tns-core-modules/platform/platform.ios.ts @@ -96,6 +96,11 @@ class Device implements definition.Device { class MainScreen implements definition.ScreenMetrics { private _screen: UIScreen; + + _invalidate(){ + // + } + private get screen(): UIScreen { if (!this._screen) { this._screen = utils.ios.getter(UIScreen, UIScreen.mainScreen); diff --git a/tns-core-modules/ui/frame/frame-common.ts b/tns-core-modules/ui/frame/frame-common.ts index eb9ab9a31..8f960bc84 100644 --- a/tns-core-modules/ui/frame/frame-common.ts +++ b/tns-core-modules/ui/frame/frame-common.ts @@ -126,6 +126,11 @@ function pageFromBuilder(moduleNamePath: string, moduleExports: any): Page { } } + // Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311 + // if (page && fileName === `${moduleNamePath}.port.xml` || fileName === `${moduleNamePath}.land.xml`){ + // page["isBiOrientational"] = true; + // } + return page; } @@ -200,6 +205,27 @@ export class FrameBase extends CustomLayoutView implements FrameDefinition { } } + // Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311 + // private _subscribedToOrientationChangedEvent = false; + // private _onOrientationChanged(){ + // if (!this._currentEntry){ + // return; + // } + + // let currentPage = this._currentEntry.resolvedPage; + // let currentNavigationEntry = this._currentEntry.entry; + // if (currentPage["isBiOrientational"] && currentNavigationEntry.moduleName) { + // if (this.canGoBack()){ + // this.goBack(); + // } + // else { + // currentNavigationEntry.backstackVisible = false; + // } + // // Re-navigate to the same page so the other (.port or .land) xml is loaded. + // this.navigate(currentNavigationEntry); + // } + // } + public navigate(param: any) { if (traceEnabled) { traceWrite(`NAVIGATE`, traceCategories.Navigation); @@ -208,6 +234,16 @@ export class FrameBase extends CustomLayoutView implements FrameDefinition { let entry = buildEntryFromArgs(param); let page = resolvePageFromEntry(entry); + // Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311 + // if (page["isBiOrientational"] && entry.moduleName && !this._subscribedToOrientationChangedEvent){ + // this._subscribedToOrientationChangedEvent = true; + // let app = require("application"); + // if (trace.enabled) { + // trace.write(`${this} subscribed to orientationChangedEvent.`, trace.categories.Navigation); + // } + // app.on(app.orientationChangedEvent, (data) => this._onOrientationChanged()); + // } + this._pushInFrameStack(); let backstackEntry: BackstackEntry = {