use of com.faltenreich.diaguard.shared.data.database.entity.FoodEaten in project Diaguard by Faltenreich.
the class MeasurementDao method delete.
@Override
public int delete(M object) {
if (object.getCategory() == Category.MEAL) {
Meal meal = (Meal) object;
for (FoodEaten foodEaten : meal.getFoodEaten()) {
meal.getFoodEatenCache().add(foodEaten);
FoodEatenDao.getInstance().delete(foodEaten);
}
}
return super.delete(object);
}
use of com.faltenreich.diaguard.shared.data.database.entity.FoodEaten in project Diaguard by Faltenreich.
the class MeasurementDao method getAvgMeasurement.
public Measurement getAvgMeasurement(Category category, Interval interval) {
long daysBetween = interval.toDuration().getStandardDays() + 1;
switch(category) {
case BLOODSUGAR:
BloodSugar bloodSugar = new BloodSugar();
bloodSugar.setMgDl(function(SqlFunction.AVG, BloodSugar.Column.MGDL, interval));
return bloodSugar;
case INSULIN:
Insulin insulin = new Insulin();
insulin.setBolus(function(SqlFunction.SUM, Insulin.Column.BOLUS, interval) / daysBetween);
insulin.setBasal(function(SqlFunction.SUM, Insulin.Column.BASAL, interval) / daysBetween);
insulin.setCorrection(function(SqlFunction.SUM, Insulin.Column.CORRECTION, interval) / daysBetween);
return insulin;
case MEAL:
Meal meal = new Meal();
float avg = function(SqlFunction.SUM, Meal.Column.CARBOHYDRATES, interval) / daysBetween;
float foodEatenSum = 0;
List<FoodEaten> foodEatenList = FoodEatenDao.getInstance().getAll(interval);
for (FoodEaten foodEaten : foodEatenList) {
foodEatenSum += foodEaten.getCarbohydrates();
}
avg = avg + (foodEatenSum / daysBetween);
meal.setCarbohydrates(avg);
return meal;
case ACTIVITY:
Activity activity = new Activity();
activity.setMinutes((int) (function(SqlFunction.SUM, Activity.Column.MINUTES, interval) / daysBetween));
return activity;
case HBA1C:
HbA1c hbA1c = new HbA1c();
hbA1c.setPercent(function(SqlFunction.AVG, HbA1c.Column.PERCENT, interval));
return hbA1c;
case WEIGHT:
Weight weight = new Weight();
weight.setKilogram(function(SqlFunction.AVG, Weight.Column.KILOGRAM, interval));
return weight;
case PULSE:
Pulse pulse = new Pulse();
pulse.setFrequency(function(SqlFunction.AVG, Pulse.Column.FREQUENCY, interval));
return pulse;
case PRESSURE:
Pressure pressure = new Pressure();
pressure.setSystolic(function(SqlFunction.AVG, Pressure.Column.SYSTOLIC, interval));
pressure.setDiastolic(function(SqlFunction.AVG, Pressure.Column.DIASTOLIC, interval));
return pressure;
case OXYGEN_SATURATION:
OxygenSaturation oxygenSaturation = new OxygenSaturation();
oxygenSaturation.setPercent(function(SqlFunction.AVG, OxygenSaturation.Column.PERCENT, interval));
return oxygenSaturation;
default:
return null;
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.FoodEaten in project Diaguard by Faltenreich.
the class EntryEditFragment method submit.
private void submit() {
Entry entry = viewModel.getEntry();
List<Measurement> measurements = viewModel.getMeasurements();
entry.setMeasurementCache(measurements);
boolean isNewEntry = !entry.isPersisted();
entry = EntryDao.getInstance().createOrUpdate(entry);
for (Measurement measurement : EntryDao.getInstance().getMeasurements(entry)) {
boolean isObsolete = !measurements.contains(measurement);
if (isObsolete) {
MeasurementDao.getInstance(measurement.getClass()).delete(measurement);
}
}
for (Measurement measurement : measurements) {
MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
}
List<Tag> tags = new ArrayList<>();
List<EntryTag> entryTags = new ArrayList<>();
for (int index = 0; index < tagListView.getChildCount(); index++) {
View view = tagListView.getChildAt(index);
if (view.getTag() instanceof Tag) {
Tag tag = (Tag) view.getTag();
if (tag.getId() < 0) {
tag = TagDao.getInstance().createOrUpdate(tag);
Tag legacy = TagDao.getInstance().getByName(tag.getName());
if (legacy != null) {
tag.setId(legacy.getId());
}
}
tag.setUpdatedAt(DateTime.now());
tags.add(tag);
EntryTag entryTag = new EntryTag();
entryTag.setEntry(entry);
entryTag.setTag(tag);
entryTags.add(entryTag);
}
}
TagDao.getInstance().bulkCreateOrUpdate(tags);
// TODO: Update instead of delete
EntryTagDao.getInstance().deleteAll(entry);
EntryTagDao.getInstance().bulkCreateOrUpdate(entryTags);
List<FoodEaten> foodEatenList = getFoodEaten();
// Force update in order to synchronize the measurement cache
entry = EntryDao.getInstance().getById(entry.getId());
entry.setMeasurementCache(EntryDao.getInstance().getMeasurements(entry));
if (isNewEntry) {
Toast.makeText(getContext(), getString(R.string.entry_added), Toast.LENGTH_LONG).show();
Events.post(new EntryAddedEvent(entry, entryTags, foodEatenList));
} else {
Events.post(new EntryUpdatedEvent(entry, entryTags, foodEatenList));
}
int alarmInMinutes = viewModel.getAlarmInMinutes();
if (alarmInMinutes > 0) {
AlarmUtils.setAlarm(alarmInMinutes * DateTimeConstants.MILLIS_PER_MINUTE);
}
finish();
}
use of com.faltenreich.diaguard.shared.data.database.entity.FoodEaten in project Diaguard by Faltenreich.
the class EntryEditFragment method getFoodEaten.
private List<FoodEaten> getFoodEaten() {
for (int index = 0; index < measurementContainer.getChildCount(); index++) {
View view = measurementContainer.getChildAt(index);
if (view instanceof MeasurementView) {
MeasurementView<?> measurementView = ((MeasurementView<?>) view);
Measurement measurement = measurementView.getMeasurement();
if (measurement instanceof Meal) {
List<FoodEaten> foodEatenList = new ArrayList<>();
for (FoodEaten foodEaten : ((Meal) measurement).getFoodEatenCache()) {
if (foodEaten.isValid()) {
foodEatenList.add(foodEaten);
}
}
return foodEatenList;
}
}
}
return new ArrayList<>();
}
use of com.faltenreich.diaguard.shared.data.database.entity.FoodEaten in project Diaguard by Faltenreich.
the class CalculatorFragment method storeValues.
private void storeValues(float mgDl, float carbohydrates, float bolus, float correction) {
DateTime now = DateTime.now();
Entry entry = new Entry();
entry.setDate(now);
EntryDao.getInstance().createOrUpdate(entry);
BloodSugar bloodSugar = new BloodSugar();
bloodSugar.setMgDl(mgDl);
bloodSugar.setEntry(entry);
MeasurementDao.getInstance(BloodSugar.class).createOrUpdate(bloodSugar);
List<FoodEaten> foodEatenList = new ArrayList<>();
if (carbohydrates > 0) {
FoodInputView foodInputView = getBinding().foodInputView;
foodEatenList.addAll(foodInputView.getFoodEatenList());
meal.setCarbohydrates(foodInputView.getInputCarbohydrates());
meal.setEntry(entry);
MeasurementDao.getInstance(Meal.class).createOrUpdate(meal);
for (FoodEaten foodEaten : foodEatenList) {
if (foodEaten.getAmountInGrams() > 0) {
foodEaten.setMeal(meal);
FoodEatenDao.getInstance().createOrUpdate(foodEaten);
}
}
}
if (bolus > 0 || correction > 0) {
Insulin insulin = new Insulin();
insulin.setBolus(bolus);
insulin.setCorrection(correction);
insulin.setEntry(entry);
MeasurementDao.getInstance(Insulin.class).createOrUpdate(insulin);
}
Events.post(new EntryAddedEvent(entry, null, foodEatenList));
openEntry(entry);
clearInput();
update();
}
Aggregations