From a38fcf6d6f203af25c85f44715794ee1b6c60184 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 18 Mar 2019 13:40:21 +0100 Subject: [PATCH] MOBILE-2921 badges: Support badges push clicks --- src/addon/badges/badges.module.ts | 9 ++- .../badges/providers/push-click-handler.ts | 64 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/addon/badges/providers/push-click-handler.ts diff --git a/src/addon/badges/badges.module.ts b/src/addon/badges/badges.module.ts index 380fe0046..20163a208 100644 --- a/src/addon/badges/badges.module.ts +++ b/src/addon/badges/badges.module.ts @@ -17,8 +17,10 @@ import { AddonBadgesProvider } from './providers/badges'; import { AddonBadgesUserHandler } from './providers/user-handler'; import { AddonBadgesMyBadgesLinkHandler } from './providers/mybadges-link-handler'; import { AddonBadgesBadgeLinkHandler } from './providers/badge-link-handler'; +import { AddonBadgesPushClickHandler } from './providers/push-click-handler'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreUserDelegate } from '@core/user/providers/user-delegate'; +import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate'; // List of providers (without handlers). export const ADDON_BADGES_PROVIDERS: any[] = [ @@ -34,16 +36,19 @@ export const ADDON_BADGES_PROVIDERS: any[] = [ AddonBadgesProvider, AddonBadgesUserHandler, AddonBadgesMyBadgesLinkHandler, - AddonBadgesBadgeLinkHandler + AddonBadgesBadgeLinkHandler, + AddonBadgesPushClickHandler ] }) export class AddonBadgesModule { constructor(userDelegate: CoreUserDelegate, userHandler: AddonBadgesUserHandler, contentLinksDelegate: CoreContentLinksDelegate, myBadgesLinkHandler: AddonBadgesMyBadgesLinkHandler, - badgeLinkHandler: AddonBadgesBadgeLinkHandler) { + badgeLinkHandler: AddonBadgesBadgeLinkHandler, + pushNotificationsDelegate: CorePushNotificationsDelegate, pushClickHandler: AddonBadgesPushClickHandler) { userDelegate.registerHandler(userHandler); contentLinksDelegate.registerHandler(myBadgesLinkHandler); contentLinksDelegate.registerHandler(badgeLinkHandler); + pushNotificationsDelegate.registerClickHandler(pushClickHandler); } } diff --git a/src/addon/badges/providers/push-click-handler.ts b/src/addon/badges/providers/push-click-handler.ts new file mode 100644 index 000000000..555a99240 --- /dev/null +++ b/src/addon/badges/providers/push-click-handler.ts @@ -0,0 +1,64 @@ +// (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 { CoreUtilsProvider } from '@providers/utils/utils'; +import { CorePushNotificationsClickHandler } from '@core/pushnotifications/providers/delegate'; +import { CoreLoginHelperProvider } from '@core/login/providers/helper'; +import { AddonBadgesProvider } from './badges'; + +/** + * Handler for badges push notifications clicks. + */ +@Injectable() +export class AddonBadgesPushClickHandler implements CorePushNotificationsClickHandler { + name = 'AddonBadgesPushClickHandler'; + priority = 200; + featureName = 'CoreUserDelegate_AddonBadges'; + + constructor(private utils: CoreUtilsProvider, private badgesProvider: AddonBadgesProvider, + private loginHelper: CoreLoginHelperProvider) {} + + /** + * 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 { + // @todo: Support 'badgecreatornotice' once we receive the hash or contexturl. + if (this.utils.isTrueOrOne(notification.notif) && notification.moodlecomponent == 'moodle' && + notification.name == 'badgerecipientnotice') { + return this.badgesProvider.isPluginEnabled(notification.site); + } + + return false; + } + + /** + * Handle the notification click. + * + * @param {any} notification The notification to check. + * @return {Promise} Promise resolved when done. + */ + handleClick(notification: any): Promise { + // @todo: Go to the badge page once we receive the hash or contexturl. + + return this.badgesProvider.invalidateUserBadges(0, Number(notification.usertoid), notification.site).catch(() => { + // Ignore errors. + }).then(() => { + return this.loginHelper.redirect('AddonBadgesUserBadgesPage', {}, notification.site); + }); + } +}