Search in sources :

Example 1 with EntryTag

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<>();
    }
}
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 2 with EntryTag

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

Example 3 with EntryTag

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();
}
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) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag)

Example 4 with EntryTag

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);
}
Also used : EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView)

Example 5 with EntryTag

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;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) FileWriter(java.io.FileWriter) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) 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) File(java.io.File) Food(com.faltenreich.diaguard.shared.data.database.entity.Food)

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