diff --git a/.travis.yml b/.travis.yml index 8612da212e..d8efacc6e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,7 +46,7 @@ before_install: - bundle install - bundle update - export LANG=en_US.UTF-8 - - nvm install 8 + - nvm install 10 - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; brew install yarn ios-sim ios-deploy imagemagick; fi - npm install -g cordova ionic gulp bower cordova-hot-code-push-cli cordova-res diff --git a/src/js/controllers/appCtrl.js b/src/js/controllers/appCtrl.js index b45c019759..2553059408 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"); - qmService.syncTrackingReminderNotifications(); + qmService.syncNotificationsIfQueued(); qmService.refreshUserUsingAccessTokenInUrlIfNecessary(); $rootScope.setMicAndSpeechEnabled(qm.mic.getMicEnabled()); qm.chatButton.setZohoChatButtonZIndex(); diff --git a/src/js/controllers/measurementAddCtrl.js b/src/js/controllers/measurementAddCtrl.js index 9ed127abcf..c1f4d9272f 100644 --- a/src/js/controllers/measurementAddCtrl.js +++ b/src/js/controllers/measurementAddCtrl.js @@ -9,9 +9,6 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti showVariableCategorySelector: false, showUnits: false, unitCategories: [], - variableCategoryName: getVariableCategoryName(), - variableCategoryObject: getVariableCategory(), - helpText: getVariableCategory().helpText, unitAbbreviatedName: '', measurement: {}, searchedUnits: [], @@ -30,6 +27,16 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti qmService.navBar.showNavigationMenuIfHideUrlParamNotSet(); qmService.rootScope.setProperty('bloodPressure', {systolicValue: null, diastolicValue: null, show: false}); $scope.state.title = 'Record a Measurement'; + setupMeasurement(); + var cat = getVariableCategory(); + if(cat){setupVariableCategory(cat);} + }); + $scope.$on('$ionicView.enter', function(e){ + qmLogService.debug('$ionicView.enter ' + $state.current.name); + qmService.hideLoader(); + qmLog.info("$ionicView.enter $scope.state.measurement is ", $scope.state.measurement); + }); + function setupMeasurement() { $scope.state.selectedDate = moment(); $scope.state.units = qm.unitHelper.getNonAdvancedUnits(); var reminderFromUrl = qm.urlHelper.getParam('trackingReminderObject', window.location.href, true); @@ -39,33 +46,34 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti var v = $stateParams.variableObject; var n = $stateParams.reminderNotification; var id = qm.urlHelper.getParam('measurementId', location.href, true); - if(tr){ + if (tr) { setupTrackingByReminder(tr); - }else if(m){ + } else if (m) { setupTrackingByMeasurement(m); - }else if(measurementFromUrl){ + } else if (measurementFromUrl) { setupTrackingByMeasurement(JSON.parse(measurementFromUrl)); - }else if(v){ + } else if (v) { setupFromVariable(v); - }else if(reminderFromUrl){ + } else if (reminderFromUrl) { setupTrackingByReminder(JSON.parse(reminderFromUrl)); - }else if(n){ + } else if (n) { setupTrackingByReminder(n); - }else if(id){ - setMeasurementVariablesByMeasurementId(id).then(function(){ - if(!$scope.state.measurementIsSetup){$scope.goBack();} + } else if (id) { + setMeasurementVariablesByMeasurementId(id).then(function () { + if (!$scope.state.measurementIsSetup) { + $scope.goBack(); + } }); - }else if($stateParams.variableName){ + } else if ($stateParams.variableName) { setupFromVariableName($stateParams.variableName); } - if(!$scope.state.measurementIsSetup){setupFromUrlParameters();} - if(!$scope.state.measurementIsSetup){setupFromVariable(qm.getPrimaryOutcomeVariable());} - }); - $scope.$on('$ionicView.enter', function(e){ - qmLogService.debug('$ionicView.enter ' + $state.current.name); - qmService.hideLoader(); - qmLog.info("$ionicView.enter $scope.state.measurement is ", $scope.state.measurement); - }); + if (!$scope.state.measurementIsSetup) { + setupFromUrlParameters(); + } + if (!$scope.state.measurementIsSetup) { + setupFromVariable(qm.getPrimaryOutcomeVariable()); + } + } var trackBloodPressure = function(){ if(!$rootScope.bloodPressure.diastolicValue || !$rootScope.bloodPressure.systolicValue){ qmService.validationFailure('Please enter both values for blood pressure.', $scope.state.measurement); @@ -142,16 +150,20 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti } }; $scope.variableCategorySelectorChange = function(variableCategoryName){ - var cat = qmService.getVariableCategoryInfo(variableCategoryName); + var cat = qm.variableCategoryHelper.findVariableCategory(variableCategoryName); setupUnit(cat.defaultUnitAbbreviatedName); $scope.state.defaultValuePlaceholderText = 'Enter a value'; $scope.state.defaultValueLabel = 'Value'; setupVariableCategory(variableCategoryName); }; var setupVariableCategory = function(variableCategoryName){ + var cat = getVariableCategory(variableCategoryName); qmLogService.debug($state.current.name + ': ' + 'variableCategoryName is ' + variableCategoryName); - $scope.state.measurement.variableCategoryName = variableCategoryName; - var cat = getVariableCategory(); + if($scope.state.measurement){ + $scope.state.measurement.variableCategoryName = cat.name; + } + $scope.state.variableCategoryObject = cat; + $scope.state.helpText = cat.helpText; $scope.state.title = "Add Measurement"; $scope.state.measurementSynonymSingularLowercase = cat.measurementSynonymSingularLowercase; if(cat.defaultValueLabel){$scope.state.defaultValueLabel = cat.defaultValueLabel;} @@ -183,8 +195,9 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti $scope.state.measurement.unitAbbreviatedName = unitAbbreviatedName; $scope.state.measurement = qm.unitHelper.updateAllUnitPropertiesOnObject(unitAbbreviatedName, $scope.state.measurement); qmLog.info("Setting $scope.state.measurement to ", $scope.state.measurement); + qm.unitHelper.setInputType($scope.state.measurement); + $scope.state.units = qm.unitHelper.getUnitArrayContaining(unitAbbreviatedName); } - setupValueFieldType(unitAbbreviatedName, valence); } $scope.selectPrimaryOutcomeVariableValue = function($event, newValue){ // remove any previous primary outcome variables if present @@ -245,7 +258,7 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti if(v.unitAbbreviatedName){ setupUnit(v.unitAbbreviatedName, v.valence); }else if(v.variableCategoryName){ - var category = qmService.getVariableCategoryInfo(v.variableCategoryName); + var category = qm.variableCategoryHelper.findVariableCategory(v); setupUnit(category.defaultUnitAbbreviatedName, v.valence); } var m = qm.measurements.newMeasurement(v); @@ -297,15 +310,6 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti measurement: $stateParams.measurement }); }; - function setupValueFieldType(unitAbbreviatedName, valence, variableName){ - $scope.state.units = qm.unitHelper.getUnitArrayContaining(unitAbbreviatedName); - //if($scope.state.measurement.inputType){return;} Why is this here? It prevents updating when we change a unit! :( - if(!unitAbbreviatedName){ - qmLogService.error('No unitAbbreviatedName provided to setupValueFieldType'); - return false; - } - $scope.state.measurement.inputType = qm.unitHelper.getInputType(unitAbbreviatedName, valence, variableName); - } function setVariableObjectFromMeasurement(){ $scope.state.variableObject = { unitAbbreviatedName: $scope.state.measurement.unitAbbreviatedName, @@ -398,11 +402,20 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti hideSheet(); }, 20000); }); - function getVariableCategoryName(object){ - return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl( - object, $scope.state.measurement, $stateParams, $stateParams.variableObject); + function getVariableCategoryName(obj){ + var cat = getVariableCategory(obj); + if(!cat){return null;} + return cat.name; } - function getVariableCategory(){ - return qmService.getVariableCategoryInfo(getVariableCategoryName()); + function getVariableCategory(obj){ + var cat; + if(obj){cat = qm.variableCategoryHelper.findVariableCategory(obj);} + if(!cat && $scope.state){cat = qm.variableCategoryHelper.findVariableCategory($scope.state);} + if(!cat){cat = qm.variableCategoryHelper.findVariableCategory($stateParams);} + if(!cat){ + qmLog.debug("No variable category name from getVariableCategory") + return null; + } + return cat; } }]); diff --git a/src/js/controllers/reminderAddCtrl.js b/src/js/controllers/reminderAddCtrl.js index af24376ea6..0a41fdfa40 100644 --- a/src/js/controllers/reminderAddCtrl.js +++ b/src/js/controllers/reminderAddCtrl.js @@ -58,9 +58,7 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s $scope.state.trackingReminder.id = $stateParams.trackingReminderId; } $scope.state.savingText = 'Save'; - qm.variableCategoryHelper.getVariableCategoriesFromGlobalsOrApi(function(variableCategories){ - $scope.state.variableCategories = variableCategories; - }); + $scope.state.variableCategories = qm.variableCategoryHelper.getVariableCategories(); qmService.navBar.showNavigationMenuIfHideUrlParamNotSet(); qmService.login.sendToLoginIfNecessaryAndComeBack("beforeEnter in " + $state.current.name); $stateParams.variableCategoryName = getVariableCategoryName(); @@ -419,7 +417,7 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s setHideDefaultValueField(); }; $scope.variableCategorySelectorChange = function(variableCategoryName){ - $scope.state.variableCategoryObject = qmService.getVariableCategoryInfo(variableCategoryName); + $scope.state.variableCategoryObject = qm.variableCategoryHelper.findVariableCategory(variableCategoryName); $scope.state.trackingReminder.unitAbbreviatedName = $scope.state.variableCategoryObject.defaultUnitAbbreviatedName; $scope.state.defaultValuePlaceholderText = 'Enter most common value'; $scope.state.defaultValueLabel = 'Default Value'; @@ -440,7 +438,7 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s } var r = $scope.state.trackingReminder; r.variableCategoryName = variableCategoryName; - $scope.state.variableCategoryObject = qmService.getVariableCategoryInfo(variableCategoryName); + $scope.state.variableCategoryObject = qm.variableCategoryHelper.findVariableCategory(variableCategoryName); if(!r.unitAbbreviatedName){ r.unitAbbreviatedName = $scope.state.variableCategoryObject.defaultUnitAbbreviatedName; } diff --git a/src/js/controllers/remindersInboxCtrl.js b/src/js/controllers/remindersInboxCtrl.js index 9c1cc53eb5..ba05712aa5 100644 --- a/src/js/controllers/remindersInboxCtrl.js +++ b/src/js/controllers/remindersInboxCtrl.js @@ -77,7 +77,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", }); $scope.$on('$ionicView.beforeLeave', function(){ qmLog.debug('RemindersInboxCtrl beforeLeave'); - qmService.syncTrackingReminderNotifications(); + qmService.syncNotificationsIfQueued(); }); $scope.$on('$ionicView.afterLeave', function(){ qmLog.debug('RemindersInboxCtrl afterLeave'); diff --git a/src/js/controllers/remindersManageCtrl.js b/src/js/controllers/remindersManageCtrl.js index 8f45fa2a7b..a2d863c0e1 100644 --- a/src/js/controllers/remindersManageCtrl.js +++ b/src/js/controllers/remindersManageCtrl.js @@ -55,7 +55,7 @@ angular.module('starter').controller('RemindersManageCtrl', ["$scope", "$state", }else{ $scope.state.noRemindersTitle = "Add " + cat; $scope.state.noRemindersText = "You haven't saved any " + cat.toLowerCase() + " favorites or reminders here, yet."; - $scope.state.noRemindersIcon = qmService.getVariableCategoryInfo(cat).ionIcon; + $scope.state.noRemindersIcon = qm.variableCategoryHelper.findVariableCategory(cat).ionIcon; $scope.stateParams.title = document.title = cat; if(!$scope.stateParams.addButtonText){ $scope.stateParams.addButtonText = 'Add New ' + pluralize($filter('wordAliases')(cat), 1) + " Reminder"; diff --git a/src/js/controllers/variableSearchCtrl.js b/src/js/controllers/variableSearchCtrl.js index b61b09b31c..df8723fb0b 100644 --- a/src/js/controllers/variableSearchCtrl.js +++ b/src/js/controllers/variableSearchCtrl.js @@ -1,6 +1,6 @@ -angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", "$rootScope", "$stateParams", "$timeout", - "$filter", "qmService", "qmLogService", function($scope, $state, $rootScope, $stateParams, $timeout, $filter, - qmService, qmLogService){ +angular.module('starter').controller('VariableSearchCtrl', + ["$scope", "$state", "$rootScope", "$stateParams", "$timeout", "$filter", "qmService", "qmLogService", + function($scope, $state, $rootScope, $stateParams, $timeout, $filter, qmService, qmLogService){ $scope.controller_name = "VariableSearchCtrl"; qmService.navBar.setFilterBarSearchIcon(false); $scope.$on('$ionicView.beforeEnter', function(e){ @@ -56,76 +56,85 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", }) } }); - $scope.selectVariable = function(variableObject){ - variableObject = qmService.barcodeScanner.addUpcToVariableObject(variableObject); - qmLog.info($state.current.name + ': ' + '$scope.selectVariable: ' + JSON.stringify(variableObject).substring(0, 140) + '...', null); - qm.variablesHelper.setLastSelectedAtAndSave(variableObject); + function saveTagged(selected) { + if ($scope.state.userTagVariableObject.unitAbbreviatedName !== '/5') { + qmService.goToState(getNextState(), { + userTaggedVariableObject: selected, + fromState: $scope.state.fromState, + fromStateParams: {variableObject: $scope.state.userTagVariableObject}, + userTagVariableObject: $scope.state.userTagVariableObject + }); + } else { + qmService.showBlackRingLoader(); + qmService.postUserTagDeferred({ + userTagVariableId: $scope.state.userTagVariableObject.variableId, + userTaggedVariableId: selected.variableId, + conversionFactor: 1 + }).then(function () { + qmService.hideLoader(); + if ($scope.state.fromState) { + qmService.goToState($scope.state.fromState, {variableName: $scope.state.userTagVariableObject.name}); + } else { + qmService.goToDefaultState(); + } + }); + } + } + function getNextState() { + var s = $state.current; + var next = s.params.nextState; + return next; + } + function saveTag(selected) { + if ($scope.state.userTaggedVariableObject.unitAbbreviatedName !== '/5') { + qmService.goToState(getNextState(), { + userTaggedVariableObject: $scope.state.userTaggedVariableObject, + fromState: $scope.state.fromState, + fromStateParams: {variableObject: $scope.state.userTaggedVariableObject}, + userTagVariableObject: selected + }); + } else { + qmService.showBlackRingLoader(); + qmService.postUserTagDeferred({ + userTagVariableId: selected.variableId, + userTaggedVariableId: $scope.state.userTaggedVariableObject.variableId, + conversionFactor: 1 + }).then(function () { + qmService.hideLoader(); + if ($scope.state.fromState) { + qmService.goToState($scope.state.fromState, {variableName: $scope.state.userTaggedVariableObject.name}); + } else { + qmService.goToDefaultState(); + } + }); + } + } + $scope.selectVariable = function(selected){ + selected = qmService.barcodeScanner.addUpcToVariableObject(selected); + var next = getNextState(); + var s = $state.current; + qmLog.info(s.name + ': ' + '$scope.selectVariable: ' + JSON.stringify(selected).substring(0, 140) + '...', null); + qm.variablesHelper.setLastSelectedAtAndSave(selected); $scope.state.variableSearchQuery.name = ''; - var userTagData; - if($state.current.name === 'app.favoriteSearch'){ - qmService.addToFavoritesUsingVariableObject(variableObject); + if(s.name === 'app.favoriteSearch'){ + qmService.addToFavoritesUsingVariableObject(selected); }else if(window.location.href.indexOf('reminder-search') !== -1){ - var options = { + qmService.reminders.addToRemindersUsingVariableObject(selected, { skipReminderSettingsIfPossible: $scope.state.skipReminderSettingsIfPossible, doneState: $scope.state.doneState - }; - qmService.reminders.addToRemindersUsingVariableObject(variableObject, options); - }else if($scope.state.nextState.indexOf('predictor') !== -1){ - qmService.goToState($scope.state.nextState, {effectVariableName: variableObject.name}); - }else if($scope.state.nextState.indexOf('outcome') !== -1){ - qmService.goToState($scope.state.nextState, {causeVariableName: variableObject.name}); + }); + }else if(next.indexOf('predictor') !== -1){ + qmService.goToState(next, {effectVariableName: selected.name}); + }else if(next.indexOf('outcome') !== -1){ + qmService.goToState(next, {causeVariableName: selected.name}); }else if($scope.state.userTaggedVariableObject){ - if($scope.state.userTaggedVariableObject.unitAbbreviatedName !== '/5'){ - qmService.goToState($scope.state.nextState, { - userTaggedVariableObject: $scope.state.userTaggedVariableObject, - fromState: $scope.state.fromState, - fromStateParams: {variableObject: $scope.state.userTaggedVariableObject}, - userTagVariableObject: variableObject - }); - }else{ - userTagData = { - userTagVariableId: variableObject.variableId, - userTaggedVariableId: $scope.state.userTaggedVariableObject.variableId, - conversionFactor: 1 - }; - qmService.showBlackRingLoader(); - qmService.postUserTagDeferred(userTagData).then(function(){ - qmService.hideLoader(); - if($scope.state.fromState){ - qmService.goToState($scope.state.fromState, {variableName: $scope.state.userTaggedVariableObject.name}); - }else{ - qmService.goToDefaultState(); - } - }); - } + saveTag(selected); }else if($scope.state.userTagVariableObject){ - if($scope.state.userTagVariableObject.unitAbbreviatedName !== '/5'){ - qmService.goToState($scope.state.nextState, { - userTaggedVariableObject: variableObject, - fromState: $scope.state.fromState, - fromStateParams: {variableObject: $scope.state.userTagVariableObject}, - userTagVariableObject: $scope.state.userTagVariableObject - }); - }else{ - userTagData = { - userTagVariableId: $scope.state.userTagVariableObject.variableId, - userTaggedVariableId: variableObject.variableId, - conversionFactor: 1 - }; - qmService.showBlackRingLoader(); - qmService.postUserTagDeferred(userTagData).then(function(){ - qmService.hideLoader(); - if($scope.state.fromState){ - qmService.goToState($scope.state.fromState, {variableName: $scope.state.userTagVariableObject.name}); - }else{ - qmService.goToDefaultState(); - } - }); - } + saveTagged(selected); }else{ - $scope.state.variableName = variableObject.name; - $scope.state.variableObject = variableObject; - qmService.goToState($scope.state.nextState, $scope.state); + $scope.state.variableName = selected.name; + $scope.state.variableObject = selected; + qmService.goToState(next, $scope.state); } }; $scope.goToStateFromVariableSearch = function(stateName, params){ @@ -134,8 +143,8 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", }; // when a query is searched in the search box function showAddVariableButtonIfNecessary(variables){ - if($scope.state.variableSearchQuery.barcode && - $scope.state.variableSearchQuery.barcode === $scope.state.variableSearchQuery.name){ + var barcode = $scope.state.variableSearchQuery.barcode; + if(barcode && barcode === $scope.state.variableSearchQuery.name){ $scope.state.showAddVariableButton = false; return; } @@ -158,9 +167,11 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", $scope.showSearchLoader = false; qmLog.info($state.current.name + ': ' + '$scope.onVariableSearch: Set showAddVariableButton to true', null); $scope.state.showAddVariableButton = true; - if($scope.state.nextState === "app.reminderAdd"){ + var s = $state.current; + var next = s.params.nextState; + if(next === "app.reminderAdd"){ $scope.state.addNewVariableButtonText = '+ Add ' + $scope.state.variableSearchQuery.name + ' reminder'; - }else if($scope.state.nextState === "app.measurementAdd"){ + }else if(next === "app.measurementAdd"){ $scope.state.addNewVariableButtonText = '+ Add ' + $scope.state.variableSearchQuery.name + ' measurement'; }else{ $scope.state.addNewVariableButtonText = '+ ' + $scope.state.variableSearchQuery.name; @@ -260,10 +271,8 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", variableObject.variableCategoryName = getVariableCategoryName(); } qmLog.info($state.current.name + ': ' + '$scope.addNewVariable: ' + JSON.stringify(variableObject)); - if($scope.state.nextState){ - $scope.state.variableObject = variableObject; - qmService.goToState($scope.state.nextState, $scope.state); - } + $scope.state.variableObject = variableObject; + qmService.goToState(getNextState(), $scope.state); }; function setHelpText(){ if($scope.state.userTaggedVariableObject){ @@ -307,7 +316,7 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", } function getVariableCategory(){ var name = getVariableCategoryName(); - if(name){return qm.variableCategoryHelper.getByNameOrId(name);} + if(name){return qm.variableCategoryHelper.findVariableCategory(name);} return null; } function getVariableCategoryName(){ diff --git a/src/js/qmHelpers.js b/src/js/qmHelpers.js index ede89102be..16babb7844 100644 --- a/src/js/qmHelpers.js +++ b/src/js/qmHelpers.js @@ -60,8 +60,8 @@ var qm = { if(qm.userHelper.isTestUser()){ return true; } - var result = qm.urlHelper.indexOfCurrentUrl("medimodo.heroku") !== -1; - return result; + return qm.urlHelper.indexOfCurrentUrl("medimodo.heroku") !== -1 || + qm.urlHelper.indexOfCurrentUrl("staging.quantimo.do"); }, isTestingOrDevelopment: function(){ return qm.appMode.isTesting() || qm.appMode.isDevelopment(); @@ -644,7 +644,11 @@ var qm = { if ( xhr.status === 200 ) { successHandler(responseObject); } else { - qm.qmLog.error("qm.api.get error from " + url + " request: " + xhr.responseText, null, responseObject); + if ( xhr.status === 401 ) { + qm.qmLog.info("qm.api.get error from " + url + " request: " + xhr.responseText, null, responseObject); + } else { + qm.qmLog.error("qm.api.get error from " + url + " request: " + xhr.responseText, null, responseObject); + } if(errorHandler){errorHandler(responseObject);} } } @@ -709,7 +713,7 @@ var qm = { if(qm.auth.getAccessTokenFromUrlUserOrStorage()){ url = addQueryParameter(url, 'access_token', qm.auth.getAccessTokenFromUrlUserOrStorage()); }else{ - qm.qmLog.error('No access token!'); + qm.qmLog.info('No access token for request!'); if(!qm.serviceWorker){ qm.chrome.showSignInNotification(); } @@ -804,6 +808,11 @@ var qm = { addVariableCategoryAndUnit: function(arr){ qm.variableCategoryHelper.addVariableCategoryProperties(arr) qm.unitHelper.addUnit(arr) + if(arr && arr.forEach){ + arr.forEach(function (r){ + qm.unitHelper.setInputType(r); + }) + } }, removeVariableCategoryAndUnit: function(arr){ if(!arr){return arr;} @@ -1392,6 +1401,11 @@ var qm = { } return a; }, + removeDuplicatesByProperty: function(myArr, prop) { + return myArr.filter((obj, pos, arr) => { + return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos; + }); + }, removeDuplicatesById: function(arr, type) { type = type || "[TYPE NOT PROVIDED]" if(!arr){ @@ -3608,7 +3622,7 @@ var qm = { }, locationAndWeatherTracking: { title: "Location and Weather Tracking", - textContent: qm.variableCategoryHelper.getVariableCategory('Location').moreInfo + textContent: qm.variableCategoryHelper.findVariableCategory('Location').moreInfo }, minimumAllowedValue: { title: "Minimum Allowed Value", @@ -4237,7 +4251,7 @@ var qm = { src.startTimeString || src.startTimeEpoch; var unit = qm.unitHelper.find(src); - var cat = qm.variableCategoryHelper.find(src); + var cat = qm.variableCategoryHelper.findVariableCategory(src); if(!unit && cat){ unit = qm.unitHelper.find(cat); } @@ -4260,7 +4274,7 @@ var qm = { variableCategoryName: src.variableCategoryName, variableName: src.variableName || src.name, } - if(!m.inputType && unit){m.inputType = qm.unitHelper.getInputType(unit.abbreviatedName, m.valence, m.variableName);} + if(!m.inputType && unit){qm.unitHelper.setInputType(m);} return m; }, fromNotification: function (n){ @@ -4273,7 +4287,9 @@ var qm = { }, getLocalMeasurements: function(params, cb){ var queue = qm.measurements.getMeasurementsFromQueue(params) || []; + qm.measurements.checkMeasurements(queue) var recent = qm.measurements.getRecentlyPostedMeasurements(params) || []; + qm.measurements.checkMeasurements(recent) qm.measurements.getPrimaryOutcomeMeasurements(function (measurements) { measurements = measurements || []; var indexed = {}; @@ -4323,22 +4339,52 @@ var qm = { if(id){recent = recent.filter(function(m){return m.id !== id;});} qm.measurements.recentlyPostedMeasurements = recent; }, - addMeasurementsToMemory: function(measurements){ - var measurementArray = measurements; - if(!Array.isArray(measurementArray)){ - measurementArray = []; - for (var variableName in measurements) { - if(!measurements.hasOwnProperty(variableName)){continue;} - var measurementObject = measurements[variableName]; - for (var date in measurementObject) { - measurementArray.push(measurementObject[date]); + addMeasurementsToMemory: function(byVariableName){ + var arr = []; + if(!Array.isArray(arr)){ + arr = []; + for (var variableName in byVariableName) { + if(!byVariableName.hasOwnProperty(variableName)){continue;} + var byDate = byVariableName[variableName]; + for (var date in byDate) { + if(byDate.hasOwnProperty(date)){ + arr.push(byDate[date]); + } else { + arr.push(byDate); + } + qm.measurements.checkMeasurements(arr) } } } - var existing = qm.measurements.recentlyPostedMeasurements; - var combined = qm.arrayHelper.concatenateUniqueId(measurementArray, existing); + qm.measurements.checkMeasurements(arr) + var existing = qm.measurements.recentlyPostedMeasurements || []; + qm.measurements.checkMeasurements(existing) + var combined = qm.arrayHelper.concatenateUniqueId(arr, existing); + qm.measurements.checkMeasurements(combined) qm.measurements.recentlyPostedMeasurements = combined; }, + checkMeasurements: function(arr){ + if(!arr){ + qmLog.error("No measurements provided to checkMeasurements", {'measurements': arr}) + debugger + return; + } + if(!arr.forEach){ + qmLog.error("Measurements provided to checkMeasurements is not an array", {'measurements': arr}) + debugger + return; + } + arr.forEach(function (m){ + if(typeof m === "function"){ + qmLog.error("existing Measurement is a function", {'measurements': arr}) + debugger + } + if(Array.isArray(m)){ + qmLog.error("existing Measurement is an array!", {'measurements': arr}) + debugger + } + }); + }, getMeasurementsFromApi: function(params, successHandler, errorHandler){ params = qm.api.addGlobalParams(params); var cachedData = qm.api.cacheGet(params, 'getMeasurementsFromApi'); @@ -4399,18 +4445,24 @@ var qm = { }, getMeasurementsFromQueue: function(params){ var measurements = qm.storage.getElementsWithRequestParams(qm.items.measurementsQueue, params) || [] + qm.measurements.checkMeasurements(measurements) measurements = qm.measurements.addInfoAndImagesToMeasurements(measurements); + qm.measurements.checkMeasurements(measurements) qm.qmLog.info("Got " + measurements.length + " measurements from queue with params: " + JSON.stringify(params), measurements); return measurements; }, addInfoAndImagesToMeasurements: function(measurements){ + qm.measurements.checkMeasurements(measurements); if(!Array.isArray(measurements)){measurements = Object.values(measurements);} + qm.measurements.checkMeasurements(measurements); function parseJsonIfPossible(str){ var object = false; + if(str === "{}"){return false;} try{ object = JSON.parse(str); }catch (e){ + qm.qmLog.error("Unrecognized note format. Could not properly format JSON note", str); return false; } return object; @@ -4419,18 +4471,20 @@ var qm = { var index; for(index = 0; index < measurements.length; ++index){ var m = measurements[index]; + if(typeof m === "function"){ + qmLog.error("Measurement is a function") + debugger + continue; + } var parsedNote = parseJsonIfPossible(m.note); - if(parsedNote){ - if(parsedNote.url && parsedNote.message){ - m.note = '' + parsedNote.message + ''; - }else{ - qm.qmLog.error("Unrecognized note format", "Could not properly format JSON note", {note: m.note}); - } + if(parsedNote && parsedNote.url && parsedNote.message){ + m.note = '' + parsedNote.message + ''; } m.startTime = m.startTime || m.startTimeEpoch; m.startAt = m.startAt || m.startTimeString; var unit = qm.unitHelper.getByNameAbbreviatedNameOrId(m.unitId || m.unitAbbreviatedName); if(!unit){ + debugger unit = qm.unitHelper.getByNameAbbreviatedNameOrId(m.unitId || m.unitAbbreviatedName); qm.qmLog.errorAndExceptionTestingOrDevelopment("Could not get unit for this measurement: ", m) } else { @@ -4443,7 +4497,7 @@ var qm = { m.displayValueAndUnitString = qm.stringHelper.formatValueUnitDisplayText(m.displayValueAndUnitString) m.valueUnitVariableName = m.displayValueAndUnitString + " " + m.variableName; if(!m.variableCategoryName){ - m.variableCategoryName = qm.variableCategoryHelper.getByNameOrId(m.variableCategoryId).name; + m.variableCategoryName = qm.variableCategoryHelper.findVariableCategory(m).name; } if(!m.image && m.roundedValue && ratingInfo[m.roundedValue]){ m.image = ratingInfo[m.roundedValue].numericImage; @@ -4453,7 +4507,7 @@ var qm = { if(m.image){m.pngPath = m.image;} m.icon = m.icon || m.ionIcon; if(m.variableCategoryName && !m.icon){ - var category = qm.variableCategoryHelper.getVariableCategory(m.variableCategoryName); + var category = qm.variableCategoryHelper.findVariableCategory(m); m.icon = category.ionIcon; m.pngPath = category.pngPath; } @@ -4546,7 +4600,7 @@ var qm = { }, setIonIcon: function(menuItem){ if(menuItem.params.variableCategoryName){ - var category = qm.variableCategoryHelper.getVariableCategory(menuItem.params.variableCategoryName); + var category = qm.variableCategoryHelper.findVariableCategory(menuItem.params); if(category && category.ionIcon){ menuItem.params.ionIcon = category.ionIcon; } @@ -4751,13 +4805,12 @@ var qm = { }, micAvailable: null, getMicEnabled: function(){ - if(qm.mic.microphoneDisabled){ - return false; - } + if(qm.mic.microphoneDisabled){return false;} if(!qm.mic.getMicAvailable()){ - return qm.mic.setMicEnabled(false); + qm.mic.setMicEnabled(false); + return false; } - return qm.storage.getItem(qm.items.micEnabled); + return qm.storage.getItem(qm.items.micEnabled) || null; }, setMicEnabled: function(micEnabled){ qm.qmLog.info("set micEnabled " + micEnabled); @@ -5809,42 +5862,40 @@ var qm = { var notifications = qm.storage.getItem(qm.items.notificationsSyncQueue); qm.storage.removeItem(qm.items.notificationsSyncQueue); qm.storage.removeItem(qm.items.trackingReminderNotificationSyncScheduled); - if(!notifications || !notifications.length){ - if(successHandler){successHandler();} - return; - } - if(!(notifications instanceof Array)){notifications = [notifications];} - if(!notifications[0]){ - qm.qmLog.error("notifications[0] is " + notifications[0], {notifications: notifications}); + var body = []; + if(notifications){ + if(!(notifications instanceof Array)){notifications = [notifications];} + body = notifications.map(function (n){ + return { + 'value': n.modifiedValue || n.value, + 'trackingReminderNotificationId': n.trackingReminderNotificationId, + 'variableId': n.variableId, + 'trackingReminderId': n.trackingReminderId, + 'action': n.action, + 'timeZone': moment.tz.guess(), + } + }) } - var body = notifications.map(function (n){ - return { - 'value': n.modifiedValue || n.value, - 'trackingReminderNotificationId': n.trackingReminderNotificationId, - 'variableId': n.variableId, - 'trackingReminderId': n.trackingReminderId, - 'action': n.action, - 'timeZone': moment.tz.guess(), - } - }) - qm.api.postToQuantiModo(body, 'v3/trackingReminderNotifications', - function(response){ - if(!response){ - var err = "No response from postToQuantiModo(body, 'v3/trackingReminderNotifications"; - if(errorHandler){ - errorHandler(err); - return; - } else { - throw err; - } + function saveResponse(response){ + 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);} + } + var data = response.data || response; + if(data.measurements){qm.measurements.addMeasurementsToMemory(data.measurements);} + if(data.trackingReminderNotifications){qm.storage.setTrackingReminderNotifications(data.trackingReminderNotifications);} + } + qm.api.postToQuantiModo(body, 'v3/trackingReminderNotifications', function(response){ + saveResponse(response); if(successHandler){successHandler(response);} }, function(response){ qm.qmLog.error(response.message) + saveResponse(response); // Sometimes we still return notifications even with an error // 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 @@ -6423,6 +6474,9 @@ var qm = { var reminders = qm.storage.getElementsWithRequestParams(qm.items.trackingReminders, requestParams); reminders = reminders || []; reminders = qm.arrayHelper.removeDuplicatesById(reminders); + reminders.forEach(function (r){ + qm.unitHelper.setInputType(r); + }) return reminders; }, getMostFrequentReminderIntervalInSeconds: function(trackingReminders){ @@ -6470,9 +6524,10 @@ var qm = { qm.qmLog.error("Cannot filter allReminders", {allReminders: allReminders}); return []; } - return allReminders.filter(function(trackingReminder){ + var favorites = allReminders.filter(function(trackingReminder){ return trackingReminder.reminderFrequency === 0; }); + return qm.arrayHelper.removeDuplicatesByProperty(favorites, 'variableId') }, getActive: function(allReminders){ if(!allReminders){ @@ -6495,7 +6550,7 @@ var qm = { trackingReminder.valueAndFrequencyTextDescription.toLowerCase().indexOf('ended') !== -1; }); }, - removeDuplicateNotifications:function(notifications){ + removeDuplicateNotifications: function(notifications){ var ids = []; var toKeep = []; if(!notifications){ @@ -6504,11 +6559,19 @@ var qm = { if(typeof notifications.forEach !== "function"){ throw "Notifications is not an array!" } + var allIds = notifications.map(function(n){ + return n.id; + }) notifications.forEach(function(n){ if(n.id !== n.trackingReminderNotificationId){ qmLog.errorAndExceptionTestingOrDevelopment("notification id: "+n.id + " does not match trackingReminderNotificationId: "+n.trackingReminderNotificationId, null, n); } + var before = n.actionArray; + n.actionArray = qm.arrayHelper.removeDuplicatesByProperty(before, 'title'); + if(before.length !== n.actionArray.length){ + qmLog.error("Duplicate button titles", before); + } var id = n.trackingReminderNotificationId || n.id; if(ids.indexOf(id) !== -1) { qmLog.errorAndExceptionTestingOrDevelopment("Duplicate notification id: "+id, null, n); @@ -7702,7 +7765,7 @@ var qm = { } var fromGlobals = qm.storage.getGlobal(key); if(fromGlobals !== null && fromGlobals !== "undefined" && fromGlobals !== "null"){ - qm.qmLog.debug("Got " + key + " from globals"); + // Not sure why this keeps getting sent to bugsnag even though it's a debug log // qm.qmLog.debug("Got " + key + " from globals"); return fromGlobals; } if(typeof localStorage === "undefined" || localStorage === null){ @@ -8720,6 +8783,9 @@ var qm = { } }, unitHelper: { + setInputType: function(obj){ + obj.inputType = qm.unitHelper.getInputType(obj.unitAbbreviatedName, obj.valence, obj.variableName); + }, getInputType: function(unitAbbreviatedName, valence, variableName) { var inputType = 'value'; if (variableName === 'Blood Pressure') {inputType = 'bloodPressure';} @@ -9573,7 +9639,7 @@ var qm = { }, getFromLocalStorage: function(params, successHandler, errorHandler){ if(!qm.getUser()){ - qm.qmLog.error("No user to get user variables!"); + qm.qmLog.debug("No user to get user variables!"); qm.commonVariablesHelper.getFromLocalStorage(params, successHandler, errorHandler); return; } @@ -9841,70 +9907,54 @@ var qm = { variableCategoryHelper: { replaceCategoryAliasWithActualNameIfNecessary: function(provided){ if(provided === "Anything"){return null;} - var category = qm.variableCategoryHelper.getByNameOrId(provided); + var category = qm.variableCategoryHelper.findVariableCategory(provided); if(!category){ qmLog.errorAndExceptionTestingOrDevelopment("Category "+provided+" not found!"); return null; } return category.name; }, - getVariableCategoriesFromApi: function(successHandler, errorHandler){ - qm.qmLog.info("Getting variable categories from API..."); - function globalSuccessHandler(variableCategories){ - qm.localForage.setItem(qm.items.variableCategories, variableCategories); - if(successHandler){ - successHandler(variableCategories); - } - } - qm.api.configureClient(arguments.callee.name); - var apiInstance = new qm.Quantimodo.VariablesApi(); - function callback(error, data, response){ - qm.api.generalResponseHandler(error, data, response, globalSuccessHandler, errorHandler, {}, 'getVariableCategoriesFromApi'); - } - apiInstance.getVariableCategories(callback); + getVariableCategories: function(){ + return qm.staticData.variableCategories; }, - getVariableCategoriesFromGlobalsOrApi: function(successHandler, errorHandler){ - var categories = qm.variableCategoryHelper.getVariableCategoriesFromGlobals(); - if(!categories && qm.staticData && qm.staticData.variableCategories){ - categories = qm.staticData.variableCategories; - } - if(categories){ - if(successHandler){ - successHandler(categories); + findVariableCategory: function(nameOrId){ + if(typeof nameOrId === 'object' && nameOrId !== null){ + var obj = nameOrId; + nameOrId = nameOrId.variableCategoryName || nameOrId.variableCategoryId; + if(!nameOrId && obj.measurement){ + nameOrId = obj.measurement.variableCategoryName || obj.measurement.variableCategoryId; + } + if(!nameOrId && obj.variableObject){ + nameOrId = obj.variableObject.variableCategoryName || obj.variableObject.variableCategoryId; + } + if(!nameOrId){ + qmLog.debug("No name or id from object in findVariableCategory", obj) + return null; } - return categories; } - qm.variableCategoryHelper.getVariableCategoriesFromApi(function(variableCategories){ - successHandler(variableCategories); - }, errorHandler); - }, - getVariableCategoriesFromGlobals: function(){ - if(qm.staticData.variableCategories){ - return qm.staticData.variableCategories; + if(!nameOrId){nameOrId = qm.urlHelper.getParam('variableCategoryName');} + if(!nameOrId){nameOrId = qm.urlHelper.getParam('variableCategoryId');} + var categories = qm.variableCategoryHelper.getVariableCategories(); + var id, name = null; + if(!nameOrId){ + qmLog.error("No name or id provided to findVariableCategory") + return null; } - return qm.globalHelper.getItem(qm.items.variableCategories); - }, - getVariableCategory: function(variableCategoryName, successHandler){ - if(!successHandler){ - var variableCategories = qm.variableCategoryHelper.getVariableCategoriesFromGlobals(); - if(variableCategories){ - return variableCategories.find(function(variableCategory){ - if(variableCategory.name.toLowerCase() === variableCategoryName.toLowerCase()){ - return true; - } - if(variableCategory.synonyms && variableCategory.synonyms.indexOf(variableCategoryName) !== -1){ - return true; - } - return variableCategory.variableCategoryNameSingular && variableCategory.variableCategoryNameSingular.toLowerCase() === variableCategoryName.toLowerCase(); - }); - } + if(Number.isInteger(nameOrId)){ + id = nameOrId; + } else { + name = nameOrId.toLowerCase(); } - qm.variableCategoryHelper.getVariableCategoriesFromGlobalsOrApi(function(variableCategories){ - var match = variableCategories.find(function(category){ - category.name = variableCategoryName; - }); - successHandler(match); + var c = categories.find(function(c){ + if(c.id === id){return true;} + if(c.name.toLowerCase() === name){return true;} + for (var i = 0; i < c.synonyms.length; i++) { + var syn = c.synonyms[i]; + if(name === syn.toLowerCase()){return true;} + } + return c.variableCategoryNameSingular && c.variableCategoryNameSingular.toLowerCase() === name; }); + return c; }, getVariableCategoryNameFromStateParamsOrUrl: function(obj1, obj2, obj3){ var name = qm.urlHelper.getParam('variableCategoryName'); @@ -9915,7 +9965,7 @@ var qm = { return name; }, getByNameOrId: function(nameOrId){ - var cats = qm.variableCategoryHelper.getVariableCategoriesFromGlobals(); + var cats = qm.variableCategoryHelper.getVariableCategories(); if(isNaN(nameOrId)){ nameOrId = nameOrId.toLowerCase(); nameOrId = nameOrId.replace("+", " ") @@ -9923,10 +9973,7 @@ var qm = { // noinspection EqualityComparisonWithCoercionJS if(c.id == nameOrId){return true;} if(c.name.toLowerCase() === nameOrId){return true;} - for (var i = 0; i < c.synonyms.length; i++) { - var syn = c.synonyms[i]; - if(nameOrId === syn.toLowerCase()){return true;} - } + return false; }); } else { @@ -9944,7 +9991,7 @@ var qm = { var cat = obj.variableCategory; if(!cat){ var nameOrId = obj.variableCategoryId || obj.variableCategoryName || null; - if(nameOrId){cat = qm.variableCategoryHelper.getByNameOrId(nameOrId);} + if(nameOrId){cat = qm.variableCategoryHelper.findVariableCategory(nameOrId);} } if(cat){ obj.fontAwesome = obj.fontAwesome || cat.fontAwesome @@ -9958,16 +10005,6 @@ var qm = { } } return arr; - }, - find: function(v) { - var nameOrId; - if(typeof v === "string" || v === parseInt(v, 10)){ - nameOrId = v; - } else { - nameOrId = v.variableCategoryId || v.variableCategoryName; - } - if(!nameOrId){return null;} - return qm.variableCategoryHelper.getByNameOrId(nameOrId); } }, visualizer: { @@ -10470,7 +10507,7 @@ var qm = { } }) .catch(function(err){ - qm.qmLog.error('An error occurred while retrieving token because: '+err.message, null, err); + qm.qmLog.debug('An error occurred while retrieving token because: '+err.message, null, err); //showToken('Error retrieving Instance ID token. ', err); //qm.webNotifications.postWebPushSubscriptionToServer(false); }); diff --git a/src/js/qmLogger.js b/src/js/qmLogger.js index a6136b32d1..842382aa49 100644 --- a/src/js/qmLogger.js +++ b/src/js/qmLogger.js @@ -258,6 +258,7 @@ var qmLog = { return false; }, bugsnagNotify: function(name, message, errorSpecificMetaData, logLevel, stackTrace){ + debugger if(typeof bugsnagClient === "undefined"){ if(!qmLog.qm.appMode.isDevelopment()){ console.error('bugsnagClient not defined', errorSpecificMetaData); diff --git a/src/js/services/qmService.js b/src/js/services/qmService.js index b9bf7b8795..0900ca6c83 100644 --- a/src/js/services/qmService.js +++ b/src/js/services/qmService.js @@ -2098,7 +2098,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i reconfigurePushNotificationsIfNoTokenOnServerOrToSync: function(){ //if(qm.platform.isMobile() && !qm.storage.getItem(qm.items.deviceTokenOnServer) && !qm.storage.getItem(qm.items.deviceTokenToSync)){ if(!qm.storage.getItem(qm.items.deviceTokenOnServer) && !qm.storage.getItem(qm.items.deviceTokenToSync)){ - qmLog.warn("No device token on deviceTokenOnServer or deviceTokenToSync! Going to reconfigure push notifications"); + qmLog.debug("No device token on deviceTokenOnServer or deviceTokenToSync! Going to reconfigure push notifications"); qmService.configurePushNotifications(); } else { qmLog.info("NOT going to reconfigurePushNotifications because we have deviceTokenOnServer || deviceTokenToSync") @@ -2292,11 +2292,12 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i dialogParams.placeholder = "Enter a variable"; } if(dialogParams.requestParams && dialogParams.requestParams.variableCategoryName){ - var variableCategory = qm.variableCategoryHelper.getVariableCategory(dialogParams.requestParams.variableCategoryName); - if(variableCategory){ - dialogParams.title = 'Select ' + variableCategory.variableCategoryNameSingular.toLowerCase(); - dialogParams.placeholder = dialogParams.placeholder.replace('variable', variableCategory.variableCategoryNameSingular.toLowerCase()); - dialogParams.helpText = dialogParams.helpText.replace('variable', variableCategory.variableCategoryNameSingular.toLowerCase()); + var cat = qm.variableCategoryHelper.findVariableCategory(dialogParams.requestParams); + if(cat){ + var name = cat.variableCategoryNameSingular.toLowerCase(); + dialogParams.title = 'Select ' + name; + dialogParams.placeholder = dialogParams.placeholder.replace('variable', name); + dialogParams.helpText = dialogParams.helpText.replace('variable', name); } } if(qm.platform.isMobile()){ @@ -2591,7 +2592,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i getTitle: function(variableCategoryName){ var title = 'Enter a variable'; if(variableCategoryName){ - var variableCategory = qm.variableCategoryHelper.getVariableCategory(variableCategoryName); + var variableCategory = qm.variableCategoryHelper.findVariableCategory(variableCategoryName); if(variableCategory){ title = "Enter a " + variableCategory.variableCategoryNameSingular; } @@ -2834,7 +2835,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i syncPromise: null, syncTrackingReminders: function(force){ if(qmService.trackingReminders.syncPromise){ - qmLog.error("Returning existing qmService.trackingReminders.syncPromise"); + qmLog.debug("Returning existing qmService.trackingReminders.syncPromise"); return qmService.trackingReminders.syncPromise; } var deferred = $q.defer(); @@ -2891,7 +2892,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i deferred.reject(error); }); }; - qmService.syncTrackingReminderNotifications().then(function(){ + qmService.syncNotificationsIfQueued().then(function(){ postTrackingRemindersToApiAndHandleResponse(); }, function (err){ postTrackingRemindersToApiAndHandleResponse(); @@ -3269,22 +3270,26 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i function addVariableCategoryInfo(array){ angular.forEach(array, function(value, key){ if(!value){ - qmLog.error("no value for key " + key + " in array ", array); + qmLog.error("no value for key " + key + " in array: ", array); + return; } - if(value && value.variableCategoryName && qmService.variableCategories[value.variableCategoryName]){ + var nameOrId = value.variableCategoryId || value.variableCategoryName || null; + if(!nameOrId){return;} + var cat = qm.variableCategoryHelper.findVariableCategory(nameOrId); + if(cat){ if(typeof value.iconClass === "undefined"){ - value.iconClass = 'icon positive ' + qmService.variableCategories[value.variableCategoryName].ionIcon; + value.iconClass = 'icon positive ' + cat.ionIcon; } if(typeof value.ionIcon === "undefined"){ - value.ionIcon = qmService.variableCategories[value.variableCategoryName].ionIcon; + value.ionIcon = cat.ionIcon; } if(typeof value.moreInfo === "undefined"){ - value.moreInfo = qmService.variableCategories[value.variableCategoryName].moreInfo; + value.moreInfo = cat.moreInfo; } if(typeof value.image === "undefined"){ qmLog.info("Updating image to " + value.variableCategoryName); value.image = { - url: qmService.variableCategories[value.variableCategoryName].imageUrl, + url: cat.imageUrl, height: "96", width: "96" }; @@ -4283,13 +4288,6 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i return variableName; } }; - qmService.getVariableCategoryInfo = function(variableCategoryName){ - var selectedVariableCategoryObject = $rootScope.variableCategories.Anything; - if(variableCategoryName && $rootScope.variableCategories[variableCategoryName]){ - selectedVariableCategoryObject = $rootScope.variableCategories[variableCategoryName]; - } - return selectedVariableCategoryObject; - }; qmService.getAndStorePrimaryOutcomeMeasurements = function(){ var deferred = $q.defer(); var errorMessage; @@ -4331,29 +4329,23 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i } var parsedMeasurementsQueue = qm.measurements.getMeasurementsFromQueue(); if(!parsedMeasurementsQueue || parsedMeasurementsQueue.length < 1){ - if(successHandler){ - successHandler(); - } - return; + if(successHandler){successHandler();} + } else { + qmService.postMeasurementsToApi(parsedMeasurementsQueue, function(response){ + if(response && response.data && response.data.userVariables){ + qm.variablesHelper.saveToLocalStorage(response.data.userVariables); + } + qm.measurements.recentlyPostedMeasurements = qm.measurements.recentlyPostedMeasurements.concat(parsedMeasurementsQueue); // Save these for history page + qm.storage.setItem(qm.items.measurementsQueue, []); + if(successHandler){successHandler();} + defer.resolve(); + }, function(error){ + qm.storage.setItem(qm.items.measurementsQueue, parsedMeasurementsQueue); + if(errorHandler){errorHandler();} + defer.reject(error); + }); + return defer.promise; } - qmService.postMeasurementsToApi(parsedMeasurementsQueue, function(response){ - if(response && response.data && response.data.userVariables){ - qm.variablesHelper.saveToLocalStorage(response.data.userVariables); - } - qm.measurements.recentlyPostedMeasurements = qm.measurements.recentlyPostedMeasurements.concat(parsedMeasurementsQueue); // Save these for history page - qm.storage.setItem(qm.items.measurementsQueue, []); - if(successHandler){ - successHandler(); - } - defer.resolve(); - }, function(error){ - qm.storage.setItem(qm.items.measurementsQueue, parsedMeasurementsQueue); - if(errorHandler){ - errorHandler(); - } - defer.reject(error); - }); - return defer.promise; }; qmService.syncPrimaryOutcomeVariableMeasurements = function(minimumSecondsBetweenGets){ function canWeSyncYet(localStorageItemName, minimumSecondsBetweenSyncs){ @@ -4559,43 +4551,24 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i }); return deferred.promise; }; - qmService.variableCategories = []; - $rootScope.variableCategories = []; - $rootScope.variableCategoryNames = []; // Dirty hack for variableCategoryNames because $rootScope.variableCategories is not an array we can ng-repeat through in selectors - $rootScope.variableCategories.Anything = qmService.variableCategories.Anything = { - defaultUnitAbbreviatedName: '', - helpText: "What do you want to record?", - variableCategoryNameSingular: "Anything", - defaultValuePlaceholderText: "Enter most common value here...", - defaultValueLabel: 'Value', - addNewVariableCardText: 'Add a new variable', - variableCategoryName: '', - defaultValue: '', - measurementSynonymSingularLowercase: "measurement", - ionIcon: "ion-speedometer" - }; qmService.getVariableCategories = function(){ - var deferred = $q.defer(); - qm.variableCategoryHelper.getVariableCategoriesFromGlobalsOrApi(function(variableCategories){ - angular.forEach(variableCategories, function(variableCategory, key){ - $rootScope.variableCategories[variableCategory.name] = variableCategory; - $rootScope.variableCategoryNames.push(variableCategory.name); - qmService.variableCategories[variableCategory.name] = variableCategory; - }); - deferred.resolve(variableCategories); - }); - return deferred.promise; + qmService.variableCategories = []; + $rootScope.variableCategories = []; + $rootScope.variableCategoryNames = []; // Dirty hack for variableCategoryNames because $rootScope.variableCategories is not an array we can ng-repeat through in selectors + $rootScope.variableCategories.Anything = qmService.variableCategories.Anything = { + defaultUnitAbbreviatedName: '', + helpText: "What do you want to record?", + variableCategoryNameSingular: "Anything", + defaultValuePlaceholderText: "Enter most common value here...", + defaultValueLabel: 'Value', + addNewVariableCardText: 'Add a new variable', + variableCategoryName: '', + defaultValue: '', + measurementSynonymSingularLowercase: "measurement", + ionIcon: "ion-speedometer" + }; }; qmService.getVariableCategories(); - qmService.getVariableCategoryIcon = function(variableCategoryName){ - var variableCategoryInfo = qmService.getVariableCategoryInfo(variableCategoryName); - if(variableCategoryInfo.ionIcon){ - return variableCategoryInfo.ionIcon; - }else{ - console.warn('Could not find icon for variableCategoryName ' + variableCategoryName); - return 'ion-speedometer'; - } - }; qmService.setPlatformVariables = function(){ var platform = {}; //qmLog.debug("ionic.Platform.platform() is " + ionic.Platform.platform()); @@ -5090,9 +5063,34 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i }); return deferred.promise; }; + qmService.syncNotificationsIfQueued =function (){ + var deferred = $q.defer(); + var notifications = qm.storage.getItem(qm.items.notificationsSyncQueue); + if(notifications && notifications.length){ + return qmService.syncTrackingReminderNotifications(); + } else { + deferred.resolve([]); + } + return deferred.promise; + }; + qmService.syncNotificationsIfEmpty =function (){ + var deferred = $q.defer(); + var notifications = qm.notifications.getFromGlobalsOrLocalStorage(); + if(!notifications || !notifications.length){ + return qmService.syncTrackingReminderNotifications(); + } else { + deferred.resolve(notifications); + } + return deferred.promise; + }; qmService.syncTrackingReminderNotifications = function(params){ var deferred = $q.defer(); if(params && params.noCache){qmService.notificationsPromise = false;} + if(!qm.getUser()){ + deferred.reject("No user to get notifications"); + qmService.notificationsPromise = false; + return deferred.promise; + } if(qmService.notificationsPromise){return qmService.notificationsPromise;} qm.notifications.syncTrackingReminderNotifications(function(response){ var notifications = qm.notifications.getFromGlobalsOrLocalStorage(); @@ -7189,13 +7187,13 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i var deferred = $q.defer(); qmService.getTrackingRemindersDeferred(variableCategoryName).then(function(reminders){ reminders = qm.reminderHelper.validateReminderArray(reminders); - qmLog.info('Got ' + reminders.length + ' unprocessed ' + variableCategoryName + ' category trackingReminders'); + qmLog.debug('Got ' + reminders.length + ' unprocessed ' + variableCategoryName + ' category trackingReminders'); var separated = qm.reminderHelper.filterByCategoryAndSeparateFavoritesAndArchived(reminders, variableCategoryName); if(type){ qmLog.info('Got ' + separated[type].length + ' ' + variableCategoryName + ' category ' + type + 's'); deferred.resolve(separated[type]); }else{ - qmLog.info('Returning reminderTypesArray from getTrackingRemindersDeferred'); + qmLog.debug('Returning reminderTypesArray from getTrackingRemindersDeferred'); deferred.resolve(separated); } }); @@ -7786,7 +7784,7 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i qmService.notifications.showAndroidPopupForMostRecentNotification(); }else{ qmLog.pushDebug('window.overApps for popups is undefined! '); - qmService.syncTrackingReminderNotifications({}).then(function(){ + qmService.syncNotificationsIfEmpty({}).then(function(){ qmLog.pushDebug('push.on.notification: successfully refreshed notifications'); }, function(error){ qmLog.error('push.on.notification: ', error); diff --git a/src/templates/fragments/favorites-list-fragment.html b/src/templates/fragments/favorites-list-fragment.html index 5c99baa97f..7fe825e636 100644 --- a/src/templates/fragments/favorites-list-fragment.html +++ b/src/templates/fragments/favorites-list-fragment.html @@ -1,4 +1,4 @@ -