From 77c77f4f8c9eb76a13e0a2d46a8c1e9896794fa9 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 29 Jul 2020 13:51:42 +0200 Subject: [PATCH] MOBILE-3511 splitview: Fix some events not triggered in right view --- .../mod/forum/pages/discussion/discussion.ts | 6 +++ .../pages/new-discussion/new-discussion.ts | 6 +++ src/components/split-view/split-view.ts | 37 ++++++++++++++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/addon/mod/forum/pages/discussion/discussion.ts b/src/addon/mod/forum/pages/discussion/discussion.ts index adf2ebbe2..78ad9c8b6 100644 --- a/src/addon/mod/forum/pages/discussion/discussion.ts +++ b/src/addon/mod/forum/pages/discussion/discussion.ts @@ -177,6 +177,11 @@ export class AddonModForumDiscussionPage implements OnDestroy { * User entered the page that contains the component. */ ionViewDidEnter(): void { + if (this.syncObserver) { + // Already setup. + return; + } + // Refresh data if this discussion is synchronized automatically. this.syncObserver = this.eventsProvider.on(AddonModForumSyncProvider.AUTO_SYNCED, (data) => { if (data.forumId == this.forumId && this.discussionId == data.discussionId @@ -688,6 +693,7 @@ export class AddonModForumDiscussionPage implements OnDestroy { this.ratingOfflineObserver && this.ratingOfflineObserver.off(); this.ratingSyncObserver && this.ratingSyncObserver.off(); this.changeDiscObserver && this.changeDiscObserver.off(); + delete this.syncObserver; } /** diff --git a/src/addon/mod/forum/pages/new-discussion/new-discussion.ts b/src/addon/mod/forum/pages/new-discussion/new-discussion.ts index 17ff5888e..b4aea7323 100644 --- a/src/addon/mod/forum/pages/new-discussion/new-discussion.ts +++ b/src/addon/mod/forum/pages/new-discussion/new-discussion.ts @@ -112,6 +112,11 @@ export class AddonModForumNewDiscussionPage implements OnDestroy { * User entered the page that contains the component. */ ionViewDidEnter(): void { + if (this.syncObserver) { + // Already setup. + return; + } + // Refresh data if this discussion is synchronized automatically. this.syncObserver = this.eventsProvider.on(AddonModForumSyncProvider.AUTO_SYNCED, (data) => { if (data.forumId == this.forumId && data.userId == this.sitesProvider.getCurrentSiteUserId()) { @@ -549,6 +554,7 @@ export class AddonModForumNewDiscussionPage implements OnDestroy { */ ionViewWillLeave(): void { this.syncObserver && this.syncObserver.off(); + delete this.syncObserver; } /** diff --git a/src/components/split-view/split-view.ts b/src/components/split-view/split-view.ts index 11f6e4dbd..58d605b64 100644 --- a/src/components/split-view/split-view.ts +++ b/src/components/split-view/split-view.ts @@ -49,18 +49,20 @@ export class CoreSplitViewComponent implements OnInit, OnDestroy { @ViewChild('menu') menu: Menu; @Input() when?: string | boolean = 'md'; + protected VIEW_EVENTS = ['willEnter', 'didEnter', 'willLeave', 'willLeave']; + protected isEnabled; protected masterPageName = ''; protected masterPageIndex = 0; protected loadDetailPage: any = false; protected element: HTMLElement; // Current element. - protected detailsDidEnterSubscription: Subscription; protected masterCanLeaveOverridden = false; protected originalMasterCanLeave: Function; protected ignoreSplitChanged = false; protected audioCaptureSubscription: Subscription; protected languageChangedSubscription: Subscription; protected pushOngoing: boolean; + protected viewEventsSubscriptions: Subscription[] = []; // Empty placeholder for the 'detail' page. detailPage: any = null; @@ -92,7 +94,7 @@ export class CoreSplitViewComponent implements OnInit, OnDestroy { this.masterPageIndex = this.masterNav.indexOf(this.masterNav.getActive()); this.emptyDetails(); - this.handleCanLeave(); + this.handleViewEvents(); } /** @@ -123,7 +125,7 @@ export class CoreSplitViewComponent implements OnInit, OnDestroy { */ handleCanLeave(): void { // Listen for the didEnter event on the details nav to detect everytime a page is loaded. - this.detailsDidEnterSubscription = this.detailNav.viewDidEnter.subscribe((detailsViewController: ViewController) => { + this.viewEventsSubscriptions.push(this.detailNav.viewDidEnter.subscribe((detailsViewController: ViewController) => { if (!this.isOn()) { return; } @@ -166,7 +168,30 @@ export class CoreSplitViewComponent implements OnInit, OnDestroy { }); }; } - }); + })); + } + + /** + * Handle Ionic Views lifecycle events in the details page. + */ + handleViewEvents(): void { + // Handle affected view events except ionViewCanLeave, propagating them to the details view. + const masterActiveView = this.masterNav.getActive(); + + for (const i in this.VIEW_EVENTS) { + const viewEvent = this.VIEW_EVENTS[i]; + + this.viewEventsSubscriptions.push(masterActiveView[viewEvent].subscribe(() => { + if (!this.isOn()) { + return; + } + + const activeView = this.detailNav.getActive(); + activeView && activeView[`_${viewEvent}`](); + })); + } + + this.handleCanLeave(); } /** @@ -274,8 +299,10 @@ export class CoreSplitViewComponent implements OnInit, OnDestroy { * Component being destroyed. */ ngOnDestroy(): void { - this.detailsDidEnterSubscription && this.detailsDidEnterSubscription.unsubscribe(); this.audioCaptureSubscription.unsubscribe(); this.languageChangedSubscription.unsubscribe(); + for (const i in this.viewEventsSubscriptions) { + this.viewEventsSubscriptions[i].unsubscribe(); + } } }