MOBILE-4021 notification: add swipe

main
Alfonso Salces 2022-04-21 10:01:36 +02:00
parent b921fd3706
commit e7c808dd94
2 changed files with 55 additions and 23 deletions

View File

@ -8,7 +8,7 @@
</ion-title> </ion-title>
</ion-toolbar> </ion-toolbar>
</ion-header> </ion-header>
<ion-content> <ion-content [core-swipe-navigation]="notifications">
<core-loading [hideUntil]="loaded"> <core-loading [hideUntil]="loaded">
<div class="list-item-limited-width"> <div class="list-item-limited-width">

View File

@ -18,9 +18,10 @@ import {
AddonNotificationsHelper, AddonNotificationsHelper,
AddonNotificationsNotificationToRender, AddonNotificationsNotificationToRender,
} from '@addons/notifications/services/notifications-helper'; } from '@addons/notifications/services/notifications-helper';
import { Component, OnInit } from '@angular/core'; import { Component, OnDestroy, OnInit } from '@angular/core';
import { CoreError } from '@classes/errors/error'; import { ActivatedRouteSnapshot } from '@angular/router';
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker'; import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
import { CoreSwipeNavigationItemsManager } from '@classes/items-management/swipe-navigation-items-manager';
import { CoreContentLinksAction, CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate'; import { CoreContentLinksAction, CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
@ -34,8 +35,9 @@ import { CoreDomUtils } from '@services/utils/dom';
templateUrl: 'notification.html', templateUrl: 'notification.html',
styleUrls: ['../../notifications.scss', 'notification.scss'], styleUrls: ['../../notifications.scss', 'notification.scss'],
}) })
export class AddonNotificationsNotificationPage implements OnInit { export class AddonNotificationsNotificationPage implements OnInit, OnDestroy {
notifications?: AddonNotificationSwipeItemsManager;
subject = ''; // Notification subject. subject = ''; // Notification subject.
content = ''; // Notification content. content = ''; // Notification content.
userIdFrom = -1; // User ID who sent the notification. userIdFrom = -1; // User ID who sent the notification.
@ -59,10 +61,10 @@ export class AddonNotificationsNotificationPage implements OnInit {
let notification: AddonNotificationsNotification; let notification: AddonNotificationsNotification;
try { try {
notification = this.getCurrentNotification(); notification = this.getNotification();
} catch (error) { } catch (error) {
await CoreDomUtils.showErrorModal(error); CoreDomUtils.showErrorModal(error);
await CoreNavigator.back(); CoreNavigator.back();
return; return;
} }
@ -76,15 +78,12 @@ export class AddonNotificationsNotificationPage implements OnInit {
this.iconUrl = notification.iconurl; this.iconUrl = notification.iconurl;
if (notification.moodlecomponent?.startsWith('mod_') && notification.iconurl) { if (notification.moodlecomponent?.startsWith('mod_') && notification.iconurl) {
const modname = notification.moodlecomponent.substring(4); const modname = notification.moodlecomponent.substring(4);
if ( if (notification.iconurl.match('/theme/image.php/[^/]+/' + modname + '/[-0-9]*/') ||
notification.iconurl.match('/theme/image.php/[^/]+/' + modname + '/[-0-9]*/') || notification.iconurl.match('/theme/image.php/[^/]+/' + notification.moodlecomponent + '/[-0-9]*/')) {
notification.iconurl.match('/theme/image.php/[^/]+/' + notification.moodlecomponent + '/[-0-9]*/')
) {
this.modname = modname; this.modname = modname;
} }
} }
this.timecreated = notification.timecreated; this.timecreated = notification.timecreated;
} else { } else {
this.subject = notification.title || ''; this.subject = notification.title || '';
this.content = notification.message || ''; this.content = notification.message || '';
@ -100,12 +99,24 @@ export class AddonNotificationsNotificationPage implements OnInit {
} }
/** /**
* Load notifications * Get notification.
*
* @returns notification.
*/
getNotification(): AddonNotificationsNotification {
const id = CoreNavigator.getRouteNumberParam('id');
const notification = id ? this.getNotificationById(id) : undefined;
return notification ?? CoreNavigator.getRequiredRouteParam('notification');
}
/**
* Obtain notification by passed id.
* *
* @param notificationId Notification id. * @param notificationId Notification id.
* @return Found notification * @return Found notification.
*/ */
loadNotifications(notificationId: number): AddonNotificationsNotification { getNotificationById(notificationId: number): AddonNotificationsNotification | undefined {
const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource( const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource(
AddonsNotificationsNotificationsSource, AddonsNotificationsNotificationsSource,
[], [],
@ -113,23 +124,23 @@ export class AddonNotificationsNotificationPage implements OnInit {
const notification = source.getItems()?.find(({ id }) => id === notificationId); const notification = source.getItems()?.find(({ id }) => id === notificationId);
if (!notification) { if (!notification) {
throw new CoreError(`Notification with id ${notificationId} not found`); return;
} }
this.loadNotifications(source);
return notification; return notification;
} }
/** /**
* Load current notification if it's found * Load notifications from source.
* *
* @return Found notification * @param source Notifications source
*/ */
getCurrentNotification(): AddonNotificationsNotification { async loadNotifications(source: AddonsNotificationsNotificationsSource): Promise<void> {
const pushNotification: AddonNotificationsNotificationData | undefined = CoreNavigator.getRouteParam('notification'); this.notifications = new AddonNotificationSwipeItemsManager(source);
return this.loadNotifications( await this.notifications.start();
pushNotification ? Number(pushNotification?.savedmessageid) : CoreNavigator.getRequiredRouteNumberParam('id'),
);
} }
/** /**
@ -193,6 +204,27 @@ export class AddonNotificationsNotificationPage implements OnInit {
site.openInBrowserWithAutoLogin(url); site.openInBrowserWithAutoLogin(url);
} }
/**
* @inheritdoc
*/
ngOnDestroy(): void {
this.notifications?.destroy();
}
}
/**
* Helper to manage swiping within a collection of notifications.
*/
class AddonNotificationSwipeItemsManager extends CoreSwipeNavigationItemsManager {
/**
* @inheritdoc
*/
protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null {
return route.params.id;
}
} }
type AddonNotificationsNotification = AddonNotificationsNotificationToRender | AddonNotificationsNotificationData; type AddonNotificationsNotification = AddonNotificationsNotificationToRender | AddonNotificationsNotificationData;