-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
frame.ios.ts
370 lines (297 loc) · 13.6 KB
/
frame.ios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import frameCommon = require("./frame-common");
import definition = require("ui/frame");
import trace = require("trace");
import pages = require("ui/page");
import enums = require("ui/enums");
import utils = require("utils/utils");
import view = require("ui/core/view");
import types = require("utils/types");
import uiUtils = require("ui/utils");
global.moduleMerge(frameCommon, exports);
var ENTRY = "_entry";
var NAV_DEPTH = "_navDepth";
var navDepth = -1;
export class Frame extends frameCommon.Frame {
private _ios: iOSFrame;
private _paramToNavigate: any;
public _shouldSkipNativePop: boolean = false;
public _navigateToEntry: definition.BackstackEntry;
public _widthMeasureSpec: number;
public _heightMeasureSpec: number;
public _layoutWidth: number;
public _layoutheight: number;
constructor() {
super();
this._ios = new iOSFrame(this);
}
public onLoaded() {
super.onLoaded();
if (this._paramToNavigate) {
this.navigate(this._paramToNavigate);
this._paramToNavigate = undefined;
}
}
public navigate(param: any) {
if (this.isLoaded) {
super.navigate(param);
}
else {
this._paramToNavigate = param;
}
}
public _navigateCore(backstackEntry: definition.BackstackEntry) {
var viewController: UIViewController = backstackEntry.resolvedPage.ios;
if (!viewController) {
throw new Error("Required page does have an viewController created.");
}
navDepth++;
var animated = false;
if (this.currentPage) {
animated = this._getIsAnimatedNavigation(backstackEntry.entry);
}
backstackEntry[NAV_DEPTH] = navDepth;
viewController[ENTRY] = backstackEntry;
this._navigateToEntry = backstackEntry;
this._updateActionBar(backstackEntry.resolvedPage);
// First navigation.
if (!this._currentEntry) {
this._ios.controller.pushViewControllerAnimated(viewController, animated);
trace.write("Frame<" + this._domId + ">.pushViewControllerAnimated(newController) depth = " + navDepth, trace.categories.Navigation);
return;
}
// We should clear the entire history.
if (backstackEntry.entry.clearHistory) {
viewController.navigationItem.hidesBackButton = true;
var newControllers = NSMutableArray.alloc().initWithCapacity(1);
newControllers.addObject(viewController);
this._ios.controller.setViewControllersAnimated(newControllers, animated);
trace.write("Frame<" + this._domId + ">.setViewControllersAnimated([newController]) depth = " + navDepth, trace.categories.Navigation);
return;
}
// We should hide the current entry from the back stack.
if (!this._isEntryBackstackVisible(this._currentEntry)) {
var newControllers = NSMutableArray.alloc().initWithArray(this._ios.controller.viewControllers);
if (newControllers.count === 0) {
throw new Error("Wrong controllers count.");
}
// the code below fixes a phantom animation that appears on the Back button in this case
// TODO: investigate why the animation happens at first place before working around it
viewController.navigationItem.hidesBackButton = this.backStack.length === 0;
// swap the top entry with the new one
newControllers.removeLastObject();
newControllers.addObject(viewController);
// replace the controllers instead of pushing directly
this._ios.controller.setViewControllersAnimated(newControllers, animated);
trace.write("Frame<" + this._domId + ">.setViewControllersAnimated([originalControllers - lastController + newController]) depth = " + navDepth, trace.categories.Navigation);
return;
}
// General case.
this._ios.controller.pushViewControllerAnimated(viewController, animated);
trace.write("Frame<" + this._domId + ">.pushViewControllerAnimated(newController) depth = " + navDepth, trace.categories.Navigation);
}
public _goBackCore(backstackEntry: definition.BackstackEntry) {
navDepth = backstackEntry[NAV_DEPTH];
trace.write("Frame<" + this._domId + ">.popViewControllerAnimated depth = " + navDepth, trace.categories.Navigation);
if (!this._shouldSkipNativePop) {
var controller = backstackEntry.resolvedPage.ios;
var animated = this._getIsAnimatedNavigation(backstackEntry.entry);
this._navigateToEntry = backstackEntry;
this._updateActionBar(backstackEntry.resolvedPage);
this._ios.controller.popToViewControllerAnimated(controller, animated);
}
}
public _updateActionBar(page?: pages.Page): void {
super._updateActionBar(page);
var page = page || this.currentPage;
var newValue = this._getNavBarVisible(page);
this._ios.showNavigationBar = newValue;
}
public _getNavBarVisible(page: pages.Page): boolean {
switch (this._ios.navBarVisibility) {
case enums.NavigationBarVisibility.always:
return true;
case enums.NavigationBarVisibility.never:
return false;
case enums.NavigationBarVisibility.auto:
let newValue: boolean;
if (page && types.isDefined(page.actionBarHidden)) {
newValue = !page.actionBarHidden;
}
else {
newValue = this.backStack.length > 0 || (page && page.actionBar && !page.actionBar._isEmpty());
}
newValue = !!newValue; // Make sure it is boolean
return newValue;
}
}
public get ios(): definition.iOSFrame {
return this._ios;
}
get _nativeView(): any {
return this._ios.controller.view;
}
public static get defaultAnimatedNavigation(): boolean {
return frameCommon.Frame.defaultAnimatedNavigation;
}
public static set defaultAnimatedNavigation(value: boolean) {
frameCommon.Frame.defaultAnimatedNavigation = value;
}
public requestLayout(): void {
super.requestLayout();
// Invalidate our Window so that layout is triggered again.
var window = this._nativeView.window;
if (window) {
window.setNeedsLayout();
}
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
let width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
let widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
let height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
let heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
this._widthMeasureSpec = widthMeasureSpec;
this._heightMeasureSpec = heightMeasureSpec;
let result = this.measurePage(this.currentPage);
let widthAndState = view.View.resolveSizeAndState(result.measuredWidth, width, widthMode, 0);
let heightAndState = view.View.resolveSizeAndState(result.measuredHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
public measurePage(page: pages.Page): { measuredWidth: number; measuredHeight: number } {
// If background does not span under statusbar - reduce available height.
let heightSpec: number = this._heightMeasureSpec;
if (page && !page.backgroundSpanUnderStatusBar) {
let height = utils.layout.getMeasureSpecSize(this._heightMeasureSpec);
let heightMode = utils.layout.getMeasureSpecMode(this._heightMeasureSpec);
let statusBarHeight = uiUtils.ios.getStatusBarHeight();
heightSpec = utils.layout.makeMeasureSpec(height - statusBarHeight, heightMode);
}
return view.View.measureChild(this, page, this._widthMeasureSpec, heightSpec);
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
this._layoutWidth = right - left;
this._layoutheight = bottom - top;
this.layoutPage(this.currentPage);
}
public layoutPage(page: pages.Page): void {
// If background does not span under statusbar - reduce available height and adjust top offset.
let statusBarHeight = (page && !page.backgroundSpanUnderStatusBar) ? uiUtils.ios.getStatusBarHeight() : 0;
view.View.layoutChild(this, page, 0, statusBarHeight, this._layoutWidth, this._layoutheight);
}
public get navigationBarHeight(): number {
var navigationBar = this._ios.controller.navigationBar;
return (navigationBar && !this._ios.controller.navigationBarHidden) ? navigationBar.frame.size.height : 0;
}
}
class UINavigationControllerImpl extends UINavigationController implements UINavigationControllerDelegate {
public static ObjCProtocols = [UINavigationControllerDelegate];
private _owner: Frame;
public static initWithOwner(owner: Frame): UINavigationControllerImpl {
var controller = <UINavigationControllerImpl>UINavigationControllerImpl.new();
controller._owner = owner;
return controller;
}
get owner(): Frame {
return this._owner;
}
public viewDidLoad(): void {
this._owner.onLoaded();
}
public viewDidLayoutSubviews(): void {
trace.write(this._owner + " viewDidLayoutSubviews, isLoaded = " + this._owner.isLoaded, trace.categories.ViewHierarchy);
this._owner._updateLayout();
}
public navigationControllerWillShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
// In this method we need to layout the new page otherwise page will be shown empty and update after that which is bad UX.
var frame = this._owner;
var newEntry: definition.BackstackEntry = viewController[ENTRY];
var newPage = newEntry.resolvedPage;
if (!newPage.parent) {
if (!frame._currentEntry) {
// First navigation
frame._currentEntry = newEntry;
}
else {
frame._navigateToEntry = newEntry;
}
frame._addView(newPage);
frame.measurePage(newPage);
frame.layoutPage(newPage)
}
else if (newPage.parent !== frame) {
throw new Error("Page is already shown on another frame.");
}
newPage.actionBar.update();
}
public navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
var frame: Frame = this._owner;
var backStack = frame.backStack;
var currentEntry = backStack.length > 0 ? backStack[backStack.length - 1] : null;
var newEntry: definition.BackstackEntry = viewController[ENTRY];
// This code check if navigation happened through UI (e.g. back button or swipe gesture).
// When calling goBack on frame isBack will be false.
var isBack: boolean = currentEntry && newEntry === currentEntry;
if (isBack) {
try {
frame._shouldSkipNativePop = true;
frame.goBack();
}
finally {
frame._shouldSkipNativePop = false;
}
}
var page = frame.currentPage;
if (page && !navigationController.viewControllers.containsObject(page.ios)) {
frame._removeView(page);
}
frame._navigateToEntry = null;
frame._currentEntry = newEntry;
var newPage = newEntry.resolvedPage;
// In iOS we intentionally delay the raising of the 'loaded' event so both platforms behave identically.
// The loaded event must be raised AFTER the page is part of the windows hierarchy and
// frame.topmost().currentPage is set to the page instance.
// https://github.com/NativeScript/NativeScript/issues/779
(<any>newPage)._delayLoadedEvent = false;
newPage._emit(view.View.loadedEvent);
frame._updateActionBar(newPage);
// notify the page
newPage.onNavigatedTo();
frame._processNavigationQueue(newPage);
}
public supportedInterfaceOrientation(): number {
return UIInterfaceOrientationMask.UIInterfaceOrientationMaskAll;
}
}
/* tslint:disable */
class iOSFrame implements definition.iOSFrame {
/* tslint:enable */
private _controller: UINavigationControllerImpl;
private _showNavigationBar: boolean;
private _navBarVisibility: string;
constructor(owner: Frame) {
this._controller = UINavigationControllerImpl.initWithOwner(owner);
this._controller.delegate = this._controller;
this._controller.automaticallyAdjustsScrollViewInsets = false;
//this.showNavigationBar = false;
this._navBarVisibility = enums.NavigationBarVisibility.auto;
}
public get controller() {
return this._controller;
}
public get showNavigationBar(): boolean {
return this._showNavigationBar;
}
public set showNavigationBar(value: boolean) {
var change = this._showNavigationBar !== value;
this._showNavigationBar = value;
this._controller.navigationBarHidden = !value;
if (change) {
this._controller.owner.requestLayout();
}
}
public get navBarVisibility(): string {
return this._navBarVisibility;
}
public set navBarVisibility(value: string) {
this._navBarVisibility = value;
}
}