X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fdb%2FTransactionDescriptionAutocompleteAdapter.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fdb%2FTransactionDescriptionAutocompleteAdapter.java;h=f5c10caf421b3abf05e7d966e765f4865462ed18;hb=9ebf60c045fdf01d6f8d1243061e69232c2841ea;hp=0000000000000000000000000000000000000000;hpb=86b5c06af4ba336f0013d01b2cc800e0b902aa0f;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TransactionDescriptionAutocompleteAdapter.java b/app/src/main/java/net/ktnx/mobileledger/db/TransactionDescriptionAutocompleteAdapter.java new file mode 100644 index 00000000..f5c10caf --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/TransactionDescriptionAutocompleteAdapter.java @@ -0,0 +1,76 @@ +/* + * 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.TransactionDAO; +import net.ktnx.mobileledger.utils.Logger; + +import java.util.ArrayList; +import java.util.List; + +public class TransactionDescriptionAutocompleteAdapter extends ArrayAdapter { + private final TransactionFilter filter = new TransactionFilter(); + private final TransactionDAO dao = DB.get() + .getTransactionDAO(); + public TransactionDescriptionAutocompleteAdapter(Context context) { + super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>()); + } + @NonNull + @Override + public Filter getFilter() { + return filter; + } + class TransactionFilter extends Filter { + @Override + protected FilterResults performFiltering(CharSequence constraint) { + FilterResults results = new FilterResults(); + if (constraint == null) { + results.count = 0; + return results; + } + + Logger.debug("acc", String.format("Looking for account '%s'", constraint)); + final List matches = TransactionDAO.unbox(dao.lookupDescriptionSync( + String.valueOf(constraint) + .toUpperCase())); + 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(); + } + } + } +}