Search in sources :

Example 6 with EntryTag

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

the class CsvImport method importFromVersion3_0.

@SuppressWarnings("ParameterCanBeLocal")
private void importFromVersion3_0(CSVReader reader, String[] nextLine) throws Exception {
    Entry lastEntry = null;
    Meal lastMeal = null;
    while ((nextLine = reader.readNext()) != null) {
        switch(nextLine[0]) {
            case Tag.BACKUP_KEY:
                if (nextLine.length >= 2) {
                    String tagName = nextLine[1];
                    if (TagDao.getInstance().getByName(tagName) == null) {
                        Tag tag = new Tag();
                        tag.setName(nextLine[1]);
                        TagDao.getInstance().createOrUpdate(tag);
                    }
                }
                break;
            case Food.BACKUP_KEY:
                if (nextLine.length >= 5) {
                    String foodName = nextLine[1];
                    Food food = FoodDao.getInstance().get(foodName);
                    if (food == null) {
                        food = new Food();
                        food.setName(foodName);
                        food.setBrand(nextLine[2]);
                        food.setIngredients(nextLine[3]);
                        food.setCarbohydrates(FloatUtils.parseNumber(nextLine[4]));
                        FoodDao.getInstance().createOrUpdate(food);
                    } else if (food.isDeleted()) {
                        // Reactivate previously deleted food that is being re-imported
                        food.setDeletedAt(null);
                        FoodDao.getInstance().createOrUpdate(food);
                    }
                }
                break;
            case Entry.BACKUP_KEY:
                lastMeal = null;
                if (nextLine.length >= 3) {
                    DateTime parsedDateTime = DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]);
                    DateStrategy dateStrategy = this.dateStrategy != null ? this.dateStrategy : defaultDateStrategy;
                    DateTime dateTime = dateStrategy.convertDate(parsedDateTime);
                    lastEntry = new Entry();
                    lastEntry.setDate(dateTime);
                    String note = nextLine[2];
                    lastEntry.setNote(note != null && note.length() > 0 ? note : null);
                    lastEntry = EntryDao.getInstance().createOrUpdate(lastEntry);
                    break;
                }
            case Measurement.BACKUP_KEY:
                if (lastEntry != null && nextLine.length >= 3) {
                    Category category = Helper.valueOf(Category.class, nextLine[1]);
                    if (category != null) {
                        try {
                            Measurement measurement = category.toClass().newInstance();
                            List<Float> valueList = new ArrayList<>();
                            for (int position = 2; position < nextLine.length; position++) {
                                String valueString = nextLine[position];
                                try {
                                    valueList.add(FloatUtils.parseNumber(valueString));
                                } catch (NumberFormatException exception) {
                                    Log.e(TAG, exception.toString());
                                }
                            }
                            float[] values = new float[valueList.size()];
                            for (int position = 0; position < valueList.size(); position++) {
                                values[position] = valueList.get(position);
                            }
                            measurement.setValues(values);
                            measurement.setEntry(lastEntry);
                            MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
                            if (measurement instanceof Meal) {
                                lastMeal = (Meal) measurement;
                            }
                        } catch (InstantiationException exception) {
                            Log.e(TAG, exception.toString());
                        } catch (IllegalAccessException exception) {
                            Log.e(TAG, exception.toString());
                        }
                    }
                }
                break;
            case EntryTag.BACKUP_KEY:
                if (lastEntry != null && nextLine.length >= 2) {
                    Tag tag = TagDao.getInstance().getByName(nextLine[1]);
                    if (tag != null) {
                        EntryTag entryTag = new EntryTag();
                        entryTag.setEntry(lastEntry);
                        entryTag.setTag(tag);
                        EntryTagDao.getInstance().createOrUpdate(entryTag);
                    }
                }
                break;
            case FoodEaten.BACKUP_KEY:
                if (lastMeal != null && nextLine.length >= 3) {
                    Food food = FoodDao.getInstance().get(nextLine[1]);
                    if (food != null) {
                        FoodEaten foodEaten = new FoodEaten();
                        foodEaten.setMeal(lastMeal);
                        foodEaten.setFood(food);
                        foodEaten.setAmountInGrams(FloatUtils.parseNumber(nextLine[2]));
                        FoodEatenDao.getInstance().createOrUpdate(foodEaten);
                    }
                }
                break;
        }
    }
}
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) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) DateStrategy(com.faltenreich.diaguard.feature.export.job.date.DateStrategy) OriginDateStrategy(com.faltenreich.diaguard.feature.export.job.date.OriginDateStrategy) DateTime(org.joda.time.DateTime) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

