Search in sources :

Example 1 with TagSavedEvent

use of com.faltenreich.diaguard.shared.event.data.TagSavedEvent 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);
            }
        }
    });
}
Also used : Context(android.content.Context) TagSavedEvent(com.faltenreich.diaguard.shared.event.data.TagSavedEvent) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag)

Aggregations

Context (android.content.Context)1 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)1 TagSavedEvent (com.faltenreich.diaguard.shared.event.data.TagSavedEvent)1