Search in sources :

Example 1 with Tag

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;
}
Also used : TextView(android.widget.TextView) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) NonNull(androidx.annotation.NonNull)

Example 2 with Tag

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

Example 3 with Tag

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

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

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();
}
Also used : MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) FoodSearchEvent(com.faltenreich.diaguard.shared.event.ui.FoodSearchEvent) NestedScrollView(androidx.core.widget.NestedScrollView) NonNull(androidx.annotation.NonNull) EntryTagDao(com.faltenreich.diaguard.shared.data.database.dao.EntryTagDao) ImageView(android.widget.ImageView) FoodSearchFragment(com.faltenreich.diaguard.feature.food.search.FoodSearchFragment) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Food(com.faltenreich.diaguard.shared.data.database.entity.Food) Handler(android.os.Handler) View(android.view.View) Button(android.widget.Button) Log(android.util.Log) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) CategoryComparatorFactory(com.faltenreich.diaguard.feature.category.CategoryComparatorFactory) BaseFragment(com.faltenreich.diaguard.shared.view.fragment.BaseFragment) EntryDao(com.faltenreich.diaguard.shared.data.database.dao.EntryDao) FragmentEntryEditBinding(com.faltenreich.diaguard.databinding.FragmentEntryEditBinding) TimePickerFragment(com.faltenreich.diaguard.feature.datetime.TimePickerFragment) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) ThreadMode(org.greenrobot.eventbus.ThreadMode) NumberPickerDialog(com.faltenreich.diaguard.shared.view.picker.NumberPickerDialog) ViewGroup(android.view.ViewGroup) AlertDialog(android.app.AlertDialog) R(com.faltenreich.diaguard.R) StringUtils(com.faltenreich.diaguard.shared.data.primitive.StringUtils) List(java.util.List) Nullable(androidx.annotation.Nullable) EditorInfo(android.view.inputmethod.EditorInfo) AlarmUtils(com.faltenreich.diaguard.feature.alarm.AlarmUtils) MainButton(com.faltenreich.diaguard.feature.navigation.MainButton) LocalTime(org.joda.time.LocalTime) ChipGroup(com.google.android.material.chip.ChipGroup) TagDao(com.faltenreich.diaguard.shared.data.database.dao.TagDao) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Helper(com.faltenreich.diaguard.shared.Helper) EntryDeletedEvent(com.faltenreich.diaguard.shared.event.data.EntryDeletedEvent) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) ToolbarDescribing(com.faltenreich.diaguard.feature.navigation.ToolbarDescribing) Events(com.faltenreich.diaguard.shared.event.Events) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) Menu(android.view.Menu) TagAutoCompleteAdapter(com.faltenreich.diaguard.feature.tag.TagAutoCompleteAdapter) DatePickerFragment(com.faltenreich.diaguard.feature.datetime.DatePickerFragment) TagListFragment(com.faltenreich.diaguard.feature.tag.TagListFragment) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ViewUtils(com.faltenreich.diaguard.shared.view.ViewUtils) LayoutInflater(android.view.LayoutInflater) ObjectFactory(com.faltenreich.diaguard.shared.data.reflect.ObjectFactory) DateTime(org.joda.time.DateTime) MeasurementFloatingActionMenu(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementFloatingActionMenu) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) MainButtonProperties(com.faltenreich.diaguard.feature.navigation.MainButtonProperties) AutoCompleteTextView(android.widget.AutoCompleteTextView) Subscribe(org.greenrobot.eventbus.Subscribe) MeasurementDao(com.faltenreich.diaguard.shared.data.database.dao.MeasurementDao) EntryUpdatedEvent(com.faltenreich.diaguard.shared.event.data.EntryUpdatedEvent) ToolbarProperties(com.faltenreich.diaguard.feature.navigation.ToolbarProperties) EditTextUtils(com.faltenreich.diaguard.shared.view.edittext.EditTextUtils) CategoryListFragment(com.faltenreich.diaguard.feature.category.CategoryListFragment) Collections(java.util.Collections) EditText(android.widget.EditText) DateTimeConstants(org.joda.time.DateTimeConstants) Handler(android.os.Handler)

Aggregations

Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)16 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)11 ArrayList (java.util.ArrayList)9 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)7 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)7 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)7 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)6 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)5 NonNull (androidx.annotation.NonNull)4 DateTime (org.joda.time.DateTime)4 Context (android.content.Context)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)3 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)3 Handler (android.os.Handler)2 LayoutInflater (android.view.LayoutInflater)2 AutoCompleteTextView (android.widget.AutoCompleteTextView)2 LinearLayout (android.widget.LinearLayout)2 TextView (android.widget.TextView)2