Search in sources :

Example 1 with Entry

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

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

the class MeasurementDao method getMeasurements.

public List<M> getMeasurements(DateTime day) {
    try {
        if (day == null) {
            return new ArrayList<>();
        }
        DateTime start = day.withTimeAtStartOfDay();
        DateTime end = day.withTime(DateTimeConstants.HOURS_PER_DAY - 1, DateTimeConstants.MINUTES_PER_HOUR - 1, DateTimeConstants.SECONDS_PER_MINUTE - 1, DateTimeConstants.MILLIS_PER_SECOND - 1);
        QueryBuilder<M, Long> measurementQb = getQueryBuilder();
        QueryBuilder<Entry, Long> entryQb = EntryDao.getInstance().getQueryBuilder();
        entryQb.orderBy(Entry.Column.DATE, true).where().gt(Entry.Column.DATE, start).and().lt(Entry.Column.DATE, end);
        return measurementQb.join(entryQb).query();
    } catch (SQLException exception) {
        Log.e(TAG, exception.toString());
        return new ArrayList<>();
    }
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime)

Example 3 with Entry

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

the class EntryDao method search.

@NonNull
public List<Entry> search(@NonNull String query, int page, int pageSize) {
    try {
        query = "%" + query + "%";
        QueryBuilder<Tag, Long> tagQb = TagDao.getInstance().getQueryBuilder();
        tagQb.where().like(Tag.Column.NAME, new SelectArg(query));
        QueryBuilder<EntryTag, Long> entryTagQb = EntryTagDao.getInstance().getQueryBuilder().leftJoinOr(tagQb);
        QueryBuilder<Entry, Long> entryQb = getQueryBuilder().leftJoinOr(entryTagQb).offset((long) (page * pageSize)).limit((long) pageSize).orderBy(Entry.Column.DATE, false);
        entryQb.where().like(Entry.Column.NOTE, new SelectArg(query));
        return entryQb.distinct().query();
    } catch (SQLException exception) {
        Log.e(TAG, exception.toString());
        return new ArrayList<>();
    }
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) SelectArg(com.j256.ormlite.stmt.SelectArg) SQLException(java.sql.SQLException) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) NonNull(androidx.annotation.NonNull)

Example 4 with Entry

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

the class EntryEditFragment method addCategory.

private void addCategory(Category category, boolean atStart) {
    Measurement measurement = ObjectFactory.createFromClass(category.toClass());
    addMeasurement(measurement, atStart);
    Entry entry = viewModel.getEntry();
    int indexInCache = entry.indexInMeasurementCache(category);
    if (indexInCache != -1) {
        entry.getMeasurementCache().set(indexInCache, measurement);
    } else {
        measurement.setEntry(entry);
        entry.getMeasurementCache().add(measurement);
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry)

Example 5 with Entry

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

the class EntryEditFragment method submit.

private void submit() {
    Entry entry = viewModel.getEntry();
    List<Measurement> measurements = viewModel.getMeasurements();
    entry.setMeasurementCache(measurements);
    boolean isNewEntry = !entry.isPersisted();
    entry = EntryDao.getInstance().createOrUpdate(entry);
    for (Measurement measurement : EntryDao.getInstance().getMeasurements(entry)) {
        boolean isObsolete = !measurements.contains(measurement);
        if (isObsolete) {
            MeasurementDao.getInstance(measurement.getClass()).delete(measurement);
        }
    }
    for (Measurement measurement : measurements) {
        MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
    }
    List<Tag> tags = new ArrayList<>();
    List<EntryTag> entryTags = new ArrayList<>();
    for (int index = 0; index < tagListView.getChildCount(); index++) {
        View view = tagListView.getChildAt(index);
        if (view.getTag() instanceof Tag) {
            Tag tag = (Tag) view.getTag();
            if (tag.getId() < 0) {
                tag = TagDao.getInstance().createOrUpdate(tag);
                Tag legacy = TagDao.getInstance().getByName(tag.getName());
                if (legacy != null) {
                    tag.setId(legacy.getId());
                }
            }
            tag.setUpdatedAt(DateTime.now());
            tags.add(tag);
            EntryTag entryTag = new EntryTag();
            entryTag.setEntry(entry);
            entryTag.setTag(tag);
            entryTags.add(entryTag);
        }
    }
    TagDao.getInstance().bulkCreateOrUpdate(tags);
    // TODO: Update instead of delete
    EntryTagDao.getInstance().deleteAll(entry);
    EntryTagDao.getInstance().bulkCreateOrUpdate(entryTags);
    List<FoodEaten> foodEatenList = getFoodEaten();
    // Force update in order to synchronize the measurement cache
    entry = EntryDao.getInstance().getById(entry.getId());
    entry.setMeasurementCache(EntryDao.getInstance().getMeasurements(entry));
    if (isNewEntry) {
        Toast.makeText(getContext(), getString(R.string.entry_added), Toast.LENGTH_LONG).show();
        Events.post(new EntryAddedEvent(entry, entryTags, foodEatenList));
    } else {
        Events.post(new EntryUpdatedEvent(entry, entryTags, foodEatenList));
    }
    int alarmInMinutes = viewModel.getAlarmInMinutes();
    if (alarmInMinutes > 0) {
        AlarmUtils.setAlarm(alarmInMinutes * DateTimeConstants.MILLIS_PER_MINUTE);
    }
    finish();
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) NestedScrollView(androidx.core.widget.NestedScrollView) ImageView(android.widget.ImageView) View(android.view.View) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) EntryUpdatedEvent(com.faltenreich.diaguard.shared.event.data.EntryUpdatedEvent) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)

Aggregations

Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)32 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)17 ArrayList (java.util.ArrayList)15 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)13 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)10 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)9 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)8 DateTime (org.joda.time.DateTime)8 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)7 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)7 List (java.util.List)7 Nullable (androidx.annotation.Nullable)5 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)5 View (android.view.View)4 ImageView (android.widget.ImageView)4 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)4 EntryDeletedEvent (com.faltenreich.diaguard.shared.event.data.EntryDeletedEvent)4 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)4 Context (android.content.Context)3 LayoutInflater (android.view.LayoutInflater)3