MOBILE-3926 core: Move item path getters to source

main
Noel De Martin 2021-11-25 12:42:22 +01:00
parent f45f984d8e
commit e8d0026995
13 changed files with 122 additions and 202 deletions

View File

@ -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<AddonBad
this.USER_ID = userId;
}
/**
* @inheritdoc
*/
getItemPath(badge: AddonBadgesUserBadge): string {
return badge.uniquehash;
}
/**
* @inheritdoc
*/
getItemQueryParams(): Params {
return {
courseId: this.COURSE_ID,
userId: this.USER_ID,
};
}
/**
* @inheritdoc
*/

View File

@ -22,7 +22,7 @@ import { AddonBadges, AddonBadgesUserBadge } from '../../services/badges';
import { CoreUtils } from '@services/utils/utils';
import { CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses';
import { CoreNavigator } from '@services/navigator';
import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { CoreSwipeItemsManager } from '@classes/items-management/swipe-items-manager';
import { CoreItemsManagerSourcesTracker } from '@classes/items-management/items-manager-sources-tracker';
import { AddonBadgesUserBadgesSource } from '@addons/badges/classes/user-badges-source';
@ -43,7 +43,7 @@ export class AddonBadgesIssuedBadgePage implements OnInit {
user?: CoreUserProfile;
course?: CoreEnrolledCourseData;
badge?: AddonBadgesUserBadge;
badges?: AddonBadgesUserBadgesSwipeManager;
badges?: CoreSwipeItemsManager;
badgeLoaded = false;
currentTime = 0;
@ -62,7 +62,7 @@ export class AddonBadgesIssuedBadgePage implements OnInit {
});
const source = CoreItemsManagerSourcesTracker.getOrCreateSource(AddonBadgesUserBadgesSource, [this.courseId, this.userId]);
this.badges = new AddonBadgesUserBadgesSwipeManager(source);
this.badges = new CoreSwipeItemsManager(source);
this.badges.start();
}
@ -117,34 +117,3 @@ export class AddonBadgesIssuedBadgePage implements OnInit {
}
}
/**
* Helper to manage swiping within a collection of user badges.
*/
class AddonBadgesUserBadgesSwipeManager extends CoreSwipeItemsManager<AddonBadgesUserBadge, AddonBadgesUserBadgesSource> {
/**
* @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;
}
}

View File

@ -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<AddonBadgesUserBadge, AddonBadgesUserBadgesSource>;
@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<AddonBadgesUserBadge, AddonBadgesUserBadgesSource> {
/**
* @inheritdoc
*/
protected getItemPath(badge: AddonBadgesUserBadge): string {
return badge.uniquehash;
}
/**
* @inheritdoc
*/
protected getItemQueryParams(): Params {
return {
courseId: this.getSource().COURSE_ID,
userId: this.getSource().USER_ID,
};
}
}

View File

@ -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<Addo
this.SELECTED_STATUS = selectedStatus;
}
/**
* @inheritdoc
*/
getItemPath(submission: AddonModAssignSubmissionForList): string {
return String(submission.submitid);
}
/**
* @inheritdoc
*/
getItemQueryParams(submission: AddonModAssignSubmissionForList): Params {
return {
blindId: submission.blindid,
groupId: this.groupId,
selectedStatus: this.SELECTED_STATUS,
};
}
/**
* Invalidate assignment cache.
*/

View File

