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();
}
}
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<>();
}
}
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<>();
}
}
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);
}
}
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();
}
Aggregations