arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/js/notifications.js
www/js/notifications.jsRaw
 1angular.module('app.notifications', ['pascalprecht.translate'])
 2
 3  .controller('NotificationsCtrl', function($scope, $http, $ionicLoading,
 4    $stateParams, $translate, $filter) {
 5    $scope.notifications = [];
 6    $scope.doRefresh = function() {
 7        //first, if already have notifications, set to viewed
 8        if(localStorage.getItem("cim_app_notifications")!=undefined){
 9            //console.log(localStorage.getItem("cim_app_notifications"));
10            $scope.notifications = JSON.parse(localStorage.getItem("cim_app_notifications"));
11            for(var i=0; i<$scope.notifications.length; i++){
12                $scope.notifications[i].state = "viewed";
13            }
14            localStorage.setItem("cim_app_notifications", JSON.stringify($scope.notifications));
15        }
16      $http.get(urlapi + 'notifications')
17        .then(function(data) {
18          //console.log(data); // for browser console
19          /*$scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
20          $scope.storageuser.notifications = [];
21          console.log($scope.storageuser.notifications.length);
22          localStorage.setItem("cim_app_userdata", JSON.stringify($scope.storageuser));*/
23
24          //get the storage notifications
25          if (localStorage.getItem("cim_app_notifications")) {
26            $scope.notifications = JSON.parse(localStorage.getItem("cim_app_notifications"));
27            $scope.notifications = $scope.notifications.concat(data.data); // for UI
28          } else {
29            $scope.notifications = data.data;
30          }
31          //store the notifications
32          localStorage.setItem("cim_app_notifications", JSON.stringify($scope.notifications));
33
34          //now clean the notification number that is showed in the menu and main page, from the storageuser.notifications
35          /*$scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
36          $scope.storageuser.pendentnotifications = [];
37          localStorage.setItem("cim_app_userdata", JSON.stringify($scope.storageuser));*/
38          $scope.$broadcast('scroll.refreshComplete'); //refresher stop
39
40        }, function(data) {
41          console.log('data error');
42          $scope.$broadcast('scroll.refreshComplete'); //refresher stop
43          $ionicLoading.show({
44            template: 'Error connecting server',
45            noBackdrop: true,
46            duration: 2000
47          });
48
49        });
50    };
51    $scope.doRefresh();
52  });