From a2f6e0139e6de76cf8c6d7e295f24e8d496f2dc9 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 2 Jun 2022 17:07:01 +0200 Subject: [PATCH] MOBILE-4081 notifications: Improve source --- .../classes/legacy-notifications-source.ts | 62 +++++++++++ .../classes/notifications-source.ts | 103 ++++++++++++++---- src/addons/notifications/pages/list/list.ts | 9 +- .../pages/notification/notification.ts | 9 +- .../notifications/services/notifications.ts | 3 +- 5 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/addons/notifications/classes/legacy-notifications-source.ts diff --git a/src/addons/notifications/classes/legacy-notifications-source.ts b/src/addons/notifications/classes/legacy-notifications-source.ts new file mode 100644 index 000000000..564689350 --- /dev/null +++ b/src/addons/notifications/classes/legacy-notifications-source.ts @@ -0,0 +1,62 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { AddonNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source'; +import { AddonNotificationsGetReadType } from '@addons/notifications/services/notifications'; +import { AddonNotificationsNotificationToRender } from '@addons/notifications/services/notifications-helper'; + +/** + * Provides a list of notifications using legacy web services. + */ +export class AddonLegacyNotificationsNotificationsSource extends AddonNotificationsNotificationsSource { + + /** + * @inheritdoc + */ + protected async loadPageItems(page: number): Promise<{ + items: AddonNotificationsNotificationToRender[]; + hasMoreItems: boolean; + }> { + let items: AddonNotificationsNotificationToRender[] = []; + let hasMoreItems = false; + let pageUnreadCount = 0; + const pageLength = this.getPageLength(); + const totalUnread = () => this.totals[AddonNotificationsGetReadType.UNREAD] ?? Number.MAX_VALUE; + + // Load unread notifications first. + if (totalUnread() > page * pageLength) { + const pageResults = await this.loadNotifications(AddonNotificationsGetReadType.UNREAD, page * pageLength); + + items = items.concat(pageResults.notifications); + hasMoreItems = pageResults.hasMoreNotifications; + pageUnreadCount = pageResults.notifications.length; + } + + // If all unread notifications have been fetched, load read notifications first. + if (totalUnread() < (page + 1) * pageLength) { + const offset = Math.max(0, page * pageLength - totalUnread()); + const pageResults = await this.loadNotifications( + AddonNotificationsGetReadType.READ, + offset, + pageLength - pageUnreadCount, + ); + + items = items.concat(pageResults.notifications); + hasMoreItems = pageResults.hasMoreNotifications; + } + + return { items, hasMoreItems }; + } + +} diff --git a/src/addons/notifications/classes/notifications-source.ts b/src/addons/notifications/classes/notifications-source.ts index 6a49c913d..4ff8edb85 100644 --- a/src/addons/notifications/classes/notifications-source.ts +++ b/src/addons/notifications/classes/notifications-source.ts @@ -12,30 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { + AddonNotifications, + AddonNotificationsGetReadType, + AddonNotificationsProvider, +} from '@addons/notifications/services/notifications'; +import { AddonNotificationsNotificationToRender } from '@addons/notifications/services/notifications-helper'; import { CoreRoutedItemsManagerSource } from '@classes/items-management/routed-items-manager-source'; -import { AddonNotifications } from '../services/notifications'; -import { AddonNotificationsHelper, AddonNotificationsNotificationToRender } from '../services/notifications-helper'; /** - * Provides a list of notifications + * Provides a list of notifications. */ -export class AddonsNotificationsNotificationsSource extends CoreRoutedItemsManagerSource { +export class AddonNotificationsNotificationsSource extends CoreRoutedItemsManagerSource { - /** - * @inheritdoc - */ - protected async loadPageItems(page: number): Promise<{ - items: AddonNotificationsNotificationToRender[]; - hasMoreItems: boolean; - }> { - // TODO this should be refactored to avoid using the existing items. - const { notifications, canLoadMore } = await AddonNotifications.getNotifications(page === 0 ? [] : this.getItems() ?? []); - - return { - items: notifications.map(notification => AddonNotificationsHelper.formatNotificationText(notification)), - hasMoreItems: canLoadMore, - }; - } + protected totals: Record = {}; /** * @inheritdoc @@ -44,4 +34,79 @@ export class AddonsNotificationsNotificationsSource extends CoreRoutedItemsManag return notification.id.toString(); } + /** + * @inheritdoc + */ + reset(): void { + this.totals = {}; + + super.reset(); + } + + /** + * @inheritdoc + */ + protected async loadPageItems(page: number): Promise<{ + items: AddonNotificationsNotificationToRender[]; + hasMoreItems: boolean; + }> { + const results = await this.loadNotifications(AddonNotificationsGetReadType.BOTH, page * this.getPageLength()); + + return { + items: results.notifications, + hasMoreItems: results.hasMoreNotifications, + }; + } + + /** + * Load notifications of the given type. + * + * @param type Type. + * @param offset Offset. + * @param limit Limit. + * @returns Notifications and whether there are any more. + */ + protected async loadNotifications(type: AddonNotificationsGetReadType, offset: number, limit?: number): Promise<{ + notifications: AddonNotificationsNotificationToRender[]; + hasMoreNotifications: boolean; + }> { + limit = limit ?? this.getPageLength(); + + if (type in this.totals && this.totals[type] <= offset) { + return { + notifications: [], + hasMoreNotifications: false, + }; + } + + const notifications = await AddonNotifications.getNotificationsWithStatus(type, { offset, limit }); + + if (notifications.length < limit) { + this.totals[type] = offset + notifications.length; + } + + return { + notifications, + hasMoreNotifications: (this.totals[type] ?? Number.MAX_VALUE) > offset + limit, + }; + } + + /** + * @inheritdoc + */ + protected setItems(notifications: AddonNotificationsNotificationToRender[], hasMoreItems: boolean): void { + const sortedNotifications = notifications.slice(0); + + sortedNotifications.sort((a, b) => a.timecreated < b.timecreated ? 1 : -1); + + super.setItems(sortedNotifications, hasMoreItems); + } + + /** + * @inheritdoc + */ + protected getPageLength(): number { + return AddonNotificationsProvider.LIST_LIMIT; + } + } diff --git a/src/addons/notifications/pages/list/list.ts b/src/addons/notifications/pages/list/list.ts index 9d144a5dd..cf6e48803 100644 --- a/src/addons/notifications/pages/list/list.ts +++ b/src/addons/notifications/pages/list/list.ts @@ -29,9 +29,10 @@ import { CorePushNotificationsDelegate } from '@features/pushnotifications/servi import { CoreSites } from '@services/sites'; import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager'; import { CoreTimeUtils } from '@services/utils/time'; -import { AddonsNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source'; +import { AddonNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source'; import { CoreListItemsManager } from '@classes/items-management/list-items-manager'; import { AddonNotificationsNotificationToRender } from '@addons/notifications/services/notifications-helper'; +import { AddonLegacyNotificationsNotificationsSource } from '@addons/notifications/classes/legacy-notifications-source'; /** * Page that displays the list of notifications. @@ -44,7 +45,7 @@ import { AddonNotificationsNotificationToRender } from '@addons/notifications/se export class AddonNotificationsListPage implements AfterViewInit, OnDestroy { @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; - notifications!: CoreListItemsManager; + notifications!: CoreListItemsManager; fetchMoreNotificationsFailed = false; canMarkAllNotificationsAsRead = false; loadingMarkAllNotificationsAsRead = false; @@ -58,7 +59,9 @@ export class AddonNotificationsListPage implements AfterViewInit, OnDestroy { constructor() { try { const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource( - AddonsNotificationsNotificationsSource, + CoreSites.getRequiredCurrentSite().isVersionGreaterEqualThan('4.0') + ? AddonNotificationsNotificationsSource + : AddonLegacyNotificationsNotificationsSource, [], ); diff --git a/src/addons/notifications/pages/notification/notification.ts b/src/addons/notifications/pages/notification/notification.ts index 5b5993f2f..6e161cec4 100644 --- a/src/addons/notifications/pages/notification/notification.ts +++ b/src/addons/notifications/pages/notification/notification.ts @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { AddonsNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source'; +import { AddonLegacyNotificationsNotificationsSource } from '@addons/notifications/classes/legacy-notifications-source'; +import { AddonNotificationsNotificationsSource } from '@addons/notifications/classes/notifications-source'; import { AddonNotificationsNotificationData } from '@addons/notifications/services/handlers/push-click'; import { AddonNotificationsHelper, @@ -118,7 +119,9 @@ export class AddonNotificationsNotificationPage implements OnInit, OnDestroy { */ getNotificationById(notificationId: number): AddonNotificationsNotification | undefined { const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource( - AddonsNotificationsNotificationsSource, + CoreSites.getRequiredCurrentSite().isVersionGreaterEqualThan('4.0') + ? AddonNotificationsNotificationsSource + : AddonLegacyNotificationsNotificationsSource, [], ); const notification = source.getItems()?.find(({ id }) => id === notificationId); @@ -137,7 +140,7 @@ export class AddonNotificationsNotificationPage implements OnInit, OnDestroy { * * @param source Notifications source */ - async loadNotifications(source: AddonsNotificationsNotificationsSource): Promise { + async loadNotifications(source: AddonNotificationsNotificationsSource): Promise { this.notifications = new AddonNotificationSwipeItemsManager(source); await this.notifications.start(); diff --git a/src/addons/notifications/services/notifications.ts b/src/addons/notifications/services/notifications.ts index d6426b01a..1b29afa61 100644 --- a/src/addons/notifications/services/notifications.ts +++ b/src/addons/notifications/services/notifications.ts @@ -165,6 +165,7 @@ export class AddonNotificationsProvider { * @param notifications Current list of loaded notifications. It's used to calculate the offset. * @param options Other options. * @return Promise resolved with notifications and if can load more. + * @deprecated since 4.1. Use getNotificationsWithStatus instead. */ async getNotifications( notifications: AddonNotificationsNotificationMessageFormatted[], @@ -231,7 +232,7 @@ export class AddonNotificationsProvider { * @param options Other options. * @return Promise resolved with notifications. */ - protected async getNotificationsWithStatus( + async getNotificationsWithStatus( read: AddonNotificationsGetReadType, options: AddonNotificationsGetNotificationsOptions = {}, ): Promise {