From c095a0970e2947bf2b9ee8a60cb5b4ac6755e540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 27 Jan 2017 14:13:39 +0100 Subject: [PATCH] MOBILE-1787 notifications: Observe events on notifications list --- .../messages/controllers/discussions.js | 3 +- www/addons/notifications/controllers/list.js | 65 ++++++++++++++++--- www/addons/notifications/services/handlers.js | 2 +- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/www/addons/messages/controllers/discussions.js b/www/addons/messages/controllers/discussions.js index 18750a8a6c5..6cd5e4cf8ec 100644 --- a/www/addons/messages/controllers/discussions.js +++ b/www/addons/messages/controllers/discussions.js @@ -115,8 +115,7 @@ angular.module('mm.addons.messages') $mmPushNotificationsDelegate.registerReceiveHandler('mmaMessages:discussions', function(notification) { if ($mmUtil.isFalseOrZero(notification.notif)) { // New message received. If it's from current site, refresh the data. - if (notification.site == $mmSite.getId()) { - $scope.loaded = false; + if (notification.site == siteId) { refreshData(); } } diff --git a/www/addons/notifications/controllers/list.js b/www/addons/notifications/controllers/list.js index 01b02796029..763cf9e4d8f 100644 --- a/www/addons/notifications/controllers/list.js +++ b/www/addons/notifications/controllers/list.js @@ -21,11 +21,14 @@ angular.module('mm.addons.notifications') * @ngdoc controller * @name mmaNotificationsListCtrl */ -.controller('mmaNotificationsListCtrl', function($scope, $mmUtil, $mmaNotifications, mmaNotificationsListLimit, - mmUserProfileState, $q, $mmEvents, $mmSite, mmaNotificationsReadChangedEvent) { +.controller('mmaNotificationsListCtrl', function($scope, $mmUtil, $mmaNotifications, mmaNotificationsListLimit, $mmAddonManager, + mmUserProfileState, $q, $mmEvents, $mmSite, mmaNotificationsReadChangedEvent, mmaNotificationsReadCronEvent, $state) { var readCount = 0, - unreadCount = 0; + unreadCount = 0, + siteId = $mmSite.getId(), + $mmPushNotificationsDelegate = $mmAddonManager.get('$mmPushNotificationsDelegate'), + cronObserver; $scope.notifications = []; $scope.userStateName = mmUserProfileState; @@ -80,12 +83,44 @@ angular.module('mm.addons.notifications') $scope.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. }); } + fetchNotifications().finally(function() { $scope.notificationsLoaded = true; }); + cronObserver = $mmEvents.on(mmaNotificationsReadCronEvent, function(data) { + if ($state.current.name == 'site.notifications' && data && (data.siteid == siteId || !data.siteid)) { + refreshData(); + } + }); + + // If a message push notification is received, refresh the view. + if ($mmPushNotificationsDelegate) { + $mmPushNotificationsDelegate.registerReceiveHandler('mmaNotifications:discussions', function(notification) { + if ($state.current.name == 'site.notifications' && $mmUtil.isTrueOrOne(notification.notif) && + notification.site == siteId) { + // New notification received. If it's from current site, refresh the data. + refreshData(); + } + }); + } + + // Refresh when entering again. + var skip = true; + $scope.$on('$ionicView.enter', function() { + if (skip) { + skip = false; + return; + } + $scope.notificationsLoaded = false; + refreshData().finally(function() { + $scope.notificationsLoaded = true; + }); + }); + // Mark notifications as read. function markNotificationsAsRead(notifications) { + // Only mark as read if we are in the state. if (notifications.length > 0) { var promises = []; @@ -96,19 +131,21 @@ angular.module('mm.addons.notifications') $q.all(promises).finally(function() { $mmaNotifications.invalidateNotificationsList().finally(function() { - $mmEvents.trigger(mmaNotificationsReadChangedEvent, { - siteid: $mmSite.getId() - }); + $mmEvents.trigger(mmaNotificationsReadChangedEvent, {siteid: siteId}); }); }); } } + function refreshData() { + return $mmaNotifications.invalidateNotificationsList().finally(function() { + return fetchNotifications(true); + }); + } + $scope.refreshNotifications = function() { - $mmaNotifications.invalidateNotificationsList().finally(function() { - fetchNotifications(true).finally(function() { - $scope.$broadcast('scroll.refreshComplete'); - }); + refreshData().finally(function() { + $scope.$broadcast('scroll.refreshComplete'); }); }; @@ -117,4 +154,12 @@ angular.module('mm.addons.notifications') $scope.$broadcast('scroll.infiniteScrollComplete'); }); }; + + $scope.$on('$destroy', function() { + cronObserver && cronObserver.off && cronObserver.off(); + + if ($mmPushNotificationsDelegate) { + $mmPushNotificationsDelegate.unregisterReceiveHandler('mmaNotifications:discussions'); + } + }); }); diff --git a/www/addons/notifications/services/handlers.js b/www/addons/notifications/services/handlers.js index 8515b8c825c..916e9b07824 100644 --- a/www/addons/notifications/services/handlers.js +++ b/www/addons/notifications/services/handlers.js @@ -113,7 +113,7 @@ angular.module('mm.addons.notifications') cronObserver && cronObserver.off && cronObserver.off(); if ($mmPushNotificationsDelegate) { - $mmPushNotificationsDelegate.unregisterReceiveHandler('mmaMessages:sidemenu'); + $mmPushNotificationsDelegate.unregisterReceiveHandler('mmaNotifications:sidemenu'); } }); };