Skip to content

Commit 3e461cd

Browse files
committed
Fix: Android platform.screen.mainScreen props are not invalidated after orientation change
Resolves NativeScript#3270
1 parent b77903f commit 3e461cd

File tree

9 files changed

+95
-23
lines changed

9 files changed

+95
-23
lines changed

tests/app/file-name-resolver-tests/file-name-resolver-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export function test_findFileMatch_with_multiple_matches_loads_by_priority() {
208208
}
209209

210210
function testTemplate(candidates: Array<string>, context: resolver.PlatformContext, expected: string) {
211-
var result = resolver.findFileMatch("test", ".xml", candidates, context);
211+
var result = resolver._findFileMatch("test", ".xml", candidates, context);
212212
TKUnit.assertEqual(result, expected, "File path");
213213
}
214214

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as fileSystemModule from "file-system";
77
import * as styleScopeModule from "ui/styling/style-scope";
88
import * as fileResolverModule from "file-system/file-name-resolver";
99
import * as builderModule from "ui/builder";
10+
import * as platformModule from "platform";
1011
import "../bundle-entry-points";
1112

1213
var builder: typeof builderModule;
@@ -16,6 +17,20 @@ function ensureBuilder() {
1617
}
1718
}
1819

20+
var platform: typeof platformModule;
21+
function ensurePlatform() {
22+
if (!platform) {
23+
platform = require("platform");
24+
}
25+
}
26+
27+
var fileNameResolver: typeof fileResolverModule;
28+
function ensureFileNameResolver() {
29+
if (!fileNameResolver) {
30+
fileNameResolver = require("file-system/file-name-resolver");
31+
}
32+
}
33+
1934
var styleScope: typeof styleScopeModule = undefined;
2035

2136
var events = new observable.Observable();
@@ -112,10 +127,10 @@ export function __onLiveSync() {
112127
}
113128

