use of com.faltenreich.diaguard.shared.data.database.entity.Meal in project Diaguard by Faltenreich.
the class EntryDao method getAverageDataTable.
/**
* @return HashMap with non-null but zeroed and default values for given categories and time periods
*/
public LinkedHashMap<Category, CategoryValueListItem[]> getAverageDataTable(DateTime day, Category[] categories, int hoursToSkip) {
if (day == null) {
return new LinkedHashMap<>();
}
int indices = DateTimeConstants.HOURS_PER_DAY / hoursToSkip;
// Key: Category, Value: Fixed-size array of values per hour-index
LinkedHashMap<Category, CategoryValueListItem[]> values = new LinkedHashMap<>();
for (Category category : categories) {
values.put(category, new CategoryValueListItem[indices]);
}
for (Category category : categories) {
// Key: Hour-index, Value: Values of hour-index
LinkedHashMap<Integer, List<CategoryValueListItem>> valuesOfHours = new LinkedHashMap<>();
for (int index = 0; index < indices; index++) {
valuesOfHours.put(index, new ArrayList<>());
}
List<Measurement> measurements = MeasurementDao.getInstance(category.toClass()).getMeasurements(day);
for (Measurement measurement : measurements) {
int index = measurement.getEntry().getDate().hourOfDay().get() / hoursToSkip;
CategoryValueListItem item = new CategoryValueListItem(category);
switch(category) {
case INSULIN:
Insulin insulin = (Insulin) measurement;
item.setValueOne(insulin.getBolus());
item.setValueTwo(insulin.getCorrection());
item.setValueThree(insulin.getBasal());
break;
case PRESSURE:
Pressure pressure = (Pressure) measurement;
item.setValueOne(pressure.getSystolic());
item.setValueTwo(pressure.getDiastolic());
break;
default:
float value = category.stackValues() ? ArrayUtils.sum(measurement.getValues()) : ArrayUtils.avg(measurement.getValues());
if (category == Category.MEAL) {
for (FoodEaten foodEaten : ((Meal) measurement).getFoodEaten()) {
value += foodEaten.getCarbohydrates();
}
}
item.setValueOne(value);
break;
}
List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
if (valuesOfHour == null) {
valuesOfHours.put(index, new ArrayList<>());
}
valuesOfHours.get(index).add(item);
}
// Average for old values
for (int index = 0; index < indices; index++) {
List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
CategoryValueListItem value = category.stackValues() ? CategoryListItemUtils.sum(category, valuesOfHour) : CategoryListItemUtils.avg(category, valuesOfHour);
values.get(category)[index] = value;
}
}
return values;
}
use of com.faltenreich.diaguard.shared.data.database.entity.Meal in project Diaguard by Faltenreich.
the class CalculatorFragment method showResult.
// Values are normalized
private void showResult(String formula, String formulaContent, final float bloodSugar, final float meal, final float bolus, final float correction) {
float insulin = bolus + correction;
// Build AlertDialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View viewPopup = inflater.inflate(R.layout.dialog_calculator_result, null);
final ViewGroup infoLayout = viewPopup.findViewById(R.id.dialog_calculator_result_info);
TextView textViewFormula = viewPopup.findViewById(R.id.dialog_calculator_result_formula);
textViewFormula.setText(formula);
TextView textViewFormulaContent = viewPopup.findViewById(R.id.dialog_calculator_result_formula_content);
textViewFormulaContent.setText(formulaContent);
// Handle negative insulin
TextView textViewInfo = viewPopup.findViewById(R.id.textViewInfo);
if (insulin <= 0) {
// Advice skipping bolus
viewPopup.findViewById(R.id.result).setVisibility(View.GONE);
textViewInfo.setVisibility(View.VISIBLE);
if (insulin < -1) {
// Advice consuming carbohydrates
textViewInfo.setText(String.format("%s %s", textViewInfo.getText().toString(), getString(R.string.bolus_no2)));
}
} else {
viewPopup.findViewById(R.id.result).setVisibility(View.VISIBLE);
textViewInfo.setVisibility(View.GONE);
}
TextView textViewValue = viewPopup.findViewById(R.id.textViewResult);
textViewValue.setText(FloatUtils.parseFloat(insulin));
TextView textViewUnit = viewPopup.findViewById(R.id.textViewUnit);
textViewUnit.setText(PreferenceStore.getInstance().getUnitAcronym(Category.INSULIN));
dialogBuilder.setView(viewPopup).setTitle(R.string.bolus).setNegativeButton(R.string.info, (dialog, id) -> {
/* Set down below */
}).setPositiveButton(R.string.store_values, (dialog, id) -> storeValues(bloodSugar, meal, bolus, correction)).setNeutralButton(R.string.back, (dialog, id) -> dialog.cancel());
AlertDialog dialog = dialogBuilder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(view -> {
infoLayout.setVisibility(View.VISIBLE);
view.setEnabled(false);
});
}
use of com.faltenreich.diaguard.shared.data.database.entity.Meal in project Diaguard by Faltenreich.
the class CalculatorFragment method onCreate.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
meal = new Meal();
}
Aggregations