You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.4 KiB

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