Skip to content

Commit 378e39b

Browse files
committed
feat: Better handling for navigation events
1 parent 3a3a36a commit 378e39b

File tree

7 files changed

+102
-114
lines changed

7 files changed

+102
-114
lines changed

packages/core/ui/frame/frame-common.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Page } from '../page';
44
import { View, CustomLayoutView, CSSType } from '../core/view';
55
import { Property } from '../core/properties';
66
import { Trace } from '../../trace';
7-
import { frameStack, topmost as frameStackTopmost, _pushInFrameStack, _popFromFrameStack, _removeFromFrameStack } from './frame-stack';
7+
import { frameStack, topmost as frameStackTopmost, _pushInFrameStack, _popFromFrameStack, _removeFromFrameStack, _isFrameStackEmpty } from './frame-stack';
88
import { viewMatchesModuleContext } from '../core/view/view-common';
99
import { getAncestor } from '../core/view-base';
1010
import { Builder } from '../builder';
@@ -193,7 +193,6 @@ export class FrameBase extends CustomLayoutView {
193193
const navigationContext: NavigationContext = {
194194
entry: backstackEntry,
195195
isBackNavigation: true,
196-
isUserInitiated: false,
197196
navigationType: NavigationType.back,
198197
};
199198

@@ -246,7 +245,6 @@ export class FrameBase extends CustomLayoutView {
246245
const navigationContext: NavigationContext = {
247246
entry: backstackEntry,
248247
isBackNavigation: false,
249-
isUserInitiated: false,
250248
navigationType: NavigationType.forward,
251249
};
252250

@@ -483,10 +481,6 @@ export class FrameBase extends CustomLayoutView {
483481
}
484482

485483
backstackEntry.resolvedPage.onNavigatingTo(backstackEntry.entry.context, isBack, backstackEntry.entry.bindingContext);
486-
this._notifyFrameNavigatingTo(backstackEntry, isBack);
487-
}
488-
489-
public _notifyFrameNavigatingTo(backstackEntry: BackstackEntry, isBack: boolean): void {
490484
this.notify<NavigationData>({
491485
eventName: FrameBase.navigatingToEvent,
492486
object: this,
@@ -548,6 +542,10 @@ export class FrameBase extends CustomLayoutView {
548542
}
549543
}
550544

545+
public _isFrameStackEmpty() {
546+
return _isFrameStackEmpty();
547+
}
548+
551549
public _pushInFrameStack() {
552550
_pushInFrameStack(this);
553551
}
@@ -767,7 +765,6 @@ export class FrameBase extends CustomLayoutView {
767765
const navigationContext: NavigationContext = {
768766
entry: newBackstackEntry,
769767
isBackNavigation: false,
770-
isUserInitiated: false,
771768
navigationType: NavigationType.replace,
772769
};
773770

packages/core/ui/frame/frame-interfaces.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export enum NavigationType {
88
back,
99
forward,
1010
replace,
11-
user,
1211
}
1312

1413
export interface TransitionState {
@@ -42,7 +41,6 @@ export interface NavigationContext {
4241
* @deprecated Use navigationType instead.
4342
*/
4443
isBackNavigation: boolean;
45-
isUserInitiated: boolean;
4644
navigationType: NavigationType;
4745
}
4846

packages/core/ui/frame/frame-stack.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export function topmost(): FrameBase {
1111
return undefined;
1212
}
1313

14+
export function _isFrameStackEmpty(): boolean {
15+
return frameStack.length === 0;
16+
}
17+
1418
export function _pushInFrameStack(frame: FrameBase): void {
1519
if (frame._isInFrameStack && frameStack[frameStack.length - 1] === frame) {
1620
return;

packages/core/ui/frame/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ export class Frame extends FrameBase {
258258
* @private
259259
*/
260260
_updateBackstack(entry: BackstackEntry, navigationType: NavigationType): void;
261+
/**
262+
* @private
263+
*/
264+
_isFrameStackEmpty(): boolean;
261265
/**
262266
* @private
263267
*/
@@ -409,7 +413,6 @@ export interface NavigationContext {
409413
* @deprecated Use navigationType instead.
410414
*/
411415
isBackNavigation: boolean;
412-
isUserInitiated: boolean;
413416
navigationType: NavigationType;
414417
}
415418

packages/core/ui/frame/index.ios.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { IOSHelper } from '../core/view/view-helper';
77
import { profile } from '../../profiling';
88
import { CORE_ANIMATION_DEFAULTS, ios as iOSUtils, layout } from '../../utils';
99
import { Trace } from '../../trace';
10-
import type { PageTransition } from '../transition/page-transition';
1110
import { SlideTransition } from '../transition/slide-transition';
1211
import { FadeTransition } from '../transition/fade-transition';
1312
import { SharedTransition } from '../transition/shared-transition';
@@ -22,11 +21,11 @@ const NAV_DEPTH = '_navDepth';
2221
const TRANSITION = '_transition';
2322
const NON_ANIMATED_TRANSITION = 'non-animated';
2423

25-
let navDepth = -1;
24+
let navDepth: number = -1;
25+
let navControllerDelegate: UINavigationControllerDelegate = null;
2626

2727
export class Frame extends FrameBase {
2828
viewController: UINavigationControllerImpl;
29-
_animatedDelegate: UINavigationControllerDelegate;
3029
public _ios: iOSFrame;
3130
iosNavigationBarClass: typeof NSObject;
3231
iosToolbarClass: typeof NSObject;
@@ -38,6 +37,11 @@ export class Frame extends FrameBase {
3837
}
3938

4039
createNativeView() {
40+
// Push frame back in frame stack since it was removed in disposeNativeView() method.
41+
if (this._currentEntry) {
42+
this._pushInFrameStack();
43+
}
44+
4145
return this.viewController.view;
4246
}
4347

@@ -61,7 +65,12 @@ export class Frame extends FrameBase {
6165

6266
this._removeFromFrameStack();
6367
this.viewController = null;
64-
this._animatedDelegate = null;
68+
69+
// This was the last frame so we can get rid of the controller delegate reference
70+
if (this._isFrameStackEmpty()) {
71+
navControllerDelegate = null;
72+
}
73+
6574
if (this._ios) {
6675
this._ios.controller = null;
6776
this._ios = null;
@@ -120,11 +129,12 @@ export class Frame extends FrameBase {
120129

121130
const nativeTransition = _getNativeTransition(navigationTransition, true);
122131
if (!nativeTransition && navigationTransition) {
123-
if (!this._animatedDelegate) {
124-
this._animatedDelegate = <UINavigationControllerDelegate>UINavigationControllerAnimatedDelegate.initWithOwner(new WeakRef(this));
132+
if (!navControllerDelegate) {
133+
navControllerDelegate = <UINavigationControllerDelegate>UINavigationControllerAnimatedDelegate.new();
125134
}
126-
this._ios.controller.delegate = this._animatedDelegate;
127-
viewController[DELEGATE] = this._animatedDelegate;
135+
136+
this._ios.controller.delegate = navControllerDelegate;
137+
viewController[DELEGATE] = navControllerDelegate;
128138
if (navigationTransition.instance) {
129139
this.transitionId = navigationTransition.instance.id;
130140
const transitionState = SharedTransition.getState(this.transitionId);
@@ -400,14 +410,6 @@ class TransitionDelegate extends NSObject {
400410
@NativeClass
401411
class UINavigationControllerAnimatedDelegate extends NSObject implements UINavigationControllerDelegate {
402412
public static ObjCProtocols = [UINavigationControllerDelegate];
403-
owner: WeakRef<Frame>;
404-
transition: PageTransition;
405-
406-
static initWithOwner(owner: WeakRef<Frame>) {
407-
const delegate = <UINavigationControllerAnimatedDelegate>UINavigationControllerAnimatedDelegate.new();
408-
delegate.owner = owner;
409-
return delegate;
410-
}
411413

412414
navigationControllerAnimationControllerForOperationFromViewControllerToViewController(navigationController: UINavigationController, operation: number, fromVC: UIViewController, toVC: UIViewController): UIViewControllerAnimatedTransitioning {
413415
let viewController: UIViewController;
@@ -432,29 +434,30 @@ class UINavigationControllerAnimatedDelegate extends NSObject implements UINavig
432434
if (Trace.isEnabled()) {
433435
Trace.write(`UINavigationControllerImpl.navigationControllerAnimationControllerForOperationFromViewControllerToViewController(${operation}, ${fromVC}, ${toVC}), transition: ${JSON.stringify(navigationTransition)}`, Trace.categories.NativeLifecycle);
434436
}
435-
this.transition = navigationTransition.instance;
436437

437-
if (!this.transition) {
438+
let transition = navigationTransition.instance;
439+
440+
if (!transition) {
438441
if (navigationTransition.name) {
439442
const curve = _getNativeCurve(navigationTransition);
440443
const name = navigationTransition.name.toLowerCase();
441444
if (name.indexOf('slide') === 0) {
442-
const direction = name.substring('slide'.length) || 'left'; //Extract the direction from the string
443-
this.transition = new SlideTransition(direction, navigationTransition.duration, curve);
445+
const direction = name.substring('slide'.length) || 'left'; // Extract the direction from the string
446+
transition = new SlideTransition(direction, navigationTransition.duration, curve);
444447
} else if (name === 'fade') {
445-
this.transition = new FadeTransition(navigationTransition.duration, curve);
448+
transition = new FadeTransition(navigationTransition.duration, curve);
446449
}
447450
}
448451
}
449452

450-
if (this.transition?.iosNavigatedController) {
451-
return this.transition.iosNavigatedController(navigationController, operation, fromVC, toVC);
453+
if (transition?.iosNavigatedController) {
454+
return transition.iosNavigatedController(navigationController, operation, fromVC, toVC);
452455
}
453456
return null;
454457
}
455458

456-
navigationControllerInteractionControllerForAnimationController(navigationController: UINavigationController, animationController: UIViewControllerAnimatedTransitioning): UIViewControllerInteractiveTransitioning {
457-
const owner = this.owner?.deref();
459+
navigationControllerInteractionControllerForAnimationController(navigationController: UINavigationControllerImpl, animationController: UIViewControllerAnimatedTransitioning): UIViewControllerInteractiveTransitioning {
460+
const owner = navigationController.owner;
458461
if (owner) {
459462
const state = SharedTransition.getState(owner.transitionId);
460463
if (state?.instance?.iosInteractionDismiss) {

packages/core/ui/gestures/index.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class UIGestureRecognizerDelegateImpl extends NSObject implements UIGestureRecog
3737
return false;
3838
}
3939
}
40-
const recognizerDelegateInstance: UIGestureRecognizerDelegateImpl = <UIGestureRecognizerDelegateImpl>UIGestureRecognizerDelegateImpl.new();
40+
const recognizerDelegateInstance = <UIGestureRecognizerDelegateImpl>UIGestureRecognizerDelegateImpl.new();
4141

4242
@NativeClass
4343
class UIGestureRecognizerImpl extends NSObject {

0 commit comments

Comments
 (0)