Search in sources :

Example 1 with FoodSavedEvent

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

the class FoodEditFragment method store.

private void store() {
    if (isValid()) {
        if (food == null) {
            food = new Food();
        }
        food.setLanguageCode(Helper.getLanguageCode());
        food.setName(getBinding().nameInput.getText());
        food.setBrand(getBinding().brandInput.getText());
        food.setIngredients(getBinding().ingredientsInput.getText());
        // FIXME: 4-digit carbohydrates are stored as 1-digit with 3 decimal places
        for (Map.Entry<Food.Nutrient, Float> entry : getBinding().nutrientInputLayout.getValues().entrySet()) {
            Food.Nutrient nutrient = entry.getKey();
            Float value = entry.getValue();
            // Auto-fill carbohydrates for user
            if (nutrient == Food.Nutrient.CARBOHYDRATES && value == null) {
                value = 0f;
            }
            nutrient.setValue(food, value != null ? value : -1);
        }
        FoodDao.getInstance().createOrUpdate(food);
        Events.post(new FoodSavedEvent(food));
        finish();
    }
}
Also used : FoodSavedEvent(com.faltenreich.diaguard.shared.event.data.FoodSavedEvent) Map(java.util.Map) Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

Example 2 with FoodSavedEvent

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

the class FoodSearchFragment method onEvent.

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(final FoodDeletedEvent event) {
    removeItem(event.context);
    ViewUtils.showSnackbar(getView(), getString(R.string.food_deleted), v -> {
        Food food = event.context;
        food.setDeletedAt(null);
        FoodDao.getInstance().createOrUpdate(food);
        Events.post(new FoodSavedEvent(food));
    });
}
Also used : FoodSavedEvent(com.faltenreich.diaguard.shared.event.data.FoodSavedEvent) Food(com.faltenreich.diaguard.shared.data.database.entity.Food) Subscribe(org.greenrobot.eventbus.Subscribe)

Aggregations

Food (com.faltenreich.diaguard.shared.data.database.entity.Food)2 FoodSavedEvent (com.faltenreich.diaguard.shared.event.data.FoodSavedEvent)2 Map (java.util.Map)1 Subscribe (org.greenrobot.eventbus.Subscribe)1