Skip to content

Commit

Permalink
Merge pull request #4044 from QuantiModo/feature/newMeasurement
Browse files Browse the repository at this point in the history
Feature/new measurement
  • Loading branch information
mikepsinn authored Sep 21, 2020
2 parents 3b54347 + a9af919 commit 2890f68
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 102 deletions.
143 changes: 62 additions & 81 deletions src/js/controllers/measurementAddCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,34 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
$scope.state.title = 'Record a Measurement';
$scope.state.selectedDate = moment();
$scope.state.units = qm.unitHelper.getNonAdvancedUnits();
if($stateParams.trackingReminder){
setupTrackingByReminderNotification($stateParams.trackingReminder);
}else if($stateParams.measurement){
setupTrackingByMeasurement($stateParams.measurement);
}else if(qm.urlHelper.getParam('measurementObject', window.location.href, true)){
setupTrackingByMeasurement(JSON.parse(qm.urlHelper.getParam('measurementObject', window.location.href, true)));
}else if($stateParams.variableObject){
setupFromVariable($stateParams.variableObject);
}else if(qm.urlHelper.getParam('trackingReminderObject', window.location.href, true)){
setupTrackingByReminderNotification(JSON.parse(qm.urlHelper.getParam('trackingReminderObject', window.location.href, true)));
}else if($stateParams.reminderNotification){
setupTrackingByReminderNotification($stateParams.reminderNotification);
}else if(qm.urlHelper.getParam('measurementId', location.href, true)){
setMeasurementVariablesByMeasurementId().then(function(){
if(!$scope.state.measurementIsSetup){
$scope.goBack();
}
var reminderFromUrl = qm.urlHelper.getParam('trackingReminderObject', window.location.href, true);
var measurementFromUrl = qm.urlHelper.getParam('measurementObject', window.location.href, true);
var tr = $stateParams.trackingReminder;
var m = $stateParams.measurement;
var v = $stateParams.variableObject;
var n = $stateParams.reminderNotification;
var id = qm.urlHelper.getParam('measurementId', location.href, true);
if(tr){
setupTrackingByReminder(tr);
}else if(m){
setupTrackingByMeasurement(m);
}else if(measurementFromUrl){
setupTrackingByMeasurement(JSON.parse(measurementFromUrl));
}else if(v){
setupFromVariable(v);
}else if(reminderFromUrl){
setupTrackingByReminder(JSON.parse(reminderFromUrl));
}else if(n){
setupTrackingByReminder(n);
}else if(id){
setMeasurementVariablesByMeasurementId(id).then(function(){
if(!$scope.state.measurementIsSetup){$scope.goBack();}
});
}else if($stateParams.variableName){
setupFromVariableName($stateParams.variableName);
}
if(!$scope.state.measurementIsSetup){
setupFromUrlParameters();
}
if(!$scope.state.measurementIsSetup){
setupFromVariable(qm.getPrimaryOutcomeVariable());
}
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);
Expand Down Expand Up @@ -208,12 +209,12 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
var startTimeEpoch = qm.urlHelper.getParam('startTimeEpoch', location.href, true);
var value = qm.urlHelper.getParam('value', location.href, true);
if(unitAbbreviatedName || variableName || startTimeEpoch || value){
var measurementObject = {};
measurementObject.unitAbbreviatedName = unitAbbreviatedName;
measurementObject.variableName = variableName;
measurementObject.startTimeEpoch = startTimeEpoch;
measurementObject.value = value;
setupTrackingByMeasurement(measurementObject);
var m = {};
m.unitAbbreviatedName = unitAbbreviatedName;
m.variableName = variableName;
m.startTimeEpoch = startTimeEpoch;
m.value = value;
setupTrackingByMeasurement(m);
}
};
function isYesNo() {
Expand Down Expand Up @@ -247,20 +248,14 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
var category = qmService.getVariableCategoryInfo(v.variableCategoryName);
setupUnit(category.defaultUnitAbbreviatedName, v.valence);
}
if(v.upc){$scope.state.measurement.upc = v.upc;}
if(v.inputType){$scope.state.measurement.inputType = v.inputType;}
$scope.state.measurement.variableName = v.name;
$scope.state.measurement.maximumAllowedValue = v.maximumAllowedValue;
$scope.state.measurement.minimumAllowedValue = v.minimumAllowedValue;
if(!$scope.state.measurement.variableName){$scope.state.measurement.variableName = v.variableName;}
if($scope.state.measurement.variableName.toLowerCase().indexOf('blood pressure') > -1){$rootScope.bloodPressure.show = true;}
if(v.variableCategoryName){
$scope.state.measurement.variableCategoryName = v.variableCategoryName;
setupVariableCategory(v.variableCategoryName);
var m = qm.measurements.newMeasurement(v);
if(m.variableName.toLowerCase().indexOf('blood pressure') > -1){$rootScope.bloodPressure.show = true;}
if(m.variableCategoryName){
setupVariableCategory(m.variableCategoryName);
}else{
$scope.state.showVariableCategorySelector = true;
}
$scope.state.measurement.combinationOperation = (v.combinationOperation) ? v.combinationOperation : 'MEAN';
$scope.state.measurement = m;
$scope.state.measurementIsSetup = true;
// Fill in default value as last value if not /5
/** @namespace variableObject.lastValue */
Expand All @@ -278,21 +273,20 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
qmLogService.error(error);
});
};
var setMeasurementVariablesByMeasurementId = function(){
var setMeasurementVariablesByMeasurementId = function(id){
var deferred = $q.defer();
qmService.showBlackRingLoader();
qmService.getMeasurementById(qm.urlHelper.getParam('measurementId', location.href, true))
.then(function(measurementObject){
qmService.getMeasurementById(id)
.then(function(m){
qmService.hideLoader();
$scope.state.measurementIsSetup = true;
setupTrackingByMeasurement(measurementObject);
setupTrackingByMeasurement(m);
deferred.resolve();
}, function(error){
qmService.hideLoader();
qmLogService.error($state.current.name + ": " + "Error response: ", error);
deferred.reject(error);
}
);
qmService.hideLoader();
qmLogService.error($state.current.name + ": " + "Error response: ", error);
deferred.reject(error);
});
return deferred.promise;
};
$scope.goToAddReminder = function(){
Expand All @@ -310,7 +304,7 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
qmLogService.error('No unitAbbreviatedName provided to setupValueFieldType');
return false;
}
$scope.state.measurement.inputType = qmService.getInputType(unitAbbreviatedName, valence, variableName);
$scope.state.measurement.inputType = qm.unitHelper.getInputType(unitAbbreviatedName, valence, variableName);
}
function setVariableObjectFromMeasurement(){
$scope.state.variableObject = {
Expand All @@ -330,42 +324,29 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
}
}
}
var setupTrackingByMeasurement = function(measurementObject){
if(isNaN(measurementObject.startTimeEpoch)){
measurementObject.startTimeEpoch = moment(measurementObject.startTimeEpoch).unix();
}
if(!measurementObject.id){
measurementObject.prevStartTimeEpoch = measurementObject.startTimeEpoch;
}
var setupTrackingByMeasurement = function(m){
if(isNaN(m.startTimeEpoch)){m.startTimeEpoch = moment(m.startTimeEpoch).unix();}
if(!m.id){m.prevStartTimeEpoch = m.startTimeEpoch;}
$scope.state.title = "Edit Measurement";
$scope.state.selectedDate = moment(measurementObject.startTimeEpoch * 1000);
$scope.state.measurement = measurementObject;
qmLog.info("Setting $scope.state.measurement to ", $scope.state.measurement);
$scope.state.selectedDate = moment(m.startTimeEpoch * 1000);
$scope.state.measurement = m;
qmLog.info("Setting $scope.state.measurement to ", m);
$scope.state.measurementIsSetup = true;
setupUnit($scope.state.measurement.unitAbbreviatedName, $scope.state.measurement.valence);
if($scope.state.measurement.variable){
$scope.state.measurement.variableName = $scope.state.measurement.variable;
}
setupUnit(m.unitAbbreviatedName, m.valence);
setStateVariable();
};
var setupTrackingByReminderNotification = function(n){
if(n){
$scope.state.title = "Record Measurement";
if(!$scope.state.measurement.unitAbbreviatedName){
setupUnit(n.unitAbbreviatedName);
}
$scope.state.hideRemindMeButton = true;
$scope.state.measurement.value = n.defaultValue;
$scope.state.measurement.variableName = n.variableName;
$scope.state.measurement.variableCategoryName = getVariableCategoryName(n);
$scope.state.measurement.combinationOperation = n.combinationOperation;
if(n.trackingReminderNotificationTimeEpoch !== "undefined" && n.trackingReminderNotificationTimeEpoch){
$scope.state.selectedDate = moment(n.trackingReminderNotificationTimeEpoch * 1000);
}
$scope.state.measurementIsSetup = true;
setupUnit(n.unitAbbreviatedName, n.valence);
setStateVariable();
var setupTrackingByReminder = function(n){
$scope.state.title = "Record Measurement";
if(!$scope.state.measurement.unitAbbreviatedName){
setupUnit(n.unitAbbreviatedName);
}
$scope.state.hideRemindMeButton = true;
var m = qm.measurements.fromNotification(n);
$scope.state.measurement = m;
if(m.startTime){$scope.state.selectedDate = moment(m.startTime * 1000);}
$scope.state.measurementIsSetup = true;
setupUnit(n.unitAbbreviatedName, n.valence);
setStateVariable();
// Create variableObject
if(!$scope.state.variableObject){
if($stateParams.variableObject !== null && typeof $stateParams.variableObject !== "undefined"){
Expand Down
79 changes: 77 additions & 2 deletions src/js/qmHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4219,6 +4219,53 @@ var qm = {
}
},
measurements: {
newMeasurement: function(src){
var value;
if(typeof src.modifiedValue !== "undefined" && src.modifiedValue !== null){
value = src.modifiedValue;
} else if (typeof src.value !== "undefined" && src.value !== null){
value = src.value;
} else if (typeof src.defaultValue !== "undefined" && src.defaultValue !== null){
value = src.defaultValue;
} else {
value = null;
}
var timeAt = src.trackingReminderNotificationTimeEpoch ||
src.startAt ||
src.startTime ||
src.notifyAt ||
src.startTimeString ||
src.startTimeEpoch;
var unit = qm.unitHelper.find(src);
var cat = qm.variableCategoryHelper.find(src);
if(!unit && cat){
unit = qm.unitHelper.find(cat);
}
var m = {
combinationOperation: src.combinationOperation || 'MEAN',
icon: src.icon,
inputType: src.inputType || (unit) ? unit.inputType : null,
maximumAllowedValue: src.maximumAllowedValue || (unit) ? unit.maximum : null,
minimumAllowedValue: src.minimumAllowedValue || (unit) ? unit.minimum : null,
pngPath: src.pngPath || src.image || (cat) ? cat.pngUrl : null,
startAt: qm.timeHelper.toDate(timeAt),
startTime: qm.timeHelper.toUnixTime(timeAt),
unitAbbreviatedName: (unit) ? unit.abbreviatedName : null,
unitId: (unit) ? unit.id : null,
unitName: (unit) ? unit.name : null,
upc: src.upc,
valence: src.valence,
value: value,
variableCategoryId: src.variableCategoryName,
variableCategoryName: src.variableCategoryName,
variableName: src.variableName || src.name,
}
if(!m.inputType && unit){m.inputType = qm.unitHelper.getInputType(unit.abbreviatedName, m.valence, m.variableName);}
return m;
},
fromNotification: function (n){
return qm.measurements.newMeasurement(n);
},
getUniqueKey: function(m){
if(m.id){return m.id.toString();}
var startTime = m.startTime || m.startTimeEpoch;
Expand Down Expand Up @@ -8482,6 +8529,13 @@ var qm = {
}
},
timeHelper: {
toDate: function(timeAt){
var unixTime = qm.timeHelper.universalConversionToUnixTimeSeconds(timeAt);
return qm.timeHelper.convertUnixTimeStampToISOString(unixTime);
},
toUnixTime: function(timeAt){
return qm.timeHelper.universalConversionToUnixTimeSeconds(timeAt);
},
getUnixTimestampInMilliseconds: function(dateTimeString){
if(!dateTimeString){
return new Date().getTime();
Expand Down Expand Up @@ -8680,6 +8734,17 @@ var qm = {
}
},
unitHelper: {
getInputType: function(unitAbbreviatedName, valence, variableName) {
var inputType = 'value';
if (variableName === 'Blood Pressure') {inputType = 'bloodPressure';}
if (unitAbbreviatedName === '/5') {
inputType = 'oneToFiveNumbers';
if (valence === 'positive') {inputType = 'happiestFaceIsFive';}
if (valence === 'negative') {inputType = 'saddestFaceIsFive';}
}
if (unitAbbreviatedName === 'yes/no') {inputType = 'yesOrNo';}
return inputType;
},
getNonAdvancedUnits: function(){
var nonAdvancedUnitObjects = [];
var allUnits = qm.staticData.units;
Expand Down Expand Up @@ -8820,9 +8885,9 @@ var qm = {
}
return arr;
},
find:function(v){
find: function(v) {
var nameOrId;
if(typeof v === "string" || data === parseInt(data, 10)){
if(typeof v === "string" || v === parseInt(v, 10)){
nameOrId = v;
} else {
nameOrId = v.unitId || v.defaultUnitId || v.defaultUnitAbbreviatedName || v.unitAbbreviatedName || v.id;
Expand Down Expand Up @@ -9907,6 +9972,16 @@ 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: {
visualizerEnabled: true,
Expand Down
19 changes: 0 additions & 19 deletions src/js/services/qmService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7439,25 +7439,6 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i
}
return true;
};
qmService.getInputType = function(unitAbbreviatedName, valence, variableName){
var inputType = 'value';
if(variableName === 'Blood Pressure'){
inputType = 'bloodPressure';
}
if(unitAbbreviatedName === '/5'){
inputType = 'oneToFiveNumbers';
if(valence === 'positive'){
inputType = 'happiestFaceIsFive';
}
if(valence === 'negative'){
inputType = 'saddestFaceIsFive';
}
}
if(unitAbbreviatedName === 'yes/no'){
inputType = 'yesOrNo';
}
return inputType;
};
var deleteAllMeasurementsForVariable = function(variableName){
qmService.showBlackRingLoader();
// Delete all measurements for a variable
Expand Down

0 comments on commit 2890f68

Please sign in to comment.