@ -13,7 +13,6 @@
// limitations under the License.
import { Component, OnDestroy, AfterViewInit, ViewChild } from '@angular/core';
import { Params } from '@angular/router';
import { CoreItemsManagerSourcesTracker } from '@classes/items-management/items-manager-sources-tracker';
import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
import { CoreSplitViewComponent } from '@components/split-view/split-view';
@ -44,7 +43,7 @@ export class AddonModAssignSubmissionListPage implements AfterViewInit, OnDestro
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
title = '';
submissions!: AddonModAssignSubmissionListManager; // List of submissions
submissions!: CoreListItemsManager<AddonModAssignSubmissionForList, AddonModAssignSubmissionsSource>; // 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<AddonModAssignSubmissionForList, AddonModAssignSubmissionsSource> {
/**
* @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,
};
}
}

View File

@ -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<AddonModAssignSubmissionForList, AddonModAssignSubmissionsSource> {
/**
* @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

View File

@ -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<Item = unknown> {
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.
*

View File

@ -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<Item = unknown, Source extends CoreItemsM
protected abstract getCurrentPageRoute(): ActivatedRoute | null;
/**
* Get the path to use when navigating to an item page.
* Get the path of the selected item given the current route.
*
* @param item Item.
* @return Path to use when navigating to the item page.
* @param route Page route.
* @return Path of the selected item in the given route.
*/
protected abstract getItemPath(item: Item): string;
protected abstract getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null;
/**
* Get the path of the selected item.
@ -106,17 +106,12 @@ export abstract class CoreItemsManager<Item = unknown, Source extends CoreItemsM
* @param route Page route, if any.
* @return Path of the selected item.
*/
protected abstract getSelectedItemPath(route?: ActivatedRouteSnapshot | null): string | null;
protected getSelectedItemPath(route?: ActivatedRouteSnapshot | null): string | null {
if (!route) {
return null;
}
/**
* 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
protected getItemQueryParams(item: Item): Params {
return {};
return this.getSelectedItemPathFromRoute(route);
}
/**
@ -152,7 +147,7 @@ export abstract class CoreItemsManager<Item = unknown, Source extends CoreItemsM
}
// If this item is already selected, do nothing.
const itemPath = this.getItemPath(item);
const itemPath = this.getSource().getItemPath(item);
const selectedItemPath = this.getSelectedItemPath(route.snapshot);
if (selectedItemPath === itemPath) {
@ -160,7 +155,7 @@ export abstract class CoreItemsManager<Item = unknown, Source extends CoreItemsM
}
// Navigate to item.
const params = this.getItemQueryParams(item);
const params = this.getSource().getItemQueryParams(item);
const pathPrefix = selectedItemPath ? selectedItemPath.split('/').fill('../').join('') : '';
await CoreNavigator.navigate(pathPrefix + itemPath, { params, ...options });
@ -173,7 +168,7 @@ export abstract class CoreItemsManager<Item = unknown, Source extends CoreItemsM
*/
protected onSourceItemsUpdated(items: Item[]): void {
this.itemsMap = items.reduce((map, item) => {
map[this.getItemPath(item)] = item;
map[this.getSource().getItemPath(item)] = item;
return map;
}, {});

View File

@ -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<Item> = CoreItemsManagerSource<Item>
> extends CoreItemsManager<Item, Source> {
@ -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);
}

View File

@ -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<Item> = CoreItemsManagerSource<Item>
>
@ -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;
}
return this.getSelectedItemPathFromRoute(route);
route = route.firstChild;
}
return segments.map(segment => segment.path).join('/').replace(/\/+/, '/').trim() || null;
}
/**

View File

@ -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<CoreUserP
this.SEARCH_QUERY = searchQuery;
}
/**
* @inheritdoc
*/
getItemPath(user: CoreUserParticipant | CoreUserData): string {
return user.id.toString();
}
/**
* @inheritdoc
*/
getItemQueryParams(): Params {
return { search: this.SEARCH_QUERY };
}
/**
* @inheritdoc
*/

View File

@ -13,7 +13,6 @@
// limitations under the License.
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { Params } from '@angular/router';
import { IonRefresher } from '@ionic/angular';
import { CoreApp } from '@services/app';
@ -50,7 +49,7 @@ export class CoreUserParticipantsPage implements OnInit, AfterViewInit, OnDestro
this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
this.participants = new CoreUserParticipantsManager(
CoreItemsManagerSourcesTracker.getOrCreateSource(CoreUserParticipantsSource, [this.courseId]),
this,
CoreUserParticipantsPage,
);
} catch (error) {
CoreDomUtils.showErrorModal(error);
@ -196,35 +195,13 @@ export class CoreUserParticipantsPage implements OnInit, AfterViewInit, OnDestro
/**
* Helper to manage the list of participants.
*/
class CoreUserParticipantsManager extends CoreListItemsManager<CoreUserParticipant | CoreUserData> {
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<CoreUserParticipant | CoreUserData, CoreUserParticipantsSource> {
/**
* @inheritdoc
*/
protected async logActivity(): Promise<void> {
await CoreUser.logParticipantsView(this.page.courseId);
await CoreUser.logParticipantsView(this.getSource().COURSE_ID);
}
}

View File

@ -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<CoreUserBasicData> {
page: CoreUserProfilePage;
constructor(source: CoreItemsManagerSource<CoreUserBasicData>, 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