" ELSE 9 END AS ordering " + "FROM accounts " +
"WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
"ORDER BY ordering, name_upper, rowid ")
- public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
- @NonNull
- String term);
+ public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
+ @NonNull
+ String term);
@Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
" WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
" ELSE 9 END AS ordering " + "FROM accounts " +
"WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
"ORDER BY ordering, name_upper, rowid ")
- public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
- @NonNull String term);
+ public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
+ @NonNull String term);
+
+ @Query("SELECT * FROM accounts " +
+ "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
+ "ORDER BY 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, name_upper, rowid ")
+ public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
+ @NonNull
+ String term);
@Query("SELECT DISTINCT name, 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 ")
- public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
+ public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@NonNull String term);
@Query("SELECT DISTINCT name, 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 ")
- public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
+ public abstract List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
@Query("SELECT * FROM accounts WHERE profile_id = :profileId")
public abstract List<Account> allForProfileSync(long profileId);
Logger.debug("acc", String.format("Looking for account '%s'", constraint));
final List<String> matches = AccountDAO.unbox(
- (profileId == NO_PROFILE_ID) ? dao.lookupByNameSync(String.valueOf(constraint)
- .toUpperCase())
- : dao.lookupInProfileByNameSync(profileId,
- String.valueOf(constraint)
- .toUpperCase()));
+ (profileId == NO_PROFILE_ID) ? dao.lookupNamesByNameSync(
+ String.valueOf(constraint)
+ .toUpperCase()) : dao.lookupNamesInProfileByNameSync(profileId,
+ String.valueOf(constraint)
+ .toUpperCase()));
results.values = matches;
results.count = matches.size();
package net.ktnx.mobileledger.db;
+import androidx.annotation.NonNull;
import androidx.room.Embedded;
import androidx.room.Relation;
public Account account;
@Relation(parentColumn = "id", entityColumn = "account_id")
public List<AccountValue> amounts;
+ @NonNull
+ @Override
+ public String toString() {
+ return account.getName();
+ }
}
--- /dev/null
+/*
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.db;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Filter;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
+
+import java.util.List;
+
+public class AccountWithAmountsAutocompleteAdapter extends ArrayAdapter<AccountWithAmounts> {
+ private final AccountFilter filter = new AccountFilter();
+ private final long profileId;
+ public AccountWithAmountsAutocompleteAdapter(Context context, @NonNull Profile profile) {
+ super(context, R.layout.account_autocomplete_row);
+ profileId = profile.getId();
+ }
+ @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(R.layout.account_autocomplete_row, parent, false);
+ }
+ AccountWithAmounts item = getItem(position);
+ ((TextView) view.findViewById(R.id.account_name)).setText(item.account.getName());
+ StringBuilder amountsText = new StringBuilder();
+ for (AccountValue amt : item.amounts) {
+ if (amountsText.length() != 0)
+ amountsText.append('\n');
+ String currency = amt.getCurrency();
+ if (Misc.emptyIsNull(currency) != null)
+ amountsText.append(currency)
+ .append(' ');
+ amountsText.append(Data.formatNumber(amt.getValue()));
+ }
+ ((TextView) view.findViewById(R.id.amounts)).setText(amountsText.toString());
+
+ return view;
+ }
+ class AccountFilter extends Filter {
+ private final AccountDAO dao = DB.get()
+ .getAccountDAO();
+ @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<AccountWithAmounts> matches =
+ dao.lookupWithAmountsInProfileByNameSync(profileId, 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<AccountWithAmounts>) results.values);
+ notifyDataSetChanged();
+ }
+ }
+ }
+}
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.databinding.NewTransactionAccountRowBinding;
-import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
+import net.ktnx.mobileledger.db.AccountWithAmountsAutocompleteAdapter;
import net.ktnx.mobileledger.model.Currency;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
.getContext();
- b.accountRowAccName.setAdapter(new AccountAutocompleteAdapter(b.getRoot()
- .getContext(), mProfile));
+ b.accountRowAccName.setAdapter(new AccountWithAmountsAutocompleteAdapter(b.getRoot()
+ .getContext(),
+ mProfile));
decimalSeparator = "";
Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf(
presentAccountName, incomingAccountName,
acc.getAccountNameCursorPosition()));
// avoid triggering completion pop-up
- AccountAutocompleteAdapter a =
- (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
+ AccountWithAmountsAutocompleteAdapter a =
+ (AccountWithAmountsAutocompleteAdapter) b.accountRowAccName.getAdapter();
try {
b.accountRowAccName.setAdapter(null);
b.accountRowAccName.setText(incomingAccountName);
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?><!--
+ ~ 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 <https://www.gnu.org/licenses/>.
+ -->
+
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:minHeight="@dimen/thumb_row_height"
+ android:padding="@dimen/half_text_margin"
+ >
+
+ <TextView
+ android:id="@+id/account_name"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:text="TextView\non two lines"
+ android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent"
+ />
+ <TextView
+ android:id="@+id/amounts"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="@dimen/text_margin"
+ android:gravity="end"
+ android:text="TextView"
+ android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@id/account_name"
+ app:layout_goneMarginStart="0dp"
+ />
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file