Example 7 with EntryTag

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

the class LogFetchDataTask method getListItems.

private List<LogListItem> getListItems(DateTime startDate, boolean scrollingDown) {
    List<LogListItem> listItems = new ArrayList<>();
    DateTime date = startDate;
    boolean loadMore = true;
    while (loadMore) {
        List<Entry> entries = EntryDao.getInstance().getEntriesOfDay(date);
        for (Entry entry : entries) {
            List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry);
            entry.setMeasurementCache(measurements);
        }
        if (entries.size() > 0) {
            LogEntryListItem firstListItemEntryOfDay = null;
            for (int entryIndex = 0; entryIndex < entries.size(); entryIndex++) {
                Entry entry = entries.get(entryIndex);
                List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
                List<FoodEaten> foodEaten = new ArrayList<>();
                for (Measurement measurement : entry.getMeasurementCache()) {
                    if (measurement instanceof Meal) {
                        foodEaten.addAll(FoodEatenDao.getInstance().getAll((Meal) measurement));
                    }
                }
                LogEntryListItem listItemEntry = new LogEntryListItem(entry, entryTags, foodEaten);
                if (entryIndex == 0) {
                    firstListItemEntryOfDay = listItemEntry;
                }
                listItemEntry.setFirstListItemEntryOfDay(firstListItemEntryOfDay);
                listItems.add(scrollingDown ? listItems.size() : entryIndex, listItemEntry);
            }
        } else {
            listItems.add(scrollingDown ? listItems.size() : 0, new LogEmptyListItem(date));
        }
        boolean isFirstDayOfMonth = date.dayOfMonth().get() == 1;
        if (isFirstDayOfMonth) {
            listItems.add(scrollingDown ? listItems.size() - 1 : 0, new LogMonthListItem(date));
        }
        loadMore = listItems.size() < (EndlessAdapter.BULK_SIZE);
        date = scrollingDown ? date.plusDays(1) : date.minusDays(1);
    }
    return listItems;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) LogEmptyListItem(com.faltenreich.diaguard.feature.log.empty.LogEmptyListItem) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) LogMonthListItem(com.faltenreich.diaguard.feature.log.month.LogMonthListItem) DateTime(org.joda.time.DateTime) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem)

Example 8 with EntryTag

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

the class PdfNoteFactory method createNote.

@Nullable
public static PdfNote createNote(PdfExportConfig config, Entry entry) {
    List<String> entryNotesAndTagsOfDay = new ArrayList<>();
    List<String> foodOfDay = new ArrayList<>();
    if (config.exportNotes() && !StringUtils.isBlank(entry.getNote())) {
        entryNotesAndTagsOfDay.add(entry.getNote());
    }
    if (config.exportTags()) {
        List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
        for (EntryTag entryTag : entryTags) {
            Tag tag = entryTag.getTag();
            if (tag != null) {
                entryNotesAndTagsOfDay.add(entryTag.getTag().getName());
            }
        }
    }
    if (config.exportFood()) {
        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);
                }
            }
        }
    }
    boolean hasEntryNotesAndTags = !entryNotesAndTagsOfDay.isEmpty();
    boolean hasFood = !foodOfDay.isEmpty();
    if (hasEntryNotesAndTags || hasFood) {
        List<String> notes = new ArrayList<>();
        if (hasEntryNotesAndTags) {
            notes.add(TextUtils.join(", ", entryNotesAndTagsOfDay));
        }
        if (hasFood) {
            notes.add(TextUtils.join(", ", foodOfDay));
        }
        String note = TextUtils.join("\n", notes);
        return new PdfNote(entry.getDate(), note);
    }
    return null;
}
Also used : EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Nullable(androidx.annotation.Nullable)

Example 9 with EntryTag

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

the class LogEntryViewHolder method onBind.

