]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
404e5127b4fdaa061399802dcc097d66d8a04e71
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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  * MoLe 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 MoLe. 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
24 import net.ktnx.mobileledger.App;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28 import net.ktnx.mobileledger.model.TransactionListItem;
29 import net.ktnx.mobileledger.utils.SimpleDate;
30
31 import java.util.ArrayList;
32
33 import static net.ktnx.mobileledger.utils.Logger.debug;
34
35 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
36     protected String doInBackground(String[] filterAccName) {
37         final MobileLedgerProfile profile = Data.getProfile();
38
39         String profile_uuid = profile.getUuid();
40         Data.backgroundTaskStarted();
41         try {
42             ArrayList<TransactionListItem> newList = new ArrayList<>();
43
44             String sql;
45             String[] params;
46
47             if (filterAccName[0] == null) {
48                 sql = "SELECT id, year, month, day FROM transactions WHERE profile=? ORDER BY " +
49                       "year desc, month desc, day desc, id desc";
50                 params = new String[]{profile_uuid};
51
52             }
53             else {
54                 sql = "SELECT distinct tr.id, tr.year, tr.month, tr.day from transactions tr " +
55                       "JOIN " + "transaction_accounts ta " +
56                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
57                       "and ta.account_name LIKE ?||'%' AND ta" +
58                       ".amount <> 0 ORDER BY tr.year desc, tr.month desc, tr.day desc, tr.id " +
59                       "desc";
60                 params = new String[]{profile_uuid, filterAccName[0]};
61             }
62
63             debug("UTT", sql);
64             SimpleDate latestDate = null, earliestDate = null;
65             SQLiteDatabase db = App.getDatabase();
66             boolean odd = true;
67             SimpleDate lastDate = SimpleDate.today();
68             try (Cursor cursor = db.rawQuery(sql, params)) {
69                 while (cursor.moveToNext()) {
70                     if (isCancelled())
71                         return null;
72
73                     int transaction_id = cursor.getInt(0);
74                     SimpleDate date =
75                             new SimpleDate(cursor.getInt(1), cursor.getInt(2), cursor.getInt(3));
76
77                     if (null == latestDate)
78                         latestDate = date;
79                     earliestDate = date;
80
81                     if (!date.equals(lastDate)) {
82                         boolean showMonth =
83                                 (date.month != lastDate.month) || (date.year != lastDate.year);
84                         newList.add(new TransactionListItem(date, showMonth));
85                     }
86                     newList.add(
87                             new TransactionListItem(new LedgerTransaction(transaction_id), odd));
88 //                    debug("UTT", String.format("got transaction %d", transaction_id));
89
90                     lastDate = date;
91                     odd = !odd;
92                 }
93                 Data.transactions.setList(newList);
94                 Data.latestTransactionDate.postValue(latestDate);
95                 Data.earliestTransactionDate.postValue(earliestDate);
96                 debug("UTT", "transaction list value updated");
97             }
98
99             return null;
100         }
101         finally {
102             Data.backgroundTaskFinished();
103         }
104     }
105 }