diff --git a/src/js/controllers/appCtrl.js b/src/js/controllers/appCtrl.js index e679ed023f..b45c019759 100644 --- a/src/js/controllers/appCtrl.js +++ b/src/js/controllers/appCtrl.js @@ -14,7 +14,7 @@ angular.module('starter')// Parent Controller - This controller runs before ever }); $scope.$on('$ionicView.afterEnter', function(e){ qmLog.debug($scope.controller_name + ".afterEnter so posting queued notifications if any"); - qm.notifications.post(); + qmService.syncTrackingReminderNotifications(); qmService.refreshUserUsingAccessTokenInUrlIfNecessary(); $rootScope.setMicAndSpeechEnabled(qm.mic.getMicEnabled()); qm.chatButton.setZohoChatButtonZIndex(); diff --git a/src/js/controllers/remindersInboxCtrl.js b/src/js/controllers/remindersInboxCtrl.js index fba10b90fb..9c1cc53eb5 100644 --- a/src/js/controllers/remindersInboxCtrl.js +++ b/src/js/controllers/remindersInboxCtrl.js @@ -60,7 +60,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", }); $scope.stateParams = $stateParams; qmService.actionSheet.setDefaultActionSheet(function(){ - $scope.refreshTrackingReminderNotifications(); + $scope.syncTrackingReminderNotifications(); }, getVariableCategoryName()); qmService.splash.hideSplashScreen(); }); @@ -68,7 +68,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", qmLog.info('RemindersInboxCtrl afterEnter: ' + window.location.href); setPageTitle(); // Setting title afterEnter doesn't fix cutoff on Android if(needToRefresh()){ - $scope.refreshTrackingReminderNotifications(); + $scope.syncTrackingReminderNotifications(); } if($rootScope.platform.isWeb){ qm.webNotifications.registerServiceWorker(); @@ -77,7 +77,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", }); $scope.$on('$ionicView.beforeLeave', function(){ qmLog.debug('RemindersInboxCtrl beforeLeave'); - qm.notifications.post(); + qmService.syncTrackingReminderNotifications(); }); $scope.$on('$ionicView.afterLeave', function(){ qmLog.debug('RemindersInboxCtrl afterLeave'); @@ -105,12 +105,11 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", function autoRefresh(){ $timeout(function(){ if($state.current.name.toLowerCase().indexOf('inbox') !== -1){ - $scope.refreshTrackingReminderNotifications(); + $scope.syncTrackingReminderNotifications(); autoRefresh(); } }, 30 * 60 * 1000); } - var setPageTitle = function(){ if(getVariableCategoryName() === 'Treatments'){ $scope.state.title = 'Overdue Meds'; @@ -148,7 +147,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", if(qm.notifications.getNumberInGlobalsOrLocalStorage(cat)){ getTrackingReminderNotifications(); }else{ - $scope.refreshTrackingReminderNotifications(); + $scope.syncTrackingReminderNotifications(); } } } @@ -178,17 +177,6 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", }); } } - function refreshNotificationsForCategory(cat) { - qmLog.info('Falling back to getTrackingReminderNotificationsFromApi request for category ' + cat); - qmService.refreshTrackingReminderNotifications({ - variableCategoryName: cat, - onlyPast: true - }, function (response) { - qmLog.info('getTrackingReminderNotificationsFromApi response for ' + cat + - ': ' + JSON.stringify(response)); - addNotificationsToScope(response.data) - }); - } function getNumberOfDisplayedNotifications() { var total = 0; var dividers = $scope.notificationDividers; @@ -232,15 +220,13 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", function getFallbackInboxContentIfNecessary(){ var num = getNumberOfDisplayedNotifications(); if(!num && !$scope.state.loading){ - var cat = getVariableCategoryName(); - if(cat){refreshNotificationsForCategory(cat);} getFavorites(); getDiscoveries(); } } var closeWindowIfNecessary = function(){ if($state.current.name === "app.remindersInboxCompact" && !getNumberOfDisplayedNotifications()){ - $scope.refreshTrackingReminderNotifications(); + $scope.syncTrackingReminderNotifications(); window.close(); } }; @@ -432,19 +418,6 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", logNotificationDividers($scope.notificationDividers); }) } - var getFilteredTodayTrackingReminderNotifications = function(){ - qmService.getTodayTrackingReminderNotificationsDeferred(getVariableCategoryName()) - .then(function(trackingReminderNotifications){ - addNotificationsToScope(trackingReminderNotifications) - getFallbackInboxContentIfNecessary(); - hideInboxLoader(); - }, function(error){ - getFallbackInboxContentIfNecessary(); - qmLog.error(error); - qmLog.error('failed to get reminder notifications!'); - hideInboxLoader(); - }); - }; $rootScope.$on('broadcastGetTrackingReminderNotifications', function(){ qmLog.info('getTrackingReminderNotifications broadcast received..'); getFilteredTrackingReminderNotificationsFromLocalStorage(); @@ -465,16 +438,16 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", } }, 10000); }; - $scope.refreshTrackingReminderNotifications = function(params){ + $scope.syncTrackingReminderNotifications = function(params){ showLoader(); - qmService.refreshTrackingReminderNotifications(params).then(function(){ + qmService.syncTrackingReminderNotifications(params).then(function(){ getTrackingReminderNotifications(); if(!getNumberOfDisplayedNotifications()){ getFallbackInboxContentIfNecessary(); } }, function(error){ getFallbackInboxContentIfNecessary(); - qmLog.info('$scope.refreshTrackingReminderNotifications: ', error); + qmLog.info('$scope.syncTrackingReminderNotifications: ', error); hideInboxLoader(); }); }; diff --git a/src/js/popup.js b/src/js/popup.js index ebd31ed167..c383c9859f 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -243,7 +243,7 @@ function updateQuestion(variableName){ qmLog.error("Asking " + questionText + "!", "qmPopup.trackingReminderNotification is: " + JSON.stringify(qmPopup.trackingReminderNotification), {trackingReminderNotification: qmPopup.trackingReminderNotification}); } - if (qmPopup.trackingReminderNotification.valence === "positive" || + if (qmPopup.trackingReminderNotification.valence === "positive" || qmPopup.trackingReminderNotification.valence === "negative") { numericRatingButtons().style.display = "none"; faceRatingButtons().style.display = "block"; @@ -312,7 +312,7 @@ document.addEventListener('DOMContentLoaded', function(){ }else{ qmLog.pushDebug("popup addEventListener: Calling hidePopup..."); hidePopup(); - qm.notifications.refreshNotifications(updateQuestion, qm.notifications.closePopup); + qm.notifications.syncTrackingReminderNotifications(updateQuestion, qm.notifications.closePopup); } qmLog.pushDebug("popup addEventListener: calling setFaceButtonListeners..."); setFaceButtonListeners(); diff --git a/src/js/qmHelpers.js b/src/js/qmHelpers.js index d2ce2a9a9f..ede89102be 100644 --- a/src/js/qmHelpers.js +++ b/src/js/qmHelpers.js @@ -5517,7 +5517,7 @@ var qm = { refreshIfEmpty: function(successHandler, errorHandler){ if(!qm.notifications.getNumberInGlobalsOrLocalStorage()){ qm.qmLog.info('No notifications in local storage'); - qm.notifications.refreshNotifications(successHandler, errorHandler); + qm.notifications.syncTrackingReminderNotifications(successHandler, errorHandler); return true; } qm.qmLog.info(qm.notifications.getNumberInGlobalsOrLocalStorage() + ' notifications in local storage'); @@ -5528,7 +5528,7 @@ var qm = { qm.qmLog.info("qm.notifications.refreshIfEmptyOrStale"); if(!qm.notifications.getNumberInGlobalsOrLocalStorage() || qm.notifications.getSecondsSinceLastNotificationsRefresh() > 3600){ qm.qmLog.info('Refreshing notifications because empty or last refresh was more than an hour ago'); - qm.notifications.refreshNotifications(callback); + qm.notifications.syncTrackingReminderNotifications(callback); }else{ qm.qmLog.info('Not refreshing notifications because last refresh was last than an hour ago and we have notifications in local storage'); if(callback){ @@ -5620,7 +5620,7 @@ var qm = { }else{ console.info('No rating notifications for popup'); qm.notifications.getLastNotificationsRefreshTime(); - qm.notifications.refreshNotifications(); + qm.notifications.syncTrackingReminderNotifications(); return null; } }, @@ -5628,31 +5628,8 @@ var qm = { return qm.storage.deleteByProperty(qm.items.trackingReminderNotifications, 'variableName', variableName); }, promise: null, - refreshNotifications: function(successHandler, errorHandler, options){ - var route = qm.apiPaths.trackingReminderNotificationsPast; - qm.api.getRequestUrl(route, function(url){ - // Can't use QM SDK in service worker - qm.api.getViaXhrOrFetch(url, function(response){ - if(!response){ - qm.qmLog.error("No response from " + url); - if(errorHandler){ - errorHandler("No response from " + url); - } - return; - } - if(response.status === 401){ - qm.chrome.showSignInNotification(); - }else{ - qm.storage.setTrackingReminderNotifications(response.data); - if(successHandler){ - successHandler(response.data); - } - } - }) - }, options); - }, refreshAndShowPopupIfNecessary: function(notificationParams){ - qm.notifications.refreshNotifications(notificationParams, function(trackingReminderNotifications){ + qm.notifications.syncTrackingReminderNotifications(function(response){ var uniqueNotification = qm.notifications.getMostRecentUniqueNotificationNotInSyncQueue(); function objectLength(obj){ var result = 0; @@ -5664,6 +5641,7 @@ var qm = { } return result; } + var trackingReminderNotifications = qm.notifications.getFromGlobalsOrLocalStorage(); var numberOfWaitingNotifications = objectLength(trackingReminderNotifications); if(uniqueNotification){ function getChromeRatingNotificationParams(trackingReminderNotification){ @@ -5769,7 +5747,7 @@ var qm = { if(!successHandler){ return null; } - qm.notifications.refreshNotifications(function(notifications){ + qm.notifications.syncTrackingReminderNotifications(function(response){ var notification = qm.notifications.getMostRecentNotification(); if(notification){ successHandler(notification); @@ -5781,7 +5759,7 @@ var qm = { schedulePost: function(delayInMilliseconds){ var queue = qm.storage.getItem(qm.items.notificationsSyncQueue); if(queue && queue.length > 10){ - qm.notifications.post(); + qm.notifications.syncTrackingReminderNotifications(); return; } if(!delayInMilliseconds){ @@ -5797,7 +5775,7 @@ var qm = { } setTimeout(function(){ qm.qmLog.info("Notifications sync countdown completed. Syncing now... "); - qm.notifications.post(); + qm.notifications.syncTrackingReminderNotifications(); }, delayInMilliseconds); }else{ if(!qm.platform.isMobile()){ // Better performance @@ -5826,8 +5804,8 @@ var qm = { qm.notifications.deleteByVariableName(n.variableName); qm.notifications.addToSyncQueue(n); }, - post: function(successHandler, errorHandler){ - qm.qmLog.debug("Called postNotifications..."); + syncTrackingReminderNotifications: function(successHandler, errorHandler){ + qm.qmLog.debug("Called syncTrackingReminderNotifications..."); var notifications = qm.storage.getItem(qm.items.notificationsSyncQueue); qm.storage.removeItem(qm.items.notificationsSyncQueue); qm.storage.removeItem(qm.items.trackingReminderNotificationSyncScheduled); @@ -5851,21 +5829,29 @@ var qm = { }) qm.api.postToQuantiModo(body, 'v3/trackingReminderNotifications', function(response){ - var measurements = response.measurements; - if(!measurements && response.data){measurements = response.data.measurements;} + if(!response){ + var err = "No response from postToQuantiModo(body, 'v3/trackingReminderNotifications"; + if(errorHandler){ + errorHandler(err); + return; + } else { + throw err; + } + } + var measurements = response.measurements || response.data.measurements; if(measurements){qm.measurements.addMeasurementsToMemory(measurements);} + var trackingReminderNotifications = response.trackingReminderNotifications || response.data.trackingReminderNotifications; + if(trackingReminderNotifications){qm.storage.setTrackingReminderNotifications(notifications);} if(successHandler){successHandler(response);} }, function(response){ - if(!response.success){ - qm.qmLog.error(response.message) - var newNotificationsSyncQueue = qm.storage.getItem(qm.items.notificationsSyncQueue); - if(newNotificationsSyncQueue){notifications = notifications.concat(newNotificationsSyncQueue);} - qm.storage.setItem(qm.items.notificationsSyncQueue, notifications); - if(errorHandler){errorHandler(response.message || response.error);} - } else{ // This happens when the error is a message saying the notification was already deleted - // so we don't want to put notifications back in queue - qm.qmLog.warn(response.message) - } + qm.qmLog.error(response.message) + // This happens when the error is a message saying the notification was already deleted + // so we don't want to put notifications back in queue + // Don't return to queue or we cause an infinite loop if we get a no changes error + // var newNotificationsSyncQueue = qm.storage.getItem(qm.items.notificationsSyncQueue); + // if(newNotificationsSyncQueue){notifications = notifications.concat(newNotificationsSyncQueue);} + // qm.storage.setItem(qm.items.notificationsSyncQueue, notifications); + if(errorHandler){errorHandler(response.message || response.error);} }); }, skip: function(trackingReminderNotification){ @@ -9519,6 +9505,7 @@ var qm = { } }, userVariables: { + defaultLimit: 20, updateLatestMeasurementTime: function(variableName, lastValue){ qm.storage.getUserVariableByName(variableName, true, lastValue); }, @@ -9531,7 +9518,7 @@ var qm = { params.sort = '-latestMeasurementTime'; } if(!params.limit){ - params.limit = 50; + params.limit = qm.userVariables.defaultLimit; } params = qm.api.addGlobalParams(params); var cacheKey = 'getUserVariablesFromApi'; diff --git a/src/js/services/qmService.js b/src/js/services/qmService.js index 1e57fbfcc2..b9bf7b8795 100644 --- a/src/js/services/qmService.js +++ b/src/js/services/qmService.js @@ -2880,7 +2880,8 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i qmLog.info("Got " + notifications.length + " notifications to from postTrackingRemindersDeferred response", null, {notifications: notifications}); - putTrackingReminderNotificationsInLocalStorageAndUpdateInbox(notifications); + qm.storage.setTrackingReminderNotifications(notifications); + qmService.notifications.broadcastGetTrackingReminderNotifications(); } }else{ qmLog.error("No postTrackingRemindersToApi response.data!"); @@ -2890,12 +2891,12 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i deferred.reject(error); }); }; - qm.notifications.post(function(){ + qmService.syncTrackingReminderNotifications().then(function(){ postTrackingRemindersToApiAndHandleResponse(); - }, function(error){ + }, function (err){ postTrackingRemindersToApiAndHandleResponse(); deferred.reject(error); - }); + }) }else{ qmLog.info('syncTrackingReminders: trackingReminderSyncQueue empty so just fetching trackingReminders from API', null); qm.reminderHelper.getTrackingRemindersFromApi({force: force}, function(reminders){ @@ -5067,14 +5068,6 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i backgroundGeolocation.stop(); } }; - var putTrackingReminderNotificationsInLocalStorageAndUpdateInbox = function(notifications){ - qmService.storage.setItem(qm.items.lastGotNotificationsAtMilliseconds, - window.qm.timeHelper.getUnixTimestampInMilliseconds()); - qm.api.addVariableCategoryAndUnit(notifications); - qm.storage.setTrackingReminderNotifications(notifications); - qmService.notifications.broadcastGetTrackingReminderNotifications(); - return notifications; - }; qmService.getSecondsSinceWeLastGotNotifications = function(){ var lastGotNotificationsAtMilliseconds = qm.storage.getItem(qm.items.lastGotNotificationsAtMilliseconds); if(!lastGotNotificationsAtMilliseconds){ @@ -5097,55 +5090,25 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i }); return deferred.promise; }; - qmService.getTodayTrackingReminderNotificationsDeferred = function(variableCategoryName){ - var params = { - minimumReminderTimeUtcString: qmService.getLocalMidnightInUtcString(), - maximumReminderTimeUtcString: qmService.getTomorrowLocalMidnightInUtcString(), - sort: 'reminderTime' - }; - if(variableCategoryName){params.variableCategoryName = variableCategoryName;} + qmService.syncTrackingReminderNotifications = function(params){ var deferred = $q.defer(); - qmService.getTrackingReminderNotificationsFromApi(params, function(notifications){ - notifications = putTrackingReminderNotificationsInLocalStorageAndUpdateInbox(notifications); - if(notifications.length){ - checkHoursSinceLastPushNotificationReceived(); - qmService.notifications.getDrawOverAppsPopupPermissionIfNecessary(); - } + if(params && params.noCache){qmService.notificationsPromise = false;} + if(qmService.notificationsPromise){return qmService.notificationsPromise;} + qm.notifications.syncTrackingReminderNotifications(function(response){ + var notifications = qm.notifications.getFromGlobalsOrLocalStorage(); + if(notifications.length && $rootScope.platform.isMobile && getDeviceTokenToSync()){qmService.registerDeviceToken();} + qmService.notifications.broadcastGetTrackingReminderNotifications(); + if($rootScope.platform.isAndroid){qmService.notifications.showAndroidPopupForMostRecentNotification(true);} + qm.chrome.updateChromeBadge(notifications.length); + qmService.notificationsPromise = false; deferred.resolve(notifications); }, function(error){ qmLog.error(error); + qmService.notificationsPromise = false; deferred.reject(error); }); - return deferred.promise; - }; - qmService.refreshTrackingReminderNotifications = function(params){ - var deferred = $q.defer(); - qm.notifications.post(function(){ - var currentDateTimeInUtcStringPlus5Min = qmService.getCurrentDateTimeInUtcStringPlusMin(5); - if(!params){params = {};} - params.reminderTime = '(lt)' + currentDateTimeInUtcStringPlus5Min; - params.sort = '-reminderTime'; - params.limit = qm.notifications.limit; // Limit to notifications in the scope instead of here to improve inbox performance - qmService.getTrackingReminderNotificationsFromApi(params, function(notifications){ - notifications = putTrackingReminderNotificationsInLocalStorageAndUpdateInbox(notifications); - if(notifications.length && $rootScope.platform.isMobile && getDeviceTokenToSync()){ - qmService.registerDeviceToken(); - } - if($rootScope.platform.isAndroid){qmService.notifications.showAndroidPopupForMostRecentNotification(true);} - qm.chrome.updateChromeBadge(notifications.length); - qmService.refreshingTrackingReminderNotifications = false; - deferred.resolve(notifications); - }, function(error){ - qmLog.error(error); - qmService.refreshingTrackingReminderNotifications = false; - deferred.reject(error); - }); - }, function(error){ - qmLog.error(error); - qmService.refreshingTrackingReminderNotifications = false; - deferred.reject(error); - }); - return deferred.promise; + setTimeout(function(){qmService.notificationsPromise = false;}, 15000) + return qmService.notificationsPromise = deferred.promise; }; qmService.getTrackingReminderByIdDeferred = function(reminderId){ var deferred = $q.defer(); @@ -5163,51 +5126,6 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i }); return deferred.promise; }; - qmService.getCurrentTrackingReminderNotificationsFromApi = function(category, today){ - var localMidnightInUtcString = qmService.getLocalMidnightInUtcString(); - var currentDateTimeInUtcString = qmService.getCurrentDateTimeInUtcString(); - var params = {}; - if(today && !category){ - var reminderTime = '(gt)' + localMidnightInUtcString; - params = {reminderTime: reminderTime, sort: 'reminderTime'}; - } - if(!today && category){ - params = {variableCategoryName: category, reminderTime: '(lt)' + currentDateTimeInUtcString}; - } - if(today && category){ - params = { - reminderTime: '(gt)' + localMidnightInUtcString, - variableCategoryName: category, - sort: 'reminderTime' - }; - } - if(!today && !category){ - params = {reminderTime: '(lt)' + currentDateTimeInUtcString}; - } - var deferred = $q.defer(); - var successHandler = function(trackingReminderNotifications){ - if(trackingReminderNotifications.success){ - deferred.resolve(trackingReminderNotifications.data); - }else{ - deferred.reject("error"); - } - }; - var errorHandler = function(error){ - qmLog.error(error); - deferred.reject(error); - }; - if(!qm.api.configureClient('getCurrentTrackingReminderNotificationsFromApi', errorHandler, params)){ - return false; - } - var apiInstance = new Quantimodo.RemindersApi(); - function callback(error, data, response){ - qmSdkApiResponseHandler(error, data, response, successHandler, errorHandler, params, 'getCurrentTrackingReminderNotificationsFromApi'); - } - params = qm.api.addGlobalParams(params); - apiInstance.getTrackingReminderNotifications(params, callback); - //qmService.get('api/v3/trackingReminderNotifications', ['variableCategoryName', 'id', 'sort', 'limit','offset','updatedAt', 'reminderTime'], params, successHandler, errorHandler); - return deferred.promise; - }; qmService.storage.deleteTrackingReminder = function(reminderToDelete){ var allTrackingReminders = qm.storage.getItem('trackingReminders'); var trackingRemindersToKeep = []; @@ -7868,7 +7786,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i qmService.notifications.showAndroidPopupForMostRecentNotification(); }else{ qmLog.pushDebug('window.overApps for popups is undefined! '); - qmService.refreshTrackingReminderNotifications(300).then(function(){ + qmService.syncTrackingReminderNotifications({}).then(function(){ qmLog.pushDebug('push.on.notification: successfully refreshed notifications'); }, function(error){ qmLog.error('push.on.notification: ', error); diff --git a/src/templates/input/action-buttons-fragment.html b/src/templates/input/action-buttons-fragment.html index 9e3855e0a5..6a8e8b74ec 100644 --- a/src/templates/input/action-buttons-fragment.html +++ b/src/templates/input/action-buttons-fragment.html @@ -1,7 +1,7 @@
Recent Values