Search in sources :

Example 1 with CategoryValueListItem

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

the class TimelineDayFragment method invalidateData.

private void invalidateData() {
    if (getContext() == null) {
        return;
    }
    if (data.needsChartData()) {
        Log.d(TAG, "Invalidating data for chart on " + data.getDay().toString());
        DataLoader.getInstance().load(getContext(), new DataLoaderListener<DayChartData>() {

            @Override
            public DayChartData onShouldLoad(Context context) {
                List<Measurement> values = new ArrayList<>();
                List<Entry> entries = EntryDao.getInstance().getEntriesOfDay(data.getDay());
                if (entries != null && entries.size() > 0) {
                    for (Entry entry : entries) {
                        // TODO: Improve performance by using transaction / bulk fetch
                        List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry, new Category[] { Category.BLOODSUGAR });
                        values.addAll(measurements);
                    }
                }
                return new DayChartData(context, PreferenceStore.getInstance().showDotsInTimeline(), PreferenceStore.getInstance().showLinesInTimeline(), values);
            }

            @Override
            public void onDidLoad(DayChartData chartData) {
                data.setChartData(chartData);
                invalidateChart();
            }
        });
    } else {
        invalidateChart();
    }
    if (data.needsListData()) {
        Log.d(TAG, "Invalidating data for list on " + data.getDay().toString());
        DataLoader.getInstance().load(getContext(), new DataLoaderListener<List<CategoryValueListItem>>() {

            @Override
            public List<CategoryValueListItem> onShouldLoad(Context context) {
                List<CategoryValueListItem> listItems = new ArrayList<>();
                LinkedHashMap<Category, CategoryValueListItem[]> values = EntryDao.getInstance().getAverageDataTable(data.getDay(), categories, SKIP_EVERY_X_HOUR);
                for (Map.Entry<Category, CategoryValueListItem[]> mapEntry : values.entrySet()) {
                    Collections.addAll(listItems, mapEntry.getValue());
                }
                return listItems;
            }

            @Override
            public void onDidLoad(List<CategoryValueListItem> listData) {
                data.setListData(listData);
                invalidateList();
            }
        });
    } else {
        invalidateList();
    }
}
Also used : Context(android.content.Context) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) DayChartData(com.faltenreich.diaguard.feature.timeline.chart.DayChartData) ArrayList(java.util.ArrayList) List(java.util.List) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with CategoryValueListItem

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

the class PdfTable method init.

