]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
replace dates in transaction list items with delimiters between items in different...
[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.model.TransactionListItem;
28 import net.ktnx.mobileledger.utils.Globals;
29 import net.ktnx.mobileledger.utils.MLDB;
30
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34
35 public class UpdateTransactionsTask extends AsyncTask<String, Void, List<TransactionListItem>> {
36     protected List<TransactionListItem> doInBackground(String[] filterAccName) {
37         Data.backgroundTaskCount.incrementAndGet();
38         String profile_uuid = Data.profile.get().getUuid();
39         try {
40             ArrayList<TransactionListItem> newList = new ArrayList<>();
41
42             String sql;
43             String[] params;
44
45             if (filterAccName[0] == null) {
46                 sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
47                       "desc";
48                 params = new String[]{profile_uuid};
49
50             }
51             else {
52                 sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
53                       "transaction_accounts ta " +
54                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
55                       "and ta.account_name LIKE ?||'%' AND ta" +
56                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
57                 params = new String[]{profile_uuid, filterAccName[0]};
58             }
59
60             Log.d("UTT", sql);
61             SQLiteDatabase db = MLDB.getReadableDatabase();
62             Date lastDate = null;
63             try (Cursor cursor = db.rawQuery(sql, params)) {
64                 while (cursor.moveToNext()) {
65                     if (isCancelled()) return null;
66
67                     int transaction_id = cursor.getInt(0);
68                     String dateString = cursor.getString(1);
69                     Date date = Globals.parseLedgerDate(dateString);
70
71                     if ((lastDate == null) || !lastDate.equals(date)) {
72                         boolean showMonth = (lastDate == null) || (date != null) &&
73                                                                   (date.getMonth() !=
74                                                                    lastDate.getMonth() ||
75                                                                    date.getYear() !=
76                                                                    lastDate.getYear());
77                         newList.add(new TransactionListItem(date, showMonth));
78                     }
79                     newList.add(new TransactionListItem(new LedgerTransaction(transaction_id)));
80 //                    Log.d("UTT", String.format("got transaction %d", transaction_id));
81
82                     lastDate = date;
83                 }
84                 Data.transactions.set(newList);
85                 Log.d("UTT", "transaction list value updated");
86             }
87
88             return newList;
89         }
90         finally {
91             Data.backgroundTaskCount.decrementAndGet();
92         }
93     }
94 }