]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
restore account name filter in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.java
1 /*
2  * Copyright © 2019 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.async;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.util.Log;
24
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.utils.MLDB;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerTransaction>> {
33     protected List<LedgerTransaction> doInBackground(String[] filterAccName) {
34         Data.backgroundTaskCount.incrementAndGet();
35         String profile_uuid = Data.profile.get().getUuid();
36         try {
37             ArrayList<LedgerTransaction> newList = new ArrayList<>();
38
39             String sql;
40             String[] params;
41
42             if (filterAccName[0] == null) {
43                 sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc";
44                 params = new String[]{profile_uuid};
45
46             }
47             else {
48                 sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
49                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
50                       "and ta.account_name LIKE ?||'%' AND ta" +
51                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
52                 params = new String[]{profile_uuid, filterAccName[0]};
53             }
54
55             Log.d("UTT", sql);
56             SQLiteDatabase db = MLDB.getReadableDatabase();
57             try (Cursor cursor = db.rawQuery(sql, params)) {
58                 while (cursor.moveToNext()) {
59                     if (isCancelled()) return null;
60
61                     int transaction_id = cursor.getInt(0);
62                     newList.add(new LedgerTransaction(transaction_id));
63 //                    Log.d("UTT", String.format("got transaction %d", transaction_id));
64                 }
65                 Data.transactions.set(newList);
66                 Log.d("UTT", "transaction list value updated");
67             }
68
69             return newList;
70         }
71         finally {
72             Data.backgroundTaskCount.decrementAndGet();
73         }
74     }
75 }