114129
try {
115-
var fileResolver: typeof fileResolverModule = require("file-system/file-name-resolver");
130+
ensureFileNameResolver();
116131

117132
// Clear file resolver cache to respect newly added files.
118-
fileResolver.clearCache();
133+
fileNameResolver.clearCache();
119134

120135
// Reload app.css in case it was changed.
121136
loadCss();
@@ -134,4 +149,12 @@ export function __onLiveSyncCore() {
134149
// Reload current page.
135150
frame.reloadPage();
136151
}
137-
global.__onLiveSyncCore = __onLiveSyncCore;
152+
global.__onLiveSyncCore = __onLiveSyncCore;
153+
154+
export function _onOrientationChanged(){
155+
ensurePlatform();
156+
platform.screen.mainScreen._invalidate();
157+
158+
ensureFileNameResolver();
159+
fileNameResolver._invalidateResolverInstance();
160+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function initComponentCallbacks() {
148148
break;
149149
}
150150

151+
appModule._onOrientationChanged();
151152
typedExports.notify(<definition.OrientationChangedEventData>{
152153
eventName: typedExports.orientationChangedEvent,
153154
android: androidApp.nativeApp,

tns-core-modules/application/application.ios.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class IOSApplication implements definition.iOSApplication {
205205
break;
206206
}
207207

208+
common._onOrientationChanged();
208209
typedExports.notify(<definition.OrientationChangedEventData>{
209210
eventName: typedExports.orientationChangedEvent,
210211
ios: this,

tns-core-modules/file-system/file-name-resolver.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,4 @@ declare module "file-system/file-name-resolver" {
1717

1818
export function resolveFileName(path: string, ext: string): string;
1919
export function clearCache(): void;
20-
21-
//@private
22-
export function findFileMatch(path: string, ext: string, candidates: Array<string>, context: PlatformContext): string
23-
//@endprivate
24-
2520
}

tns-core-modules/file-system/file-name-resolver.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import fs = require("file-system");
33
import types = require("utils/types");
44
import trace = require("trace");
55
import platform = require("platform");
6-
import * as appModule from "application";
6+
7+
declare module "file-system/file-name-resolver" {
8+
export function _findFileMatch(path: string, ext: string, candidates: Array<string>, context: PlatformContext): string
9+
export function _invalidateResolverInstance(): void;
10+
}
711

812
var MIN_WH: string = "minWH";
913
var MIN_W: string = "minW";
@@ -75,7 +79,7 @@ var minHeightQualifier: QualifierSpec = {
7579
}
7680
}
7781

78-
var paltformQualifier: QualifierSpec = {
82+
var platformQualifier: QualifierSpec = {
7983
isMatch: function (value: string): boolean {
8084
return value === "android" ||
8185
value === "ios";
@@ -104,7 +108,7 @@ var supportedQualifiers: Array<QualifierSpec> = [
104108
minWidthQualifier,
105109
minHeightQualifier,
106110
orientationQualifier,
107-
paltformQualifier
111+
platformQualifier
108112
];
109113

110114
export class FileNameResolver implements definition.FileNameResolver {
@@ -136,7 +140,7 @@ export class FileNameResolver implements definition.FileNameResolver {
136140
ext = "." + ext;
137141

138142
var candidates = this.getFileCandidatesFromFolder(path, ext);
139-
result = findFileMatch(path, ext, candidates, this._context);
143+
result = _findFileMatch(path, ext, candidates, this._context);
140144

141145
if (trace.enabled) {
142146
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 {
171175
}
172176
}
173177

174-
export function findFileMatch(path: string, ext: string, candidates: Array<string>, context: definition.PlatformContext): string {
178+
export function _findFileMatch(path: string, ext: string, candidates: Array<string>, context: definition.PlatformContext): string {
175179
var bestValue = -1
176180
var result: string = null;
177181

@@ -226,18 +230,9 @@ function checkQualifier(value: string, context: definition.PlatformContext) {
226230
return -1;
227231
}
228232

229-
var appEventAttached: boolean = false;
230233
var resolverInstance: FileNameResolver;
231234

232235
export function resolveFileName(path: string, ext: string): string {
233-
if (!appEventAttached) {
234-
var app: typeof appModule = require("application");
235-
app.on(app.orientationChangedEvent, (data) => {
236-
resolverInstance = undefined;
237-
});
238-
appEventAttached = true;
239-
}
240-
241236
if (!resolverInstance) {
242237
resolverInstance = new FileNameResolver({
243238
width: platform.screen.mainScreen.widthDIPs,
@@ -254,4 +249,8 @@ export function clearCache(): void {
254249
if (resolverInstance) {
255250
resolverInstance.clearCache();
256251
}
252+
}
253+
254+
export function _invalidateResolverInstance(): void {
255+
resolverInstance = undefined;
257256
}

tns-core-modules/platform/platform.android.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import definition = require("platform");
33
import utils = require("utils/utils");
44
import * as enumsModule from "ui/enums";
55

6+
declare module "platform" {
7+
export interface ScreenMetrics {
8+
_invalidate(): void;
9+
}
10+
}
11+
612
const MIN_TABLET_PIXELS = 600;
713

814
export module platformNames {
@@ -103,6 +109,11 @@ class Device implements definition.Device {
103109

104110
class MainScreen implements definition.ScreenMetrics {
105111
private _metrics: android.util.DisplayMetrics;
112+
113+
public _invalidate(): void {
114+
this._metrics = null;
115+
}
116+
106117
private get metrics(): android.util.DisplayMetrics {
107118
if (!this._metrics) {
108119
this._metrics = new android.util.DisplayMetrics();

tns-core-modules/platform/platform.ios.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class Device implements definition.Device {
9898

9999
class MainScreen implements definition.ScreenMetrics {
100100
private _screen: UIScreen;
101+
102+
_invalidate(){
103+
//
104+
}
105+
101106
private get screen(): UIScreen {
102107
if (!this._screen) {
103108
this._screen = utils.ios.getter(UIScreen, UIScreen.mainScreen);

tns-core-modules/ui/frame/frame-common.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function pageFromBuilder(moduleNamePath: string, moduleExports: any): Page {
122122

123123
// Possible XML file path.
124124
var fileName = resolveFileName(moduleNamePath, "xml");
125+
125126
if (fileName) {
126127
if (trace.enabled) {
127128
trace.write("Loading XML file: " + fileName, trace.categories.Navigation);
@@ -136,6 +137,11 @@ function pageFromBuilder(moduleNamePath: string, moduleExports: any): Page {
136137
}
137138
}
138139

140+
// Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311
141+
// if (page && fileName === `${moduleNamePath}.port.xml` || fileName === `${moduleNamePath}.land.xml`){
142+
// page["isBiOrientational"] = true;
143+
// }
144+
139145
return page;
140146
}
141147

@@ -210,6 +216,27 @@ export class Frame extends CustomLayoutView implements definition.Frame {
210216
}
211217
}
212218

219+
// Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311
220+
// private _subscribedToOrientationChangedEvent = false;
221+
// private _onOrientationChanged(){
222+
// if (!this._currentEntry){
223+
// return;
224+
// }
225+
226+
// let currentPage = this._currentEntry.resolvedPage;
227+
// let currentNavigationEntry = this._currentEntry.entry;
228+
// if (currentPage["isBiOrientational"] && currentNavigationEntry.moduleName) {
229+
// if (this.canGoBack()){
230+
// this.goBack();
231+
// }
232+
// else {
233+
// currentNavigationEntry.backstackVisible = false;
234+
// }
235+
// // Re-navigate to the same page so the other (.port or .land) xml is loaded.
236+
// this.navigate(currentNavigationEntry);
237+
// }
238+
// }
239+
213240
public navigate(param: any) {
214241
if (trace.enabled) {
215242
trace.write(`NAVIGATE`, trace.categories.Navigation);
@@ -218,6 +245,16 @@ export class Frame extends CustomLayoutView implements definition.Frame {
218245
var entry = buildEntryFromArgs(param);
219246
var page = resolvePageFromEntry(entry);
220247

248+
// Attempts to implement https://github.com/NativeScript/NativeScript/issues/1311
249+
// if (page["isBiOrientational"] && entry.moduleName && !this._subscribedToOrientationChangedEvent){
250+
// this._subscribedToOrientationChangedEvent = true;
251+
// let app = require("application");
252+
// if (trace.enabled) {
253+
// trace.write(`${this} subscribed to orientationChangedEvent.`, trace.categories.Navigation);
254+
// }
255+
// app.on(app.orientationChangedEvent, (data) => this._onOrientationChanged());
256+
// }
257+
221258
this._pushInFrameStack();
222259

223260
var backstackEntry: definition.BackstackEntry = {

0 commit comments

Comments
 (0)