summaryrefslogtreecommitdiff
path: root/www/js/NewsCtrl.js
blob: ba45fb0e1ff2d858f4ed493d07da1f3fbf78e949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console,moment*/

angular.module('zmApp.controllers').controller('zmApp.NewsCtrl', ['$scope', '$rootScope', '$ionicModal', 'NVRDataModel', '$ionicSideMenuDelegate', '$ionicHistory', '$state', '$http', 'zm', function ($scope, $rootScope, $ionicModal, NVRDataModel, $ionicSideMenuDelegate, $ionicHistory, $state, $http, zm) {
  $scope.openMenu = function () {
    $ionicSideMenuDelegate.toggleLeft();
  };

  //----------------------------------------------------------------
  // Alarm notification handling
  //----------------------------------------------------------------
  $scope.handleAlarms = function () {
    $rootScope.isAlarm = !$rootScope.isAlarm;
    if (!$rootScope.isAlarm) {
      $rootScope.alarmCount = "0";
      $ionicHistory.nextViewOptions({
        disableBack: true
      });
      $state.go("app.events", {
        "id": 0,
        "playEvent": false
      }, {
        reload: true
      });
      return;
    }
  };

  //-------------------------------------------------------------------------
  // Lets make sure we set screen dim properly as we enter
  // The problem is we enter other states before we leave previous states
  // from a callback perspective in ionic, so we really can't predictably
  // reset power state on exit as if it is called after we enter another
  // state, that effectively overwrites current view power management needs
  //------------------------------------------------------------------------
  $scope.$on('$ionicView.enter', function () {
    // console.log("**VIEW ** News Ctrl Entered");
    NVRDataModel.setAwake(false);

  });

  $scope.isUnread = function (itemdate) {
    var lastDate = NVRDataModel.getLatestBlogPostChecked();
    //console.log ("BLOG DATE="+itemdate+"  LAST DATE="+lastDate);
    //get("latestBlogPostChecked");
    if (!lastDate) return true;
    var mLastDate = moment(lastDate);
    var mItemDate = moment(itemdate);
    //var unread = mItemDate.diff(mLastDate) >0) ? true:false;
    //console.log (unread);
    return (mItemDate.diff(mLastDate, 'seconds') > 0) ? true : false;

  };

  $scope.loadPost = function (item, itemdate) {
    var lastDate =
      NVRDataModel.getLatestBlogPostChecked(); //zmStorageService.get("latestBlogPostChecked");

    if (!lastDate) {
      NVRDataModel.debug("First time checking blog posts, I see");
      NVRDataModel.setLatestBlogPostChecked(itemdate);
      //zmStorageService.set("latestBlogPostChecked", itemdate);
    } else {
      NVRDataModel.debug("last  post checked is " + lastDate);
      NVRDataModel.debug("current post dated is " + itemdate);

      var mLastDate = moment(lastDate);
      var mItemDate = moment(itemdate);
      if (mItemDate.diff(mLastDate, 'seconds') > 0) {
        NVRDataModel.debug("Updating lastDate to this post");

        NVRDataModel.setLatestBlogPostChecked(itemdate); //zmStorageService.set("latestBlogPostChecked", itemdate);

        if (itemdate == $scope.newsItems[0].date) {
          // we are reading the latest post
          $rootScope.newBlogPost = "";
        }
      }

    }

    window.open(item, '_blank', 'location=yes');
    return false;
  };

  $scope.newsItems = [];


  $http.get(zm.blogUrl, {
      responseType: 'text'
    })
    .then(function (datastr) {
      datastr = datastr.data;
      var trunc = "])}while(1);</x>";
      datastr = datastr.substr(trunc.length);
      // console.log ("DATA:"+data);
      // 
      var data = JSON.parse(datastr);
      for (var i = 0; i < data.payload.posts.length; i++) {
        $scope.newsItems.push({
          title: data.payload.posts[i].title,
          url: "https://medium.com/zmninja/" + data.payload.posts[i].uniqueSlug,
          date: moment(data.payload.posts[i].createdAt).format("YYYY-MM-DD HH:mm:ss")
        });
      }

    });

}]);