Search in sources :

Example 11 with Measurement

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

the class CsvImport method importFromVersion2_2.

@SuppressWarnings("ParameterCanBeLocal")
private void importFromVersion2_2(CSVReader reader, String[] nextLine) throws Exception {
    Entry entry = null;
    while ((nextLine = reader.readNext()) != null) {
        String key = nextLine[0];
        if (key.equalsIgnoreCase(Entry.BACKUP_KEY)) {
            entry = new Entry();
            entry.setDate(DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]));
            String note = nextLine[2];
            entry.setNote(note != null && note.length() > 0 ? note : null);
            entry = EntryDao.getInstance().createOrUpdate(entry);
        } else if (key.equalsIgnoreCase(Measurement.BACKUP_KEY) && entry != null) {
            try {
                Category category = Helper.valueOf(Category.class, nextLine[1]);
                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(entry);
                MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
            } catch (InstantiationException exception) {
                Log.e(TAG, exception.toString());
            } catch (IllegalAccessException exception) {
                Log.e(TAG, exception.toString());
            }
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with Measurement

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

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

the class CsvImport method importFromVersion1_0.

private void importFromVersion1_0(CSVReader reader, String[] nextLine) throws Exception {
    while (nextLine != null) {
        Entry entry = new Entry();
        entry.setDate(DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]));
        String note = nextLine[2];
        entry.setNote(note != null && note.length() > 0 ? note : null);
        EntryDao.getInstance().createOrUpdate(entry);
        try {
            CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, nextLine[2]);
            Category category = categoryDeprecated.toUpdate();
            Measurement measurement = category.toClass().newInstance();
            measurement.setValues(FloatUtils.parseNumber(nextLine[0]));
            measurement.setEntry(entry);
            MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
        } catch (InstantiationException exception) {
            Log.e(TAG, exception.toString());
        } catch (IllegalAccessException exception) {
            Log.e(TAG, exception.toString());
        }
        nextLine = reader.readNext();
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) CategoryDeprecated(com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated)

Example 14 with Measurement

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

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

the class PdfTimeline method fetchData.

private void fetchData() {
    bloodSugars = new ArrayList<>();
    pdfNotes = new ArrayList<>();
    for (Entry entry : entriesOfDay) {
        if (showChartForBloodSugar) {
            List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry);
            for (Measurement measurement : measurements) {
                if (measurement instanceof BloodSugar) {
                    bloodSugars.add((BloodSugar) measurement);
                }
            }
        }
        PdfNote pdfNote = PdfNoteFactory.createNote(cache.getConfig(), entry);
        if (pdfNote != null) {
            pdfNotes.add(pdfNote);
        }
    }
    List<Category> categories = new ArrayList<>();
    for (Category category : cache.getConfig().getCategories()) {
        if (category != Category.BLOODSUGAR) {
            categories.add(category);
        }
    }
    measurements = EntryDao.getInstance().getAverageDataTable(cache.getDateTime(), categories.toArray(new Category[0]), HOUR_INTERVAL);
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) ArrayList(java.util.ArrayList) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) PdfNote(com.faltenreich.diaguard.feature.export.job.pdf.meta.PdfNote)

Aggregations

Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)24 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)15 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)12 ArrayList (java.util.ArrayList)12 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)8 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)8 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)6 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)6 View (android.view.View)4 ImageView (android.widget.ImageView)4 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)4 List (java.util.List)4 DateTime (org.joda.time.DateTime)4 AutoCompleteTextView (android.widget.AutoCompleteTextView)3 NestedScrollView (androidx.core.widget.NestedScrollView)3 MeasurementView (com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView)3 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)3 CategoryDeprecated (com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated)3 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)2 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)2