]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java
streamlined database utility, fed with the application context upon startup
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListViewModel.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
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;
27
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.LedgerTransaction;
30 import net.ktnx.mobileledger.utils.MLDB;
31
32 import java.util.ArrayList;
33
34 public class TransactionListViewModel extends ViewModel {
35
36     private ArrayList<LedgerTransaction> transactions;
37
38     public void reloadTransactions(TransactionListFragment context) {
39         ArrayList<LedgerTransaction> newList = new ArrayList<>();
40
41         Activity act = context.getActivity();
42
43         boolean hasFilter =
44                 act.findViewById(R.id.transaction_list_account_name_filter).getVisibility() ==
45                 View.VISIBLE;
46
47         String sql;
48         String[] params;
49
50         sql = "SELECT id FROM transactions  ORDER BY date desc, id desc";
51         params = null;
52
53         if (hasFilter) {
54             String filterAccName = String.valueOf(
55                     ((AutoCompleteTextView) act.findViewById(R.id.transaction_filter_account_name))
56                             .getText());
57
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};
63             }
64         }
65
66         Log.d("tmp", sql);
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)));
71                 }
72                 transactions = newList;
73                 Log.d("transactions", "transaction list updated");
74             }
75         }
76
77     }
78     public LedgerTransaction getTransaction(int position) {
79         if (position >= transactions.size()) return null;
80         return transactions.get(position);
81     }
82     public int getTransactionCount() {
83         if (transactions == null) return 0;
84         return transactions.size();
85     }
86 }