2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
20 import android.app.Activity;
21 import android.arch.lifecycle.ViewModel;
22 import android.database.Cursor;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.util.Log;
25 import android.view.View;
26 import android.widget.AutoCompleteTextView;
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.LedgerTransaction;
30 import net.ktnx.mobileledger.utils.MLDB;
32 import java.util.ArrayList;
34 public class TransactionListViewModel extends ViewModel {
36 private ArrayList<LedgerTransaction> transactions;
38 public void reloadTransactions(TransactionListFragment context) {
39 ArrayList<LedgerTransaction> newList = new ArrayList<>();
41 Activity act = context.getActivity();
44 act.findViewById(R.id.transaction_list_account_name_filter).getVisibility() ==
50 sql = "SELECT id FROM transactions ORDER BY date desc, id desc";
54 String filterAccName = String.valueOf(
55 ((AutoCompleteTextView) act.findViewById(R.id.transaction_filter_account_name))
58 if (!filterAccName.isEmpty()) {
59 sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
60 "ON ta.transaction_id=tr.id WHERE ta.account_name LIKE ?||'%' AND ta" +
61 ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
62 params = new String[]{filterAccName};
67 try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
68 try (Cursor cursor = db.rawQuery(sql, params)) {
69 while (cursor.moveToNext()) {
70 newList.add(new LedgerTransaction(cursor.getInt(0)));
72 transactions = newList;
73 Log.d("transactions", "transaction list updated");
78 public LedgerTransaction getTransaction(int position) {
79 if (position >= transactions.size()) return null;
80 return transactions.get(position);
82 public int getTransactionCount() {
83 if (transactions == null) return 0;
84 return transactions.size();