Skip to content

Commit 993a7a0

Browse files
authored
Merge pull request #866 from crazyserver/MOBILE-1787-1
MOBILE-1787 notifications: Observe events on notifications list
2 parents ef05fd8 + c095a09 commit 993a7a0

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

www/addons/messages/controllers/discussions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ angular.module('mm.addons.messages')
115115
$mmPushNotificationsDelegate.registerReceiveHandler('mmaMessages:discussions', function(notification) {
116116
if ($mmUtil.isFalseOrZero(notification.notif)) {
117117
// New message received. If it's from current site, refresh the data.
118-
if (notification.site == $mmSite.getId()) {
119-
$scope.loaded = false;
118+
if (notification.site == siteId) {
120119
refreshData();
121120
}
122121
}

www/addons/notifications/controllers/list.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ angular.module('mm.addons.notifications')
2121
* @ngdoc controller
2222
* @name mmaNotificationsListCtrl
2323
*/
24-
.controller('mmaNotificationsListCtrl', function($scope, $mmUtil, $mmaNotifications, mmaNotificationsListLimit,
25-
mmUserProfileState, $q, $mmEvents, $mmSite, mmaNotificationsReadChangedEvent) {
24+
.controller('mmaNotificationsListCtrl', function($scope, $mmUtil, $mmaNotifications, mmaNotificationsListLimit, $mmAddonManager,
25+
mmUserProfileState, $q, $mmEvents, $mmSite, mmaNotificationsReadChangedEvent, mmaNotificationsReadCronEvent, $state) {
2626

2727
var readCount = 0,
28-
unreadCount = 0;
28+
unreadCount = 0,
29+
siteId = $mmSite.getId(),
30+
$mmPushNotificationsDelegate = $mmAddonManager.get('$mmPushNotificationsDelegate'),
31+
cronObserver;
2932

3033
$scope.notifications = [];
3134
$scope.userStateName = mmUserProfileState;
@@ -80,12 +83,44 @@ angular.module('mm.addons.notifications')
8083
$scope.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading.
8184
});
8285
}
86+
8387
fetchNotifications().finally(function() {
8488
$scope.notificationsLoaded = true;
8589
});
8690

91+
cronObserver = $mmEvents.on(mmaNotificationsReadCronEvent, function(data) {
92+
if ($state.current.name == 'site.notifications' && data && (data.siteid == siteId || !data.siteid)) {
93+
refreshData();
94+
}
95+
});
96+
97+
// If a message push notification is received, refresh the view.
98+
if ($mmPushNotificationsDelegate) {
99+
$mmPushNotificationsDelegate.registerReceiveHandler('mmaNotifications:discussions', function(notification) {
100+
if ($state.current.name == 'site.notifications' && $mmUtil.isTrueOrOne(notification.notif) &&
101+
notification.site == siteId) {
102+
// New notification received. If it's from current site, refresh the data.
103+
refreshData();
104+
}
105+
});
106+
}
107+
108+
// Refresh when entering again.
109+
var skip = true;
110+
$scope.$on('$ionicView.enter', function() {
111+
if (skip) {
112+
skip = false;
113+
return;
114+
}
115+
$scope.notificationsLoaded = false;
116+
refreshData().finally(function() {
117+
$scope.notificationsLoaded = true;
118+
});
119+
});
120+
87121
// Mark notifications as read.
88122
function markNotificationsAsRead(notifications) {
123+
// Only mark as read if we are in the state.
89124
if (notifications.length > 0) {
90125
var promises = [];
91126

@@ -96,19 +131,21 @@ angular.module('mm.addons.notifications')
96131

97132
$q.all(promises).finally(function() {
98133
$mmaNotifications.invalidateNotificationsList().finally(function() {
99-
$mmEvents.trigger(mmaNotificationsReadChangedEvent, {
100-
siteid: $mmSite.getId()
101-
});
134+
$mmEvents.trigger(mmaNotificationsReadChangedEvent, {siteid: siteId});
102135
});
103136
});
104137
}
105138
}
106139

140+
function refreshData() {
141+
return $mmaNotifications.invalidateNotificationsList().finally(function() {
142+
return fetchNotifications(true);
143+
});
144+
}
145+
107146
$scope.refreshNotifications = function() {
108-
$mmaNotifications.invalidateNotificationsList().finally(function() {
109-
fetchNotifications(true).finally(function() {
110-
$scope.$broadcast('scroll.refreshComplete');
111-
});
147+
refreshData().finally(function() {
148+
$scope.$broadcast('scroll.refreshComplete');
112149
});
113150
};
114151

@@ -117,4 +154,12 @@ angular.module('mm.addons.notifications')
117154
$scope.$broadcast('scroll.infiniteScrollComplete');
118155
});
119156
};
157+
158+
$scope.$on('$destroy', function() {
159+
cronObserver && cronObserver.off && cronObserver.off();
160+
161+
if ($mmPushNotificationsDelegate) {
162+
$mmPushNotificationsDelegate.unregisterReceiveHandler('mmaNotifications:discussions');
163+
}
164+
});
120165
});

www/addons/notifications/services/handlers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ angular.module('mm.addons.notifications')
113113
cronObserver && cronObserver.off && cronObserver.off();
114114

115115
if ($mmPushNotificationsDelegate) {
116-
$mmPushNotificationsDelegate.unregisterReceiveHandler('mmaMessages:sidemenu');
116+
$mmPushNotificationsDelegate.unregisterReceiveHandler('mmaNotifications:sidemenu');
117117
}
118118
});
119119
};

0 commit comments

Comments
 (0)