private void init() {
    PdfExportConfig config = cache.getConfig();
    Context context = config.getContext();
    List<List<Cell>> data = new ArrayList<>();
    List<Cell> cells = new ArrayList<>();
    Cell headerCell = new CellBuilder(new Cell(cache.getFontBold())).setWidth(getLabelWidth()).setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime())).build();
    cells.add(headerCell);
    float cellWidth = (cache.getPage().getWidth() - getLabelWidth()) / (DateTimeConstants.HOURS_PER_DAY / 2f);
    for (int hour = 0; hour < DateTimeConstants.HOURS_PER_DAY; hour += PdfTable.HOURS_TO_SKIP) {
        Cell hourCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(cellWidth).setText(Integer.toString(hour)).setForegroundColor(Color.gray).setTextAlignment(Align.CENTER).build();
        cells.add(hourCell);
    }
    data.add(cells);
    LinkedHashMap<Category, CategoryValueListItem[]> values = EntryDao.getInstance().getAverageDataTable(cache.getDateTime(), config.getCategories(), HOURS_TO_SKIP);
    int rowIndex = 0;
    for (Category category : values.keySet()) {
        CategoryValueListItem[] items = values.get(category);
        if (items != null) {
            String label = context.getString(category.getStringAcronymResId());
            int backgroundColor = rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white;
            switch(category) {
                case INSULIN:
                    if (config.splitInsulin()) {
                        data.add(createMeasurementRows(cache, items, cellWidth, 0, label + " " + context.getString(R.string.bolus), backgroundColor));
                        data.add(createMeasurementRows(cache, items, cellWidth, 1, label + " " + context.getString(R.string.correction), backgroundColor));
                        data.add(createMeasurementRows(cache, items, cellWidth, 2, label + " " + context.getString(R.string.basal), backgroundColor));
                    } else {
                        data.add(createMeasurementRows(cache, items, cellWidth, -1, label, backgroundColor));
                    }
                    break;
                case PRESSURE:
                    data.add(createMeasurementRows(cache, items, cellWidth, 0, label + " " + context.getString(R.string.systolic_acronym), backgroundColor));
                    data.add(createMeasurementRows(cache, items, cellWidth, 1, label + " " + context.getString(R.string.diastolic_acronym), backgroundColor));
                    break;
                default:
                    data.add(createMeasurementRows(cache, items, cellWidth, 0, label, backgroundColor));
                    break;
            }
            rowIndex++;
        }
    }
    if (config.exportNotes() || config.exportTags() || config.exportFood()) {
        List<PdfNote> pdfNotes = new ArrayList<>();
        for (Entry entry : entriesOfDay) {
            PdfNote pdfNote = PdfNoteFactory.createNote(config, entry);
            if (pdfNote != null) {
                pdfNotes.add(pdfNote);
            }
        }
        data.addAll(CellFactory.createRowsForNotes(cache, pdfNotes, getLabelWidth()));
    }
    boolean hasData = data.size() > 1;
    if (!hasData) {
        data.add(CellFactory.createEmptyRow(cache));
    }
    try {
        table.setData(data);
    } catch (Exception exception) {
        Log.e(TAG, exception.toString());
    }
}
Also used : PdfExportConfig(com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfExportConfig) Context(android.content.Context) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) ArrayList(java.util.ArrayList) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Point(com.pdfjet.Point) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) CellBuilder(com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Cell(com.pdfjet.Cell) PdfNote(com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfNote)

Example 3 with CategoryValueListItem

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

the class PdfTable method createMeasurementRows.

private List<Cell> createMeasurementRows(PdfExportCache cache, CategoryValueListItem[] items, float cellWidth, int valueIndex, String label, int backgroundColor) {
    List<Cell> cells = new ArrayList<>();
    Cell labelCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(getLabelWidth()).setText(label).setBackgroundColor(backgroundColor).setForegroundColor(Color.gray).build();
    cells.add(labelCell);
    for (CategoryValueListItem item : items) {
        Category category = item.getCategory();
        float value = 0;
        switch(valueIndex) {
            case -1:
                value = item.getValueTotal();
                break;
            case 0:
                value = item.getValueOne();
                break;
            case 1:
                value = item.getValueTwo();
                break;
            case 2:
                value = item.getValueThree();
                break;
        }
        int textColor = Color.black;
        if (category == Category.BLOODSUGAR && cache.getConfig().highlightLimits()) {
            if (value > PreferenceStore.getInstance().getLimitHyperglycemia()) {
                textColor = cache.getColorHyperglycemia();
            } else if (value < PreferenceStore.getInstance().getLimitHypoglycemia()) {
                textColor = cache.getColorHypoglycemia();
            }
        }
        float customValue = PreferenceStore.getInstance().formatDefaultToCustomUnit(category, value);
        String text = customValue > 0 ? FloatUtils.parseFloat(customValue) : "";
        Cell measurementCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(cellWidth).setText(text).setTextAlignment(Align.CENTER).setBackgroundColor(backgroundColor).setForegroundColor(textColor).build();
        cells.add(measurementCell);
    }
    return cells;
}
Also used : Category(com.faltenreich.diaguard.shared.data.database.entity.Category) CellBuilder(com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder) ArrayList(java.util.ArrayList) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Cell(com.pdfjet.Cell) Point(com.pdfjet.Point)

Example 4 with CategoryValueListItem

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

the class PdfTimeline method createRowForMeasurements.

private List<Cell> createRowForMeasurements(Category category, CategoryValueListItem[] values, int rowIndex, int valueIndex, String label) {
    List<Cell> row = new ArrayList<>();
    Cell titleCell = new Cell(cache.getFontNormal());
    titleCell.setText(label);
    titleCell.setWidth(getLabelWidth());
    titleCell.setBgColor(rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white);
    titleCell.setFgColor(Color.gray);
    titleCell.setPenColor(Color.gray);
    row.add(titleCell);
    for (CategoryValueListItem item : values) {
        Cell valueCell = new Cell(cache.getFontNormal());
        float value = 0;
        switch(valueIndex) {
            case -1:
                value = item.getValueTotal();
                break;
            case 0:
                value = item.getValueOne();
                break;
            case 1:
                value = item.getValueTwo();
                break;
            case 2:
                value = item.getValueThree();
                break;
        }
        float customValue = PreferenceStore.getInstance().formatDefaultToCustomUnit(category, value);
        String text = customValue > 0 ? FloatUtils.parseFloat(customValue) : "";
        valueCell.setText(text);
        valueCell.setWidth((cache.getPage().getWidth() - getLabelWidth()) / (DateTimeConstants.HOURS_PER_DAY / HOUR_INTERVAL));
        valueCell.setBgColor(rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white);
        valueCell.setFgColor(Color.black);
        valueCell.setPenColor(Color.gray);
        valueCell.setTextAlignment(Align.CENTER);
        row.add(valueCell);
    }
    return row;
}
Also used : ArrayList(java.util.ArrayList) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Cell(com.pdfjet.Cell)

Example 5 with CategoryValueListItem

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

the class PdfTimeline method initTable.

private void initTable() {
    List<List<Cell>> tableData = new ArrayList<>();
    Context context = cache.getContext();
    int rowIndex = 0;
    for (Map.Entry<Category, CategoryValueListItem[]> entry : measurements.entrySet()) {
        Category category = entry.getKey();
        CategoryValueListItem[] values = entry.getValue();
        String label = context.getString(category.getStringAcronymResId());
        switch(category) {
            case INSULIN:
                if (cache.getConfig().splitInsulin()) {
                    tableData.add(createRowForMeasurements(category, values, rowIndex, 0, label + " " + context.getString(R.string.bolus)));
                    tableData.add(createRowForMeasurements(category, values, rowIndex, 1, label + " " + context.getString(R.string.correction)));
                    tableData.add(createRowForMeasurements(category, values, rowIndex, 2, label + " " + context.getString(R.string.basal)));
                } else {
                    tableData.add(createRowForMeasurements(category, values, rowIndex, -1, label));
                }
                break;
            case PRESSURE:
                tableData.add(createRowForMeasurements(category, values, rowIndex, 0, label + " " + context.getString(R.string.systolic_acronym)));
                tableData.add(createRowForMeasurements(category, values, rowIndex, 1, label + " " + context.getString(R.string.diastolic_acronym)));
                break;
            default:
                tableData.add(createRowForMeasurements(category, values, rowIndex, -1, label));
        }
        rowIndex++;
    }
    tableData.addAll(CellFactory.createRowsForNotes(cache, pdfNotes, getLabelWidth()));
    if (tableData.isEmpty() && !showChartForBloodSugar) {
        tableData.add(CellFactory.createEmptyRow(cache));
    }
    try {
        // Must be executed early to know the table's height
        table.setData(tableData);
    } catch (Exception exception) {
        Log.e(TAG, exception.toString());
    }
}
Also used : Context(android.content.Context) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Point(com.pdfjet.Point)

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