use of com.faltenreich.diaguard.shared.data.database.entity.EntryTag 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.EntryTag 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();
}
use of com.faltenreich.diaguard.shared.data.database.entity.EntryTag in project Diaguard by Faltenreich.
the class EntryEditFragment method setEntry.
private void setEntry(@NonNull Entry entry) {
noteInput.setText(entry.getNote());
// FIXME: Previous order gets ignored on Fragment recreation
List<Measurement> measurements = entry.getMeasurementCache();
if (measurements != null && !measurements.isEmpty()) {
Collections.sort(measurements, CategoryComparatorFactory.getInstance().createComparatorFromMeasurements());
for (Measurement measurement : measurements) {
addMeasurement(measurement, false);
}
} else if (!entry.isPersisted() && !isRecreated) {
for (Category category : viewModel.getPinnedCategories()) {
if (!hasCategory(category)) {
addCategory(category, false);
}
}
}
if (viewModel.getEntryTags() != null) {
for (EntryTag entryTag : viewModel.getEntryTags()) {
Tag tag = entryTag.getTag();
if (tag != null) {
addTag(entryTag.getTag());
}
}
}
invalidateDateTime();
fabMenu.restock();
}
use of com.faltenreich.diaguard.shared.data.database.entity.EntryTag in project Diaguard by Faltenreich.
the class EntryEditFragment method addTag.
private void addTag(Tag tag) {
int index = viewModel.getIndexOfTag(tag);
if (index == -1) {
EntryTag entryTag = new EntryTag();
entryTag.setEntry(viewModel.getEntry());
entryTag.setTag(tag);
viewModel.getEntryTags().add(entryTag);
}
ChipView chip = new ChipView(getContext());
chip.setTag(tag);
chip.setText(tag.getName());
chip.setCloseIconVisible(true);
chip.setOnCloseIconClickListener(view -> removeTag(tag, chip));
chip.setOnClickListener(view -> removeTag(tag, chip));
tagListView.addView(chip);
tagAdapter.set(tag, false);
dismissTagDropDown();
tagListView.setVisibility(View.VISIBLE);
}
use of com.faltenreich.diaguard.shared.data.database.entity.EntryTag in project Diaguard by Faltenreich.
the class CsvExport method doInBackground.
@Override
protected File doInBackground(Void... params) {
DateTime dateStart = config.getDateStart();
DateTime dateEnd = config.getDateEnd();
Category[] categories = config.getCategories();
boolean isBackup = config.isBackup();
File file = isBackup ? Export.getBackupFile(config, FileType.CSV) : Export.getExportFile(config);
try {
FileWriter fileWriter = new FileWriter(file);
CSVWriter writer = new CSVWriter(fileWriter, CsvMeta.CSV_DELIMITER);
if (isBackup) {
// Meta information to detect the data scheme in future iterations
String[] meta = new String[] { CsvMeta.CSV_KEY_META, Integer.toString(DatabaseHelper.getVersion()) };
writer.writeNext(meta);
List<Tag> tags = TagDao.getInstance().getAll();
for (Tag tag : tags) {
writer.writeNext(ArrayUtils.add(tag.getValuesForBackup(), 0, tag.getKeyForBackup()));
}
List<Food> foods = FoodDao.getInstance().getAllFromUser();
for (Food food : foods) {
writer.writeNext(ArrayUtils.add(food.getValuesForBackup(), 0, food.getKeyForBackup()));
}
}
List<Entry> entries = dateStart != null && dateEnd != null ? EntryDao.getInstance().getEntriesBetween(dateStart, dateEnd) : EntryDao.getInstance().getAll();
int position = 0;
for (Entry entry : entries) {
publishProgress(String.format(Locale.getDefault(), "%s %d/%d", config.getContext().getString(R.string.entry), position, entries.size()));
writer.writeNext(isBackup ? ArrayUtils.add(entry.getValuesForBackup(), 0, entry.getKeyForBackup()) : entry.getValuesForExport());
List<Measurement> measurements = categories != null ? EntryDao.getInstance().getMeasurements(entry, categories) : EntryDao.getInstance().getMeasurements(entry);
for (Measurement measurement : measurements) {
writer.writeNext(isBackup ? ArrayUtils.add(measurement.getValuesForBackup(), 0, measurement.getKeyForBackup()) : measurement.getValuesForExport());
if (isBackup && measurement instanceof Meal) {
Meal meal = (Meal) measurement;
for (FoodEaten foodEaten : meal.getFoodEaten()) {
if (foodEaten.getMeal() != null && foodEaten.getFood() != null) {
writer.writeNext(ArrayUtils.add(foodEaten.getValuesForBackup(), 0, foodEaten.getKeyForBackup()));
}
}
}
}
if (isBackup) {
List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
for (EntryTag entryTag : entryTags) {
if (entryTag.getEntry() != null && entryTag.getTag() != null) {
writer.writeNext(ArrayUtils.add(entryTag.getValuesForBackup(), 0, entryTag.getKeyForBackup()));
}
}
}
position++;
}
writer.close();
} catch (IOException exception) {
Log.e(TAG, exception.toString());
}
return file;
}
Aggregations