Search in sources :

Example 6 with CategoryValueListItem

use of com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem in project Diaguard by Faltenreich.

the class TimelineDayFragment method invalidateList.

public void invalidateList() {
    if (isAdded() && !data.needsListData()) {
        Log.d(TAG, "Invalidating view for list on " + data.getDay().toString());
        List<CategoryValueListItem> valueListItems = data.getListData();
        if (valueAdapter.getItemCount() > 0) {
            for (int index = 0; index < valueListItems.size(); index++) {
                CategoryValueListItem listItem = valueListItems.get(index);
                RecyclerView.ViewHolder viewHolder = valueListView.findViewHolderForAdapterPosition(index);
                if (viewHolder instanceof CategoryValueViewHolder) {
                    valueAdapter.setItem(listItem, index);
                    // We access the ViewHolder directly for better performance compared to notifyItem(Range)Changed
                    CategoryValueViewHolder categoryValueViewHolder = (CategoryValueViewHolder) viewHolder;
                    categoryValueViewHolder.bind(listItem);
                }
            }
        } else {
            for (Category category : categories) {
                imageAdapter.addItem(new CategoryImageListItem(category));
            }
            imageAdapter.notifyDataSetChanged();
            // Other notify methods lead to rendering issues on view paging
            valueAdapter.addItems(valueListItems);
            valueAdapter.notifyDataSetChanged();
        }
    }
}
Also used : CategoryImageListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryImageListItem) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) RecyclerView(androidx.recyclerview.widget.RecyclerView) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) CategoryValueViewHolder(com.faltenreich.diaguard.feature.timeline.table.CategoryValueViewHolder)

Example 7 with CategoryValueListItem

use of com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem in project Diaguard by Faltenreich.

the class EntryDao method getAverageDataTable.

/**
 * @return HashMap with non-null but zeroed and default values for given categories and time periods
 */
public LinkedHashMap<Category, CategoryValueListItem[]> getAverageDataTable(DateTime day, Category[] categories, int hoursToSkip) {
    if (day == null) {
        return new LinkedHashMap<>();
    }
    int indices = DateTimeConstants.HOURS_PER_DAY / hoursToSkip;
    // Key: Category, Value: Fixed-size array of values per hour-index
    LinkedHashMap<Category, CategoryValueListItem[]> values = new LinkedHashMap<>();
    for (Category category : categories) {
        values.put(category, new CategoryValueListItem[indices]);
    }
    for (Category category : categories) {
        // Key: Hour-index, Value: Values of hour-index
        LinkedHashMap<Integer, List<CategoryValueListItem>> valuesOfHours = new LinkedHashMap<>();
        for (int index = 0; index < indices; index++) {
            valuesOfHours.put(index, new ArrayList<>());
        }
        List<Measurement> measurements = MeasurementDao.getInstance(category.toClass()).getMeasurements(day);
        for (Measurement measurement : measurements) {
            int index = measurement.getEntry().getDate().hourOfDay().get() / hoursToSkip;
            CategoryValueListItem item = new CategoryValueListItem(category);
            switch(category) {
                case INSULIN:
                    Insulin insulin = (Insulin) measurement;
                    item.setValueOne(insulin.getBolus());
                    item.setValueTwo(insulin.getCorrection());
                    item.setValueThree(insulin.getBasal());
                    break;
                case PRESSURE:
                    Pressure pressure = (Pressure) measurement;
                    item.setValueOne(pressure.getSystolic());
                    item.setValueTwo(pressure.getDiastolic());
                    break;
                default:
                    float value = category.stackValues() ? ArrayUtils.sum(measurement.getValues()) : ArrayUtils.avg(measurement.getValues());
                    if (category == Category.MEAL) {
                        for (FoodEaten foodEaten : ((Meal) measurement).getFoodEaten()) {
                            value += foodEaten.getCarbohydrates();
                        }
                    }
                    item.setValueOne(value);
                    break;
            }
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            if (valuesOfHour == null) {
                valuesOfHours.put(index, new ArrayList<>());
            }
            valuesOfHours.get(index).add(item);
        }
        // Average for old values
        for (int index = 0; index < indices; index++) {
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            CategoryValueListItem value = category.stackValues() ? CategoryListItemUtils.sum(category, valuesOfHour) : CategoryListItemUtils.avg(category, valuesOfHour);
            values.get(category)[index] = value;
        }
    }
    return values;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) Pressure(com.faltenreich.diaguard.shared.data.database.entity.Pressure) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CategoryValueListItem (com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem)7 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)6 ArrayList (java.util.ArrayList)6 List (java.util.List)4 Context (android.content.Context)3 Cell (com.pdfjet.Cell)3 Point (com.pdfjet.Point)3 LinkedHashMap (java.util.LinkedHashMap)3 CellBuilder (com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder)2 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 PdfExportConfig (com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfExportConfig)1 PdfNote (com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfNote)1 DayChartData (com.faltenreich.diaguard.feature.timeline.chart.DayChartData)1 CategoryImageListItem (com.faltenreich.diaguard.feature.timeline.table.CategoryImageListItem)1 CategoryValueViewHolder (com.faltenreich.diaguard.feature.timeline.table.CategoryValueViewHolder)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 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)1