Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions www/addons/messages/controllers/discussions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
65 changes: 55 additions & 10 deletions www/addons/notifications/controllers/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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');
});
};

Expand All @@ -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');
}
});
});
2 changes: 1 addition & 1 deletion www/addons/notifications/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ angular.module('mm.addons.notifications')
cronObserver && cronObserver.off && cronObserver.off();

if ($mmPushNotificationsDelegate) {
$mmPushNotificationsDelegate.unregisterReceiveHandler('mmaMessages:sidemenu');
$mmPushNotificationsDelegate.unregisterReceiveHandler('mmaNotifications:sidemenu');
}
});
};
Expand Down