Search in sources :

Example 1 with CellBuilder

use of com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder 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 2 with CellBuilder

use of com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder 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 3 with CellBuilder

use of com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder in project Diaguard by Faltenreich.

the class PdfLog method getRow.

private List<Cell> getRow(PdfExportCache cache, String title, String subtitle, String description, int backgroundColor, int foregroundColor) {
    List<Cell> entryRow = new ArrayList<>();
    float width = cache.getPage().getWidth();
    Cell titleCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(PdfLog.TIME_WIDTH).setText(title).setBackgroundColor(backgroundColor).setForegroundColor(Color.gray).build();
    entryRow.add(titleCell);
    Cell subtitleCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(getLabelWidth()).setText(subtitle).setBackgroundColor(backgroundColor).setForegroundColor(Color.gray).build();
    entryRow.add(subtitleCell);
    Cell descriptionCell = new CellBuilder(new MultilineCell(cache.getFontNormal())).setWidth(width - titleCell.getWidth() - subtitleCell.getWidth()).setText(description).setBackgroundColor(backgroundColor).setForegroundColor(foregroundColor).build();
    entryRow.add(descriptionCell);
    return entryRow;
}
Also used : MultilineCell(com.faltenreich.diaguard.feature.export.job.pdf.view.MultilineCell) CellBuilder(com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder) ArrayList(java.util.ArrayList) MultilineCell(com.faltenreich.diaguard.feature.export.job.pdf.view.MultilineCell) Cell(com.pdfjet.Cell)

Example 4 with CellBuilder

use of com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder in project Diaguard by Faltenreich.

the class PdfLog method init.

private void init() {
    PdfExportConfig config = cache.getConfig();
    Context context = config.getContext();
    List<List<Cell>> data = new ArrayList<>();
    List<Cell> headerRow = new ArrayList<>();
    Cell headerCell = new CellBuilder(new Cell(cache.getFontBold())).setWidth(getLabelWidth()).setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime())).build();
    headerRow.add(headerCell);
    data.add(headerRow);
    for (Entry entry : entriesOfDay) {
        List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry, cache.getConfig().getCategories());
        entry.setMeasurementCache(measurements);
    }
    int rowIndex = 0;
    for (Entry entry : entriesOfDay) {
        int backgroundColor = rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white;
        int oldSize = data.size();
        String time = entry.getDate().toString("HH:mm");
        for (Measurement measurement : entry.getMeasurementCache()) {
            Category category = measurement.getCategory();
            int textColor = Color.black;
            if (category == Category.BLOODSUGAR && config.highlightLimits()) {
                BloodSugar bloodSugar = (BloodSugar) measurement;
                float value = bloodSugar.getMgDl();
                if (value > PreferenceStore.getInstance().getLimitHyperglycemia()) {
                    textColor = cache.getColorHyperglycemia();
                } else if (value < PreferenceStore.getInstance().getLimitHypoglycemia()) {
                    textColor = cache.getColorHypoglycemia();
                }
            }
            String measurementText = measurement.print(context);
            if (category == Category.MEAL && config.exportFood()) {
                List<String> foodOfDay = new ArrayList<>();
                Meal meal = (Meal) MeasurementDao.getInstance(Meal.class).getMeasurement(entry);
                if (meal != null) {
                    for (FoodEaten foodEaten : FoodEatenDao.getInstance().getAll(meal)) {
                        String foodNote = foodEaten.print();
                        if (foodNote != null) {
                            foodOfDay.add(foodNote);
                        }
                    }
                }
                if (!foodOfDay.isEmpty()) {
                    String foodText = TextUtils.join(", ", foodOfDay);
                    measurementText = String.format("%s\n%s", measurementText, foodText);
                }
            }
            data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(category.getStringAcronymResId()), measurementText, backgroundColor, textColor));
        }
        if (config.exportTags()) {
            List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
            if (!entryTags.isEmpty()) {
                List<String> tagNames = new ArrayList<>();
                for (EntryTag entryTag : entryTags) {
                    Tag tag = entryTag.getTag();
                    if (tag != null) {
                        String tagName = tag.getName();
                        if (!StringUtils.isBlank(tagName)) {
                            tagNames.add(tagName);
                        }
                    }
                }
                data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(R.string.tags), TextUtils.join(", ", tagNames), backgroundColor));
            }
        }
        if (config.exportNotes()) {
            if (!StringUtils.isBlank(entry.getNote())) {
                data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(R.string.note), entry.getNote(), backgroundColor));
            }
        }
        rowIndex++;
    }
    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) 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) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) 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) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) MultilineCell(com.faltenreich.diaguard.feature.export.job.pdf.view.MultilineCell) Cell(com.pdfjet.Cell)

Aggregations

CellBuilder (com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder)4 Cell (com.pdfjet.Cell)4 ArrayList (java.util.ArrayList)4 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)3 Point (com.pdfjet.Point)3 Context (android.content.Context)2 PdfExportConfig (com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfExportConfig)2 MultilineCell (com.faltenreich.diaguard.feature.export.job.pdf.view.MultilineCell)2 CategoryValueListItem (com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem)2 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)2 List (java.util.List)2 PdfNote (com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfNote)1 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)1 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)1 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)1 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)1 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)1 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)1