@@ -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} ) ;
0 commit comments