Skip to content

Commit 2c25086

Browse files
committed
MOBILE-4359 core: Create and use runAfterLoginNavigation
This function will be used to run code after the login navigation finishes, to avoid collisions when doing extra navigations
1 parent 3278d5a commit 2c25086

File tree

13 files changed

+117
-60
lines changed

13 files changed

+117
-60
lines changed

src/addons/blog/pages/index/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { AddonBlogOffline, AddonBlogOfflineEntry } from '@addons/blog/services/b
2929
import { AddonBlogSync } from '@addons/blog/services/blog-sync';
3030
import { Component, computed, OnDestroy, OnInit, signal } from '@angular/core';
3131
import { CoreComments } from '@features/comments/services/comments';
32-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
3332
import { CoreTag } from '@features/tag/services/tag';
3433
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
3534
import { CoreNavigator } from '@services/navigator';
@@ -202,8 +201,7 @@ export class AddonBlogIndexPage implements OnInit, OnDestroy {
202201
this.commentsEnabled = CoreComments.areCommentsEnabledInSite();
203202
this.tagsEnabled = CoreTag.areTagsAvailableInSite();
204203

205-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
206-
deepLinkManager.treatLink();
204+
CoreSites.loginNavigationFinished();
207205

208206
await this.fetchEntries(false, false, true);
209207
this.optionsAvailable = await AddonBlog.isEditingEnabled();

src/addons/calendar/pages/index/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { AddonCalendarCalendarComponent } from '../../components/calendar/calend
3030
import { AddonCalendarUpcomingEventsComponent } from '../../components/upcoming-events/upcoming-events';
3131
import { CoreNavigator } from '@services/navigator';
3232
import { CoreConstants } from '@/core/constants';
33-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
3433
import { CoreModals } from '@services/modals';
3534

3635
/**
@@ -178,8 +177,7 @@ export class AddonCalendarIndexPage implements OnInit, OnDestroy {
178177
}
179178
});
180179

181-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
182-
deepLinkManager.treatLink();
180+
CoreSites.loginNavigationFinished();
183181
}
184182

185183
/**

src/addons/messages/pages/discussions-35/discussions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { Subscription } from 'rxjs';
3030
import { Translate } from '@singletons';
3131
import { CoreNavigator } from '@services/navigator';
3232
import { CoreScreen } from '@services/screen';
33-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
3433
import { CorePlatform } from '@services/platform';
3534
import { CoreSplitViewComponent } from '@components/split-view/split-view';
3635
import { CoreKeyboard } from '@singletons/keyboard';
@@ -145,17 +144,15 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy {
145144
this.discussionUserId = CoreNavigator.getRouteNumberParam('userId', { params }) ?? this.discussionUserId;
146145
});
147146

148-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
149-
150147
await this.fetchData();
151148

152149
if (!this.discussionUserId && this.discussions.length > 0 && CoreScreen.isTablet && this.discussions[0].message) {
153150
// Take first and load it.
154151
await this.gotoDiscussion(this.discussions[0].message.user);
155152
}
156153

157-
// Treat deep link now that the conversation route has been loaded if needed.
158-
deepLinkManager.treatLink();
154+
// Mark login navigation finished now that the conversation route has been loaded if needed.
155+
CoreSites.loginNavigationFinished();
159156
}
160157

161158
/**

src/addons/messages/pages/group-conversations/group-conversations.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { ActivatedRoute, Params } from '@angular/router';
3838
import { CoreUtils } from '@services/utils/utils';
3939
import { CoreNavigator } from '@services/navigator';
4040
import { CoreScreen } from '@services/screen';
41-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
4241
import { CorePlatform } from '@services/platform';
4342
import { CoreSplitViewComponent } from '@components/split-view/split-view';
4443

@@ -312,8 +311,6 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
312311
}
313312
});
314313

315-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
316-
317314
await this.fetchData();
318315

319316
if (!this.selectedConversationId && !this.selectedUserId && CoreScreen.isTablet) {
@@ -326,8 +323,8 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
326323
}
327324
}
328325

329-
// Treat deep link now that the conversation route has been loaded if needed.
330-
deepLinkManager.treatLink();
326+
// Mark login navigation finished now that the conversation route has been loaded if needed.
327+
CoreSites.loginNavigationFinished();
331328
}
332329

333330
/**

src/addons/notifications/pages/list/list.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { CoreSplitViewComponent } from '@components/split-view/split-view';
2626
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
2727
import { CorePushNotificationsDelegate } from '@features/pushnotifications/services/push-delegate';
2828
import { CoreSites } from '@services/sites';
29-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
3029
import { CoreTimeUtils } from '@services/utils/time';
3130
import { AddonNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source';
3231
import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
@@ -130,8 +129,7 @@ export class AddonNotificationsListPage implements AfterViewInit, OnDestroy {
130129
this.loadMarkAllAsReadButton();
131130
});
132131

133-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
134-
deepLinkManager.treatLink();
132+
CoreSites.loginNavigationFinished();
135133
}
136134

137135
/**

src/core/classes/queue-runner.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export type CoreQueueRunnerItem<T = any> = {
3939
* Deferred with a promise resolved/rejected with the result of the function.
4040
*/
4141
deferred: CorePromisedValue<T>;
42+
43+
/**
44+
* Item's priority. Only used if usePriority=true.
45+
*/
46+
priority: number;
4247
};
4348

4449
/**
@@ -49,6 +54,12 @@ export type CoreQueueRunnerAddOptions = {
4954
* Whether to allow having multiple entries with same ID in the queue.
5055
*/
5156
allowRepeated?: boolean;
57+
58+
/**
59+
* If usePriority=true, the priority of the item. Higher priority means it will be executed first.
60+
* Please notice that the first item is always run immediately, so it's not affected by the priority.
61+
*/
62+
priority?: number;
5263
};
5364

5465
/**
@@ -60,7 +71,13 @@ export class CoreQueueRunner {
6071
protected orderedQueue: CoreQueueRunnerItem[] = [];
6172
protected numberRunning = 0;
6273

63-
constructor(protected maxParallel: number = 1) { }
74+
/**
75+
* Constructor.
76+
*
77+
* @param maxParallel Max number of parallel executions.
78+
* @param usePriority If true, the queue will be ordered by priority.
79+
*/
80+
constructor(protected maxParallel: number = 1, protected usePriority = false) { }
6481

6582
/**
6683
* Get unique ID.
@@ -147,10 +164,14 @@ export class CoreQueueRunner {
147164
id,
148165
fn,
149166
deferred: new CorePromisedValue<T>(),
167+
priority: options.priority ?? 0,
150168
};
151169

152170
this.queue[id] = item;
153171
this.orderedQueue.push(item);
172+
if (this.usePriority) {
173+
this.orderedQueue.sort((a, b) => b.priority - a.priority);
174+
}
154175

155176
// Process next item if we haven't reached the max yet.
156177
this.processNextItem();

src/core/features/courses/pages/my/my.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { CoreBlockComponent } from '@features/block/components/block/block';
2121
import { CoreBlockDelegate } from '@features/block/services/block-delegate';
2222
import { CoreCourseBlock } from '@features/course/services/course';
2323
import { CoreCoursesDashboard, CoreCoursesDashboardProvider } from '@features/courses/services/dashboard';
24-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
2524
import { CoreSites } from '@services/sites';
2625
import { CoreDomUtils } from '@services/utils/dom';
2726
import { CoreUtils } from '@services/utils/utils';
@@ -97,8 +96,7 @@ export class CoreCoursesMyPage implements OnInit, OnDestroy, AsyncDirective {
9796
async ngOnInit(): Promise<void> {
9897
this.downloadCoursesEnabled = !CoreCourses.isDownloadCoursesDisabledInSite();
9998

100-
const deepLinkManager = new CoreMainMenuDeepLinkManager();
101-
deepLinkManager.treatLink();
99+
CoreSites.loginNavigationFinished();
102100

103101
await this.loadSiteName();
104102

src/core/features/mainmenu/classes/deep-link-manager.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ export class CoreMainMenuDeepLinkManager {
5050
/**
5151
* Treat a deep link if there's any to treat.
5252
*/
53-
treatLink(): void {
54-
if (!this.pendingRedirect) {
53+
async treatLink(): Promise<void> {
54+
const pendingRedirect = this.pendingRedirect;
55+
if (!pendingRedirect) {
5556
return;
5657
}
5758

58-
if (this.pendingRedirect.redirectPath) {
59-
this.treatPath(this.pendingRedirect.redirectPath, this.pendingRedirect.redirectOptions);
60-
} else if (this.pendingRedirect.urlToOpen) {
61-
this.treatUrlToOpen(this.pendingRedirect.urlToOpen);
62-
}
63-
6459
delete this.pendingRedirect;
60+
if (pendingRedirect.redirectPath) {
61+
await this.treatPath(pendingRedirect.redirectPath, pendingRedirect.redirectOptions);
62+
} else if (pendingRedirect.urlToOpen) {
63+
await this.treatUrlToOpen(pendingRedirect.urlToOpen);
64+
}
6565
}
6666

6767
/**
@@ -70,18 +70,18 @@ export class CoreMainMenuDeepLinkManager {
7070
* @param path Path.
7171
* @param navOptions Navigation options.
7272
*/
73-
protected treatPath(path: string, navOptions: CoreNavigationOptions = {}): void {
73+
protected async treatPath(path: string, navOptions: CoreNavigationOptions = {}): Promise<void> {
7474
const params = navOptions.params;
7575
const coursePathMatches = path.match(/^course\/(\d+)\/?$/);
7676

7777
if (coursePathMatches) {
7878
if (!params?.course) {
79-
CoreCourseHelper.getAndOpenCourse(Number(coursePathMatches[1]), params);
79+
await CoreCourseHelper.getAndOpenCourse(Number(coursePathMatches[1]), params);
8080
} else {
81-
CoreCourse.openCourse(params.course, navOptions);
81+
await CoreCourse.openCourse(params.course, navOptions);
8282
}
8383
} else {
84-
CoreNavigator.navigateToSitePath(path, {
84+
await CoreNavigator.navigateToSitePath(path, {
8585
...navOptions,
8686
preferCurrentTab: false,
8787
});
@@ -96,7 +96,7 @@ export class CoreMainMenuDeepLinkManager {
9696
protected async treatUrlToOpen(url: string): Promise<void> {
9797
const action = await CoreContentLinksHelper.getFirstValidActionFor(url);
9898
if (action?.sites?.[0]) {
99-
action.action(action.sites[0]);
99+
await action.action(action.sites[0]);
100100
}
101101
}
102102

src/core/features/mainmenu/pages/home/home.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { CoreTabsOutletComponent, CoreTabsOutletTab } from '@components/tabs-out
2121
import { CoreMainMenuHomeDelegate, CoreMainMenuHomeHandlerToDisplay } from '../../services/home-delegate';
2222
import { CoreUtils } from '@services/utils/utils';
2323
import { CoreMainMenuHomeHandlerService } from '@features/mainmenu/services/handlers/mainmenu';
24-
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
2524

2625
/**
2726
* Page that displays the Home.
@@ -40,14 +39,11 @@ export class CoreMainMenuHomePage implements OnInit {
4039

4140
protected subscription?: Subscription;
4241
protected updateSiteObserver?: CoreEventObserver;
43-
protected deepLinkManager?: CoreMainMenuDeepLinkManager;
4442

4543
/**
4644
* @inheritdoc
4745
*/
4846
async ngOnInit(): Promise<void> {
49-
this.deepLinkManager = new CoreMainMenuDeepLinkManager();
50-
5147
await this.loadSiteName();
5248

5349
this.subscription = CoreMainMenuHomeDelegate.getHandlersObservable().subscribe((handlers) => {
@@ -108,7 +104,7 @@ export class CoreMainMenuHomePage implements OnInit {
108104
* Tab was selected.
109105
*/
110106
tabSelected(): void {
111-
this.deepLinkManager?.treatLink();
107+
CoreSites.loginNavigationFinished();
112108
}
113109

114110
/**

src/core/features/mainmenu/pages/menu/menu.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from '../../servic
2323
import { Router } from '@singletons';
2424
import { CoreUtils } from '@services/utils/utils';
2525
import { CoreAriaRoleTab, CoreAriaRoleTabFindable } from '@classes/aria-role-tab';
26-
import { CoreNavigationOptions, CoreNavigator } from '@services/navigator';
26+
import { CoreNavigator } from '@services/navigator';
2727
import { filter } from 'rxjs/operators';
2828
import { NavigationEnd } from '@angular/router';
2929
import { trigger, state, style, transition, animate } from '@angular/animations';
@@ -32,6 +32,7 @@ import { CoreDom } from '@singletons/dom';
3232
import { CoreLogger } from '@singletons/logger';
3333
import { CorePlatform } from '@services/platform';
3434
import { CoreWait } from '@singletons/wait';
35+
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
3536

3637
const ANIMATION_DURATION = 500;
3738

@@ -83,9 +84,6 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
8384
protected backButtonFunction: (event: BackButtonEvent) => void;
8485
protected selectHistory: string[] = [];
8586
protected firstSelectedTab?: string;
86-
protected urlToOpen?: string;
87-
protected redirectPath?: string;
88-
protected redirectOptions?: CoreNavigationOptions;
8987
protected logger: CoreLogger;
9088

9189
@ViewChild('mainTabs') mainTabs?: IonTabs;
@@ -111,9 +109,16 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
111109
*/
112110
async ngOnInit(): Promise<void> {
113111
this.showTabs = true;
114-
this.urlToOpen = CoreNavigator.getRouteParam('urlToOpen');
115-
this.redirectPath = CoreNavigator.getRouteParam('redirectPath');
116-
this.redirectOptions = CoreNavigator.getRouteParam('redirectOptions');
112+
113+
const deepLinkManager = new CoreMainMenuDeepLinkManager();
114+
115+
// Treat the deep link (if any) when the login navigation finishes.
116+
CoreSites.runAfterLoginNavigation({
117+
priority: 800,
118+
callback: async () => {
119+
await deepLinkManager.treatLink();
120+
},
121+
});
117122

118123
this.isMainScreen = !this.mainTabs?.outlet.canGoBack();
119124
this.updateVisibility();
@@ -213,12 +218,7 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
213218
// Use navigate instead of mainTabs.select to be able to pass page params.
214219
CoreNavigator.navigateToSitePath(tabPage, {
215220
preferCurrentTab: false,
216-
params: {
217-
urlToOpen: this.urlToOpen,
218-
redirectPath: this.redirectPath,
219-
redirectOptions: this.redirectOptions,
220-
...tabPageParams,
221-
},
221+
params: tabPageParams,
222222
});
223223
}
224224
}

0 commit comments

Comments
 (0)