From: Damyan Ivanov Date: Sat, 13 Feb 2021 08:10:08 +0000 (+0200) Subject: implement account name lookup in Templates' account fields X-Git-Tag: v0.17.0~143 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=d6040b0ed8cc133b60ad25aa2f38e2cb9a6ad166;p=mobile-ledger.git implement account name lookup in Templates' account fields --- diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java index 80bab0cc..3bdf431d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java @@ -23,6 +23,7 @@ import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; +import androidx.room.RoomWarnings; import androidx.room.Update; import net.ktnx.mobileledger.db.Account; @@ -51,4 +52,40 @@ public abstract class AccountDAO extends BaseDAO { // @Transaction // @Query("SELECT * FROM patterns") // List getPatternsWithAccounts(); + + @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + + " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " + + " ELSE 9 END AS ordering " + "FROM accounts " + + "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " + + "ORDER BY ordering, name_upper, rowid ") + @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) + public abstract LiveData> lookupInProfileByName(@NonNull String profileUUID, + @NonNull String term); + + @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + + " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " + + " ELSE 9 END AS ordering " + "FROM accounts " + + "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " + + "ORDER BY ordering, name_upper, rowid ") + @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) + public abstract List lookupInProfileByNameSync(@NonNull String profileUUID, + @NonNull String term); + + @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + + " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " + + " ELSE 9 END AS ordering " + "FROM accounts " + + "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ") + @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) + public abstract LiveData> lookupByName(@NonNull String term); + + @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + + " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " + + " ELSE 9 END AS ordering " + "FROM accounts " + + "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ") + @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) + public abstract List lookupByNameSync(@NonNull String term); } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java b/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java new file mode 100644 index 00000000..9eabeb91 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java @@ -0,0 +1,92 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.db; + +import android.content.Context; +import android.widget.ArrayAdapter; +import android.widget.Filter; + +import androidx.annotation.NonNull; + +import net.ktnx.mobileledger.dao.AccountDAO; + +import java.util.ArrayList; +import java.util.List; + +public class AccountAutocompleteAdapter extends ArrayAdapter { + private final AccountFilter filter = new AccountFilter(); + private final AccountDAO dao = DB.get() + .getAccountDAO(); + private String profileUUID; + public AccountAutocompleteAdapter(Context context) { + super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>()); + } + public void setProfileUUID(String profileUUID) { + this.profileUUID = profileUUID; + } + @NonNull + @Override + public Filter getFilter() { + return filter; + } + // @NonNull +// @Override +// public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { +// View view = convertView; +// if (view == null) { +// view = LayoutInflater.from(parent.getContext()) +// .inflate(android.R.layout.simple_dropdown_item_1line, parent, +// false); +// } +// Account item = getItem(position); +// ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName()); +// return view; +// } + class AccountFilter extends Filter { + @Override + protected FilterResults performFiltering(CharSequence constraint) { + FilterResults results = new FilterResults(); + if (constraint == null) { + results.count = 0; + return results; + } + + final List matches = + (profileUUID == null) ? dao.lookupByNameSync(String.valueOf(constraint)) + : dao.lookupInProfileByNameSync(profileUUID, + String.valueOf(constraint)); + results.values = matches; + results.count = matches.size(); + + return results; + } + @Override + @SuppressWarnings("unchecked") + protected void publishResults(CharSequence constraint, FilterResults results) { + if (results.values == null) { + notifyDataSetInvalidated(); + } + else { + setNotifyOnChange(false); + clear(); + addAll((List) results.values); + notifyDataSetChanged(); + } + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java index 4f70f846..46bdba55 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java @@ -22,6 +22,7 @@ import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -32,6 +33,7 @@ import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.databinding.TemplateDetailsAccountBinding; import net.ktnx.mobileledger.databinding.TemplateDetailsHeaderBinding; +import net.ktnx.mobileledger.db.AccountAutocompleteAdapter; import net.ktnx.mobileledger.db.TemplateBase; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.TemplateDetailsItem; @@ -448,6 +450,11 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter b.templateDetailsAccountName.setText( + ((TextView) view).getText())); TextWatcher accountCommentWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} diff --git a/app/src/main/res/layout/template_details_account.xml b/app/src/main/res/layout/template_details_account.xml index 93372b7a..2bed3700 100644 --- a/app/src/main/res/layout/template_details_account.xml +++ b/app/src/main/res/layout/template_details_account.xml @@ -61,8 +61,10 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/template_details_account_name_source" + app:endIconMode="clear_text" > -