From e8d00269951dd3d86a199bf2574b14c0c2a4d268 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 25 Nov 2021 12:42:22 +0100 Subject: [PATCH] MOBILE-3926 core: Move item path getters to source --- .../badges/classes/user-badges-source.ts | 18 +++++++++ .../pages/issued-badge/issued-badge.page.ts | 37 ++----------------- .../pages/user-badges/user-badges.page.ts | 29 +-------------- .../mod/assign/classes/submissions-source.ts | 19 ++++++++++ .../submission-list/submission-list.page.ts | 31 +--------------- .../submission-review/submission-review.ts | 25 ++----------- .../items-management/items-manager-source.ts | 21 +++++++++++ .../classes/items-management/items-manager.ts | 31 +++++++--------- .../items-management/list-items-manager.ts | 8 ++-- .../items-management/swipe-items-manager.ts | 28 +++++++------- .../user/classes/participants-source.ts | 15 ++++++++ .../pages/participants/participants.page.ts | 29 ++------------- .../user/pages/profile/profile.page.ts | 33 ++--------------- 13 files changed, 122 insertions(+), 202 deletions(-) diff --git a/src/addons/badges/classes/user-badges-source.ts b/src/addons/badges/classes/user-badges-source.ts index 1ece130e0..ea36a237f 100644 --- a/src/addons/badges/classes/user-badges-source.ts +++ b/src/addons/badges/classes/user-badges-source.ts @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { Params } from '@angular/router'; import { CoreItemsManagerSource } from '@classes/items-management/items-manager-source'; import { AddonBadges, AddonBadgesUserBadge } from '../services/badges'; @@ -30,6 +31,23 @@ export class AddonBadgesUserBadgesSource extends CoreItemsManagerSource { - - /** - * @inheritdoc - */ - protected getItemPath(badge: AddonBadgesUserBadge): string { - return String(badge.uniquehash); - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(): Params { - return { - courseId: this.getSource().COURSE_ID, - userId: this.getSource().USER_ID, - }; - } - - /** - * @inheritdoc - */ - protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null { - return route.params.badgeHash; - } - -} diff --git a/src/addons/badges/pages/user-badges/user-badges.page.ts b/src/addons/badges/pages/user-badges/user-badges.page.ts index 5900ff36a..e614c3bac 100644 --- a/src/addons/badges/pages/user-badges/user-badges.page.ts +++ b/src/addons/badges/pages/user-badges/user-badges.page.ts @@ -19,7 +19,6 @@ import { CoreTimeUtils } from '@services/utils/time'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; -import { Params } from '@angular/router'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreNavigator } from '@services/navigator'; import { CoreListItemsManager } from '@classes/items-management/list-items-manager'; @@ -36,7 +35,7 @@ import { CoreItemsManagerSourcesTracker } from '@classes/items-management/items- export class AddonBadgesUserBadgesPage implements AfterViewInit, OnDestroy { currentTime = 0; - badges: AddonBadgesUserBadgesListManager; + badges: CoreListItemsManager; @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; @@ -49,7 +48,7 @@ export class AddonBadgesUserBadgesPage implements AfterViewInit, OnDestroy { courseId = 0; } - this.badges = new AddonBadgesUserBadgesListManager( + this.badges = new CoreListItemsManager( CoreItemsManagerSourcesTracker.getOrCreateSource(AddonBadgesUserBadgesSource, [courseId, userId]), AddonBadgesUserBadgesPage, ); @@ -104,27 +103,3 @@ export class AddonBadgesUserBadgesPage implements AfterViewInit, OnDestroy { } } - -/** - * Helper class to manage badges list. - */ -class AddonBadgesUserBadgesListManager extends CoreListItemsManager { - - /** - * @inheritdoc - */ - protected getItemPath(badge: AddonBadgesUserBadge): string { - return badge.uniquehash; - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(): Params { - return { - courseId: this.getSource().COURSE_ID, - userId: this.getSource().USER_ID, - }; - } - -} diff --git a/src/addons/mod/assign/classes/submissions-source.ts b/src/addons/mod/assign/classes/submissions-source.ts index f9a21f280..fce121386 100644 --- a/src/addons/mod/assign/classes/submissions-source.ts +++ b/src/addons/mod/assign/classes/submissions-source.ts @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { Params } from '@angular/router'; import { CoreItemsManagerSource } from '@classes/items-management/items-manager-source'; import { CoreGroupInfo, CoreGroups } from '@services/groups'; import { CoreSites } from '@services/sites'; @@ -68,6 +69,24 @@ export class AddonModAssignSubmissionsSource extends CoreItemsManagerSource; // List of submissions protected gradedObserver: CoreEventObserver; // Observer to refresh data when a grade changes. protected syncObserver: CoreEventObserver; // Observer to refresh data when the async is synchronized. @@ -99,7 +98,7 @@ export class AddonModAssignSubmissionListPage implements AfterViewInit, OnDestro }, }); - this.submissions = new AddonModAssignSubmissionListManager( + this.submissions = new CoreListItemsManager( submissionsSource, AddonModAssignSubmissionListPage, ); @@ -213,29 +212,3 @@ export class AddonModAssignSubmissionListPage implements AfterViewInit, OnDestro } } - -/** - * Helper class to manage submissions. - */ -class AddonModAssignSubmissionListManager - extends CoreListItemsManager { - - /** - * @inheritdoc - */ - protected getItemPath(submission: AddonModAssignSubmissionForList): string { - return String(submission.submitid); - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(submission: AddonModAssignSubmissionForList): Params { - return { - blindId: submission.blindid, - groupId: this.getSource().groupId, - selectedStatus: this.getSource().SELECTED_STATUS, - }; - } - -} diff --git a/src/addons/mod/assign/pages/submission-review/submission-review.ts b/src/addons/mod/assign/pages/submission-review/submission-review.ts index 02a3e4e3b..015c73425 100644 --- a/src/addons/mod/assign/pages/submission-review/submission-review.ts +++ b/src/addons/mod/assign/pages/submission-review/submission-review.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; -import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router'; +import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router'; import { CoreItemsManagerSourcesTracker } from '@classes/items-management/items-manager-sources-tracker'; import { CoreSwipeItemsManager } from '@classes/items-management/swipe-items-manager'; import { CoreCourse } from '@features/course/services/course'; @@ -22,7 +22,7 @@ import { IonRefresher } from '@ionic/angular'; import { CoreNavigator } from '@services/navigator'; import { CoreScreen } from '@services/screen'; import { CoreDomUtils } from '@services/utils/dom'; -import { AddonModAssignSubmissionForList, AddonModAssignSubmissionsSource } from '../../classes/submissions-source'; +import { AddonModAssignSubmissionsSource } from '../../classes/submissions-source'; import { AddonModAssignSubmissionComponent } from '../../components/submission/submission'; import { AddonModAssign, AddonModAssignAssign } from '../../services/assign'; @@ -216,26 +216,7 @@ export class AddonModAssignSubmissionReviewPage implements OnInit, OnDestroy, Ca /** * Helper to manage swiping within a collection of submissions. */ -class AddonModAssignSubmissionSwipeItemsManager - extends CoreSwipeItemsManager { - - /** - * @inheritdoc - */ - protected getItemPath(submission: AddonModAssignSubmissionForList): string { - return String(submission.submitid); - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(submission: AddonModAssignSubmissionForList): Params { - return { - blindId: submission.blindid, - groupId: this.getSource().groupId, - selectedStatus: this.getSource().SELECTED_STATUS, - }; - } +class AddonModAssignSubmissionSwipeItemsManager extends CoreSwipeItemsManager { /** * @inheritdoc diff --git a/src/core/classes/items-management/items-manager-source.ts b/src/core/classes/items-management/items-manager-source.ts index 68baccf70..5a00e5c9c 100644 --- a/src/core/classes/items-management/items-manager-source.ts +++ b/src/core/classes/items-management/items-manager-source.ts @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { Params } from '@angular/router'; + /** * Updates listener. */ @@ -143,6 +145,25 @@ export abstract class CoreItemsManagerSource { this.setItems((this.items ?? []).concat(items), hasMoreItems ?? false); } + /** + * Get the query parameters to use when navigating to an item page. + * + * @param item Item. + * @return Query parameters to use when navigating to the item page. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getItemQueryParams(item: Item): Params { + return {}; + } + + /** + * Get the path to use when navigating to an item page. + * + * @param item Item. + * @return Path to use when navigating to the item page. + */ + abstract getItemPath(item: Item): string; + /** * Load page items. * diff --git a/src/core/classes/items-management/items-manager.ts b/src/core/classes/items-management/items-manager.ts index edc277469..f2be8bd67 100644 --- a/src/core/classes/items-management/items-manager.ts +++ b/src/core/classes/items-management/items-manager.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router'; +import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router'; import { CoreNavigationOptions, CoreNavigator } from '@services/navigator'; import { CoreItemsManagerSource } from './items-manager-source'; @@ -93,12 +93,12 @@ export abstract class CoreItemsManager { - map[this.getItemPath(item)] = item; + map[this.getSource().getItemPath(item)] = item; return map; }, {}); diff --git a/src/core/classes/items-management/list-items-manager.ts b/src/core/classes/items-management/list-items-manager.ts index 9bbf3fdfc..a9e22b9a7 100644 --- a/src/core/classes/items-management/list-items-manager.ts +++ b/src/core/classes/items-management/list-items-manager.ts @@ -26,7 +26,7 @@ import { CoreItemsManagerSource } from './items-manager-source'; /** * Helper class to manage the state and routing of a list of items in a page. */ -export abstract class CoreListItemsManager< +export class CoreListItemsManager< Item = unknown, Source extends CoreItemsManagerSource = CoreItemsManagerSource > extends CoreItemsManager { @@ -206,10 +206,12 @@ export abstract class CoreListItemsManager< /** * @inheritdoc */ - protected getSelectedItemPath(route?: ActivatedRouteSnapshot | null): string | null { + protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null { const segments: UrlSegment[] = []; - while ((route = route?.firstChild)) { + while (route.firstChild) { + route = route.firstChild; + segments.push(...route.url); } diff --git a/src/core/classes/items-management/swipe-items-manager.ts b/src/core/classes/items-management/swipe-items-manager.ts index b15e59870..3dd41cd4f 100644 --- a/src/core/classes/items-management/swipe-items-manager.ts +++ b/src/core/classes/items-management/swipe-items-manager.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router'; +import { ActivatedRoute, ActivatedRouteSnapshot, UrlSegment } from '@angular/router'; import { CoreNavigator } from '@services/navigator'; @@ -22,7 +22,7 @@ import { CoreItemsManagerSource } from './items-manager-source'; /** * Helper class to manage the state and routing of a swipeable page. */ -export abstract class CoreSwipeItemsManager< +export class CoreSwipeItemsManager< Item = unknown, Source extends CoreItemsManagerSource = CoreItemsManagerSource > @@ -49,14 +49,6 @@ export abstract class CoreSwipeItemsManager< await this.navigateToItemBy(1, 'forward'); } - /** - * Get the path of the selected item given the current route. - * - * @param route Page route. - * @return Path of the selected item in the given route. - */ - protected abstract getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null; - /** * @inheritdoc */ @@ -67,12 +59,20 @@ export abstract class CoreSwipeItemsManager< /** * @inheritdoc */ - protected getSelectedItemPath(route?: ActivatedRouteSnapshot | null): string | null { - if (!route) { - return null; + protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null { + const segments: UrlSegment[] = []; + + while (route) { + segments.push(...route.url); + + if (!route.firstChild) { + break; + } + + route = route.firstChild; } - return this.getSelectedItemPathFromRoute(route); + return segments.map(segment => segment.path).join('/').replace(/\/+/, '/').trim() || null; } /** diff --git a/src/core/features/user/classes/participants-source.ts b/src/core/features/user/classes/participants-source.ts index 594ff4817..9aa6e4db1 100644 --- a/src/core/features/user/classes/participants-source.ts +++ b/src/core/features/user/classes/participants-source.ts @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { Params } from '@angular/router'; import { CoreItemsManagerSource } from '@classes/items-management/items-manager-source'; import { CoreUser, CoreUserData, CoreUserParticipant, CoreUserProvider } from '../services/user'; @@ -40,6 +41,20 @@ export class CoreUserParticipantsSource extends CoreItemsManagerSource { - - page: CoreUserParticipantsPage; - - constructor(source: CoreUserParticipantsSource, page: CoreUserParticipantsPage) { - super(source, CoreUserParticipantsPage); - - this.page = page; - } - - /** - * @inheritdoc - */ - protected getItemPath(participant: CoreUserParticipant | CoreUserData): string { - return participant.id.toString(); - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(): Params { - return { search: this.page.searchQuery }; - } +class CoreUserParticipantsManager extends CoreListItemsManager { /** * @inheritdoc */ protected async logActivity(): Promise { - await CoreUser.logParticipantsView(this.page.courseId); + await CoreUser.logParticipantsView(this.getSource().COURSE_ID); } } diff --git a/src/core/features/user/pages/profile/profile.page.ts b/src/core/features/user/pages/profile/profile.page.ts index c9b2622c2..bcfc6bad3 100644 --- a/src/core/features/user/pages/profile/profile.page.ts +++ b/src/core/features/user/pages/profile/profile.page.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router'; +import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router'; import { Component, OnDestroy, OnInit } from '@angular/core'; import { IonRefresher } from '@ionic/angular'; import { Subscription } from 'rxjs'; @@ -21,7 +21,7 @@ import { CoreSite } from '@classes/site'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; -import { CoreUser, CoreUserBasicData, CoreUserProfile, CoreUserProvider } from '@features/user/services/user'; +import { CoreUser, CoreUserProfile, CoreUserProvider } from '@features/user/services/user'; import { CoreUserHelper } from '@features/user/services/user-helper'; import { CoreUserDelegate, CoreUserDelegateService, CoreUserProfileHandlerData } from '@features/user/services/user-delegate'; import { CoreUtils } from '@services/utils/utils'; @@ -30,7 +30,6 @@ import { CoreCourses } from '@features/courses/services/courses'; import { CoreSwipeItemsManager } from '@classes/items-management/swipe-items-manager'; import { CoreUserParticipantsSource } from '@features/user/classes/participants-source'; import { CoreItemsManagerSourcesTracker } from '@classes/items-management/items-manager-sources-tracker'; -import { CoreItemsManagerSource } from '@classes/items-management/items-manager-source'; @Component({ selector: 'page-core-user-profile', @@ -57,7 +56,6 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { communicationHandlers: CoreUserProfileHandlerData[] = []; users?: CoreUserSwipeItemsManager; - usersQueryParams: Params = {}; constructor(private route: ActivatedRoute) { this.obsProfileRefreshed = CoreEvents.on(CoreUserProvider.PROFILE_REFRESHED, (data) => { @@ -93,9 +91,8 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { if (this.courseId && this.route.snapshot.data.swipeManagerSource === 'participants') { const search = CoreNavigator.getRouteParam('search'); const source = CoreItemsManagerSourcesTracker.getOrCreateSource(CoreUserParticipantsSource, [this.courseId, search]); - this.users = new CoreUserSwipeItemsManager(source, this); + this.users = new CoreUserSwipeItemsManager(source); - this.usersQueryParams.search = search; this.users.start(); } @@ -227,29 +224,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { /** * Helper to manage swiping within a collection of users. */ -class CoreUserSwipeItemsManager extends CoreSwipeItemsManager { - - page: CoreUserProfilePage; - - constructor(source: CoreItemsManagerSource, page: CoreUserProfilePage) { - super(source); - - this.page = page; - } - - /** - * @inheritdoc - */ - protected getItemPath(item: CoreUserBasicData): string { - return String(item.id); - } - - /** - * @inheritdoc - */ - protected getItemQueryParams(): Params { - return this.page.usersQueryParams; - } +class CoreUserSwipeItemsManager extends CoreSwipeItemsManager { /** * @inheritdoc