Search in sources :

Example 1 with EntryAddedEvent

use of com.faltenreich.diaguard.shared.event.data.EntryAddedEvent 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();
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) NestedScrollView(androidx.core.widget.NestedScrollView) ImageView(android.widget.ImageView) View(android.view.View) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) EntryUpdatedEvent(com.faltenreich.diaguard.shared.event.data.EntryUpdatedEvent) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)

Example 2 with EntryAddedEvent

use of com.faltenreich.diaguard.shared.event.data.EntryAddedEvent 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)

Example 3 with EntryAddedEvent

use of com.faltenreich.diaguard.shared.event.data.EntryAddedEvent in project Diaguard by Faltenreich.

the class BaseFragment method onEvent.

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(final EntryDeletedEvent event) {
    if (getView() == null) {
        return;
    }
    ViewUtils.showSnackbar(getView(), getString(R.string.entry_deleted), v -> {
        Entry entry = event.context;
        EntryDao.getInstance().createOrUpdate(entry);
        for (Measurement measurement : entry.getMeasurementCache()) {
            measurement.setEntry(entry);
            // noinspection unchecked
            MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
        }
        for (EntryTag entryTag : event.entryTags) {
            entryTag.setEntry(entry);
            EntryTagDao.getInstance().createOrUpdate(entryTag);
        }
        Events.post(new EntryAddedEvent(entry, event.entryTags, event.foodEatenList));
    });
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) Subscribe(org.greenrobot.eventbus.Subscribe)

Aggregations

Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)3 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)3 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)2 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)2 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)2 ArrayList (java.util.ArrayList)2 View (android.view.View)1 AutoCompleteTextView (android.widget.AutoCompleteTextView)1 ImageView (android.widget.ImageView)1 NestedScrollView (androidx.core.widget.NestedScrollView)1 MeasurementView (com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView)1 FoodInputView (com.faltenreich.diaguard.feature.food.input.FoodInputView)1 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)1 Insulin (com.faltenreich.diaguard.shared.data.database.entity.Insulin)1 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)1 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)1 EntryUpdatedEvent (com.faltenreich.diaguard.shared.event.data.EntryUpdatedEvent)1 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)1 Subscribe (org.greenrobot.eventbus.Subscribe)1 DateTime (org.joda.time.DateTime)1