Search in sources :

Example 1 with FoodInputView

use of com.faltenreich.diaguard.feature.food.input.FoodInputView 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();
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) FoodInputView(com.faltenreich.diaguard.feature.food.input.FoodInputView) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) ArrayList(java.util.ArrayList) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) DateTime(org.joda.time.DateTime)

Aggregations

FoodInputView (com.faltenreich.diaguard.feature.food.input.FoodInputView)1 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)1 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)1 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)1 Insulin (com.faltenreich.diaguard.shared.data.database.entity.Insulin)1 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)1 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)1 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1