use of com.faltenreich.diaguard.shared.data.database.entity.Tag in project Diaguard by Faltenreich.
the class TagAutoCompleteAdapter method getView.
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item_tag_preview, parent, false);
}
TextView characterView = view.findViewById(R.id.tag_character);
TextView nameView = view.findViewById(R.id.tag_name);
Tag tag = getItem(position);
if (tag != null && !TextUtils.isEmpty(tag.getName())) {
characterView.setText(tag.getCharacter());
nameView.setText(tag.getName());
}
return view;
}
use of com.faltenreich.diaguard.shared.data.database.entity.Tag in project Diaguard by Faltenreich.
the class TagEditFragment method store.
private void store() {
String name = getBinding().input.getText().toString();
DataLoader.getInstance().load(getContext(), new DataLoaderListener<TagResult>() {
@Override
public TagResult onShouldLoad(Context context) {
if (StringUtils.isBlank(name)) {
return new TagResult(null, Error.EMPTY);
} else if (TagDao.getInstance().getByName(name) != null) {
return new TagResult(null, Error.DUPLICATE);
} else {
Tag tag = new Tag();
tag.setName(name);
TagDao.getInstance().createOrUpdate(tag);
return new TagResult(tag, null);
}
}
@Override
public void onDidLoad(TagResult result) {
if (result.tag != null) {
Events.post(new TagSavedEvent(result.tag));
dismiss();
} else {
String error = getString(result.error != null ? result.error.textResId : R.string.error_unexpected);
getBinding().input.setError(error);
}
}
});
}
use of com.faltenreich.diaguard.shared.data.database.entity.Tag 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.Tag 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.Tag in project Diaguard by Faltenreich.
the class EntryEditFragment method initLayout.
private void initLayout() {
dateButton.setOnClickListener(view -> showDatePicker());
timeButton.setOnClickListener(view -> showTimePicker());
tagInput.setAdapter(tagAdapter);
tagInput.setOnEditorActionListener((textView, action, keyEvent) -> {
if (action == EditorInfo.IME_ACTION_DONE) {
String name = textView.getText().toString().trim();
if (!StringUtils.isBlank(name)) {
addTag(name);
textView.setText(null);
}
return true;
}
return false;
});
tagInput.setOnFocusChangeListener((view, hasFocus) -> {
// Attempt to fix android.view.WindowManager$BadTokenException
new Handler().post(() -> {
try {
if (hasFocus)
tagInput.showDropDown();
} catch (Exception exception) {
Log.e(TAG, exception.toString());
}
});
});
tagInput.setOnClickListener(view -> tagInput.showDropDown());
tagInput.setOnItemClickListener((adapterView, view, position, l) -> {
tagInput.setText(null);
addTag(tagAdapter.getItem(position));
});
tagListView.setVisibility(View.GONE);
tagEditButton.setOnClickListener(view -> openTagSettings());
EditTextUtils.afterTextChanged(noteInput, () -> viewModel.getEntry().setNote(noteInput.getText().toString()));
alarmContainer.setVisibility(viewModel.isEditing() ? View.GONE : View.VISIBLE);
alarmButton.setOnClickListener(view -> showAlarmPicker());
fabMenu.setOnCategorySelectedListener((category -> addCategory(category, true)));
fabMenu.setOnMiscellaneousSelectedListener(this::openCategoryPicker);
invalidateAlarm();
}
Aggregations