@Override
public void onBind(LogEntryListItem item) {
    Entry entry = item.getEntry();
    List<EntryTag> entryTags = item.getEntryTags();
    List<FoodEaten> foodEatenList = item.getFoodEatenList();
    getBinding().dateLabel.setText(entry.getDate().toString("HH:mm"));
    TextView noteLabel = getBinding().noteLabel;
    if (entry.getNote() != null && entry.getNote().length() > 0) {
        noteLabel.setVisibility(View.VISIBLE);
        noteLabel.setText(entry.getNote());
    } else {
        noteLabel.setVisibility(View.GONE);
    }
    TextView foodLabel = getBinding().foodLabel;
    if (foodEatenList != null && foodEatenList.size() > 0) {
        List<String> foodNotes = new ArrayList<>();
        for (FoodEaten foodEaten : foodEatenList) {
            String foodEatenAsString = foodEaten.print();
            if (foodEatenAsString != null) {
                foodNotes.add(foodEatenAsString);
            }
        }
        if (foodNotes.size() > 0) {
            foodLabel.setVisibility(View.VISIBLE);
            foodLabel.setText(TextUtils.join("\n", foodNotes));
        } else {
            foodLabel.setVisibility(View.GONE);
        }
    } else {
        foodLabel.setVisibility(View.GONE);
    }
    ChipGroup entryTagChipGroup = getBinding().entryTagChipGroup;
    entryTagChipGroup.setVisibility(entryTags != null && entryTags.size() > 0 ? View.VISIBLE : View.GONE);
    entryTagChipGroup.removeAllViews();
    if (entryTags != null) {
        for (EntryTag entryTag : entryTags) {
            Tag tag = entryTag.getTag();
            if (tag != null) {
                ChipView chipView = new ChipView(getContext());
                chipView.setText(tag.getName());
                chipView.setOnClickListener(view -> listener.onTagSelected(tag, view));
                entryTagChipGroup.addView(chipView);
            }
        }
    }
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout measurementsLayout = getBinding().measurementsLayout;
    if (inflater != null) {
        measurementsLayout.removeAllViews();
        List<Measurement> measurements = entry.getMeasurementCache();
        if (measurements.size() > 0) {
            measurementsLayout.setVisibility(View.VISIBLE);
            for (Measurement measurement : measurements) {
                Category category = measurement.getCategory();
                View viewMeasurement = inflater.inflate(R.layout.list_item_log_measurement, measurementsLayout, false);
                ImageView categoryImage = viewMeasurement.findViewById(R.id.image);
                int imageResourceId = category.getIconImageResourceId();
                categoryImage.setImageDrawable(ContextCompat.getDrawable(getContext(), imageResourceId));
                categoryImage.setColorFilter(ContextCompat.getColor(getContext(), R.color.gray_dark));
                TextView value = viewMeasurement.findViewById(R.id.value);
                value.setText(measurement.print(getContext()));
                if (category == Category.BLOODSUGAR) {
                    BloodSugar bloodSugar = (BloodSugar) measurement;
                    if (PreferenceStore.getInstance().limitsAreHighlighted()) {
                        int backgroundColor = ContextCompat.getColor(getContext(), R.color.green);
                        if (bloodSugar.getMgDl() > PreferenceStore.getInstance().getLimitHyperglycemia()) {
                            backgroundColor = ContextCompat.getColor(getContext(), R.color.red);
                        } else if (bloodSugar.getMgDl() < PreferenceStore.getInstance().getLimitHypoglycemia()) {
                            backgroundColor = ContextCompat.getColor(getContext(), R.color.blue);
                        }
                        categoryImage.setColorFilter(backgroundColor);
                    }
                }
                measurementsLayout.addView(viewMeasurement);
            }
        } else {
            measurementsLayout.setVisibility(View.GONE);
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) ArrayList(java.util.ArrayList) ImageView(android.widget.ImageView) View(android.view.View) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) TextView(android.widget.TextView) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ImageView(android.widget.ImageView) ChipGroup(com.google.android.material.chip.ChipGroup) LinearLayout(android.widget.LinearLayout)

Example 10 with EntryTag

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

EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)11 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)8 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)8 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)8 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)7 ArrayList (java.util.ArrayList)6 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)5 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)5 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)3 DateTime (org.joda.time.DateTime)3 View (android.view.View)2 ImageView (android.widget.ImageView)2 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)2 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)2 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)2 Context (android.content.Context)1 LayoutInflater (android.view.LayoutInflater)1 AutoCompleteTextView (android.widget.AutoCompleteTextView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1