Search in sources :

Example 6 with Food

use of com.faltenreich.diaguard.shared.data.database.entity.Food 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)

Example 7 with Food

use of com.faltenreich.diaguard.shared.data.database.entity.Food in project Diaguard by Faltenreich.

the class FoodSearchViewHolder method onBind.

@Override
protected void onBind(FoodSearchListItem item) {
    Food food = item.getFood();
    getBinding().nameLabel.setText(food.getName());
    getBinding().brandLabel.setText(food.getBrand());
    getBinding().brandLabel.setVisibility(StringUtils.isBlank(food.getBrand()) ? View.GONE : View.VISIBLE);
    getBinding().valueLabel.setText(food.getValueForUi());
    getBinding().recentIndicator.setVisibility(item.getFoodEaten() != null ? View.VISIBLE : View.GONE);
}
Also used : Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

Example 8 with Food

use of com.faltenreich.diaguard.shared.data.database.entity.Food in project Diaguard by Faltenreich.

the class MigrateSodiumTask method doInBackground.

@Override
protected Void doInBackground(Void... voids) {
    List<Food> foodList = FoodDao.getInstance().getAllCommon();
    for (Food food : foodList) {
        Float sodium = food.getSodium();
        food.setSodium(sodium != null && sodium > 0 ? sodium / 1000 : null);
    }
    FoodDao.getInstance().bulkCreateOrUpdate(foodList);
    Log.i(TAG, String.format("Fixed sodium of %d common food items", foodList.size()));
    return null;
}
Also used : Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

Example 9 with Food

use of com.faltenreich.diaguard.shared.data.database.entity.Food in project Diaguard by Faltenreich.

the class FoodInputViewHolder method onBind.

@SuppressLint("RestrictedApi")
@Override
protected void onBind(FoodEaten item) {
    Food food = item.getFood();
    getBinding().nameLabel.setText(food.getName());
    getBinding().valueLabel.setText(String.format("%s %s", food.getValueForUi(), PreferenceStore.getInstance().getLabelForMealPer100g(getContext())));
    getBinding().deleteButton.setContentDescription(String.format(getContext().getString(R.string.remove_placeholder), food.getName()));
    boolean isSet = item.isValid();
    String text = isSet ? String.format("%s %s", FloatUtils.parseFloat(item.getAmountInGrams()), getContext().getString(R.string.grams_milliliters_acronym)) : getContext().getString(R.string.amount);
    int backgroundColor = isSet ? ColorUtils.getBackgroundTertiary(getContext()) : ColorUtils.getPrimaryColor(getContext());
    int textColor = isSet ? ColorUtils.getTextColorPrimary(getContext()) : Color.WHITE;
    AppCompatButton amountButton = getBinding().amountButton;
    amountButton.setText(text);
    amountButton.setSupportBackgroundTintList(ColorStateList.valueOf(backgroundColor));
    amountButton.setTextColor(textColor);
}
Also used : SuppressLint(android.annotation.SuppressLint) Food(com.faltenreich.diaguard.shared.data.database.entity.Food) AppCompatButton(androidx.appcompat.widget.AppCompatButton) SuppressLint(android.annotation.SuppressLint)

Example 10 with Food

use of com.faltenreich.diaguard.shared.data.database.entity.Food in project Diaguard by Faltenreich.

the class FoodDao method search.

public List<Food> search(String query, long page, boolean showCustomFood, boolean showCommonFood, boolean showBrandedFood) {
    if (!showCustomFood && !showCommonFood && !showBrandedFood) {
        return new ArrayList<>();
    }
    try {
        QueryBuilder<Food, Long> queryBuilder = getQueryBuilder().orderByRaw(String.format("%s COLLATE NOCASE", Food.Column.NAME)).orderBy(Food.Column.UPDATED_AT, false).offset(page * BaseDao.PAGE_SIZE).limit(BaseDao.PAGE_SIZE);
        Where<Food, Long> where = queryBuilder.where();
        where.isNull(Food.Column.DELETED_AT);
        if (query != null && query.length() > 0) {
            where.and();
            where.like(Food.Column.NAME, new SelectArg("%" + query + "%"));
        }
        int whereTypeCount = 0;
        if (showCustomFood) {
            where.isNull(Food.Column.LABELS);
            where.isNull(Food.Column.SERVER_ID);
            where.and(2);
            whereTypeCount++;
        }
        if (showCommonFood) {
            where.isNotNull(Food.Column.LABELS);
            where.isNull(Food.Column.SERVER_ID);
            where.and(2);
            whereTypeCount++;
        }
        if (showBrandedFood) {
            where.isNotNull(Food.Column.SERVER_ID);
            whereTypeCount++;
        }
        where.or(whereTypeCount);
        where.and(2);
        return where.query();
    } catch (SQLException exception) {
        Log.e(TAG, exception.toString());
        return new ArrayList<>();
    }
}
Also used : SelectArg(com.j256.ormlite.stmt.SelectArg) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

Aggregations

Food (com.faltenreich.diaguard.shared.data.database.entity.Food)11 ArrayList (java.util.ArrayList)4 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)2 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)2 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)2 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)2 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)2 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)2 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)2 FoodSavedEvent (com.faltenreich.diaguard.shared.event.data.FoodSavedEvent)2 IOException (java.io.IOException)2 DateTime (org.joda.time.DateTime)2 SuppressLint (android.annotation.SuppressLint)1 Nullable (androidx.annotation.Nullable)1 AppCompatButton (androidx.appcompat.widget.AppCompatButton)1 DateStrategy (com.faltenreich.diaguard.feature.export.job.date.DateStrategy)1 OriginDateStrategy (com.faltenreich.diaguard.feature.export.job.date.OriginDateStrategy)1 ProductDto (com.faltenreich.diaguard.feature.food.networking.dto.ProductDto)1 SelectArg (com.j256.ormlite.stmt.SelectArg)1 CSVReader (com.opencsv.CSVReader)1