From 6536ffecadc4229d9127cebb3d8c6558974b390e Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 18 Mar 2019 10:59:36 +0100 Subject: [PATCH] MOBILE-2921 assign: Support assign push clicks --- src/addon/mod/assign/assign.module.ts | 9 ++- .../assign/providers/push-click-handler.ts | 62 +++++++++++++++++++ .../mod/forum/providers/push-click-handler.ts | 2 +- 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/addon/mod/assign/providers/push-click-handler.ts diff --git a/src/addon/mod/assign/assign.module.ts b/src/addon/mod/assign/assign.module.ts index 39a87ad9d..74fe86c4e 100644 --- a/src/addon/mod/assign/assign.module.ts +++ b/src/addon/mod/assign/assign.module.ts @@ -17,6 +17,7 @@ import { CoreCronDelegate } from '@providers/cron'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate'; import { AddonModAssignProvider } from './providers/assign'; import { AddonModAssignOfflineProvider } from './providers/assign-offline'; import { AddonModAssignSyncProvider } from './providers/assign-sync'; @@ -30,6 +31,7 @@ import { AddonModAssignPrefetchHandler } from './providers/prefetch-handler'; import { AddonModAssignSyncCronHandler } from './providers/sync-cron-handler'; import { AddonModAssignIndexLinkHandler } from './providers/index-link-handler'; import { AddonModAssignListLinkHandler } from './providers/list-link-handler'; +import { AddonModAssignPushClickHandler } from './providers/push-click-handler'; import { AddonModAssignSubmissionModule } from './submission/submission.module'; import { AddonModAssignFeedbackModule } from './feedback/feedback.module'; import { CoreUpdateManagerProvider } from '@providers/update-manager'; @@ -64,7 +66,8 @@ export const ADDON_MOD_ASSIGN_PROVIDERS: any[] = [ AddonModAssignPrefetchHandler, AddonModAssignSyncCronHandler, AddonModAssignIndexLinkHandler, - AddonModAssignListLinkHandler + AddonModAssignListLinkHandler, + AddonModAssignPushClickHandler ] }) export class AddonModAssignModule { @@ -72,13 +75,15 @@ export class AddonModAssignModule { prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModAssignPrefetchHandler, cronDelegate: CoreCronDelegate, syncHandler: AddonModAssignSyncCronHandler, updateManager: CoreUpdateManagerProvider, contentLinksDelegate: CoreContentLinksDelegate, linkHandler: AddonModAssignIndexLinkHandler, - listLinkHandler: AddonModAssignListLinkHandler) { + listLinkHandler: AddonModAssignListLinkHandler, pushNotificationsDelegate: CorePushNotificationsDelegate, + pushClickHandler: AddonModAssignPushClickHandler) { moduleDelegate.registerHandler(moduleHandler); prefetchDelegate.registerHandler(prefetchHandler); cronDelegate.register(syncHandler); contentLinksDelegate.registerHandler(linkHandler); contentLinksDelegate.registerHandler(listLinkHandler); + pushNotificationsDelegate.registerClickHandler(pushClickHandler); // Allow migrating the tables from the old app to the new schema. updateManager.registerSiteTablesMigration([ diff --git a/src/addon/mod/assign/providers/push-click-handler.ts b/src/addon/mod/assign/providers/push-click-handler.ts new file mode 100644 index 000000000..abe6fa608 --- /dev/null +++ b/src/addon/mod/assign/providers/push-click-handler.ts @@ -0,0 +1,62 @@ +// (C) Copyright 2015 Martin Dougiamas +// +// 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 { Injectable } from '@angular/core'; +import { CoreUrlUtilsProvider } from '@providers/utils/url'; +import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CorePushNotificationsClickHandler } from '@core/pushnotifications/providers/delegate'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModAssignProvider } from './assign'; + +/** + * Handler for assign push notifications clicks. + */ +@Injectable() +export class AddonModAssignPushClickHandler implements CorePushNotificationsClickHandler { + name = 'AddonModAssignPushClickHandler'; + priority = 200; + featureName = 'CoreCourseModuleDelegate_AddonModAssign'; + + constructor(private utils: CoreUtilsProvider, private assignProvider: AddonModAssignProvider, + private urlUtils: CoreUrlUtilsProvider, private courseHelper: CoreCourseHelperProvider) {} + + /** + * Check if a notification click is handled by this handler. + * + * @param {any} notification The notification to check. + * @return {boolean} Whether the notification click is handled by this handler + */ + handles(notification: any): boolean | Promise { + return this.utils.isTrueOrOne(notification.notif) && notification.moodlecomponent == 'mod_assign' && + notification.name == 'assign_notification'; + } + + /** + * Handle the notification click. + * + * @param {any} notification The notification to check. + * @return {Promise} Promise resolved when done. + */ + handleClick(notification: any): Promise { + const contextUrlParams = this.urlUtils.extractUrlParams(notification.contexturl), + courseId = Number(notification.courseid), + moduleId = Number(contextUrlParams.id); + + return this.assignProvider.invalidateContent(moduleId, courseId, notification.site).catch(() => { + // Ignore errors. + }).then(() => { + return this.courseHelper.navigateToModule(moduleId, notification.site, courseId); + }); + } +} diff --git a/src/addon/mod/forum/providers/push-click-handler.ts b/src/addon/mod/forum/providers/push-click-handler.ts index 145d5bf88..4a224a843 100644 --- a/src/addon/mod/forum/providers/push-click-handler.ts +++ b/src/addon/mod/forum/providers/push-click-handler.ts @@ -59,7 +59,7 @@ export class AddonModForumPushClickHandler implements CorePushNotificationsClick pageParams.postId = Number(contextUrlParams.urlHash.replace('p', '')); } - return this.forumProvider.invalidateDiscussionPosts(pageParams.discussionId).catch(() => { + return this.forumProvider.invalidateDiscussionPosts(pageParams.discussionId, notification.site).catch(() => { // Ignore errors. }).then(() => { return this.linkHelper.goInSite(undefined, 'AddonModForumDiscussionPage', pageParams, notification.site);