]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
fu: Date → Calendar
[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 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.Globals;
30
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.Calendar;
34 import java.util.Date;
35
36 import static net.ktnx.mobileledger.utils.Logger.debug;
37
38 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
39     protected String doInBackground(String[] filterAccName) {
40         final MobileLedgerProfile profile = Data.profile.getValue();
41         if (profile == null) return "Profile not configured";
42
43         String profile_uuid = profile.getUuid();
44         Data.backgroundTaskStarted();
45         try {
46             ArrayList<TransactionListItem> newList = new ArrayList<>();
47
48             String sql;
49             String[] params;
50
51             if (filterAccName[0] == null) {
52                 sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
53                       "desc";
54                 params = new String[]{profile_uuid};
55
56             }
57             else {
58                 sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
59                       "transaction_accounts ta " +
60                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
61                       "and ta.account_name LIKE ?||'%' AND ta" +
62                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
63                 params = new String[]{profile_uuid, filterAccName[0]};
64             }
65
66             debug("UTT", sql);
67             SQLiteDatabase db = App.getDatabase();
68             String lastDateString = Globals.formatLedgerDate(new Date());
69             Calendar lastDate = Globals.parseLedgerDateAsCalendar(lastDateString);
70             boolean odd = true;
71             try (Cursor cursor = db.rawQuery(sql, params)) {
72                 while (cursor.moveToNext()) {
73                     if (isCancelled())
74                         return null;
75
76                     int transaction_id = cursor.getInt(0);
77                     String dateString = cursor.getString(1);
78                     Calendar date = Globals.parseLedgerDateAsCalendar(dateString);
79
80                     if (!lastDateString.equals(dateString)) {
81                         boolean showMonth =
82                                 (date.get(Calendar.MONTH) != lastDate.get(Calendar.MONTH)) ||
83                                 (date.get(Calendar.YEAR) != lastDate.get(Calendar.YEAR));
84                         newList.add(new TransactionListItem(date.getTime(), 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                     lastDateString = dateString;
92                     odd = !odd;
93                 }
94                 Data.transactions.setList(newList);
95                 debug("UTT", "transaction list value updated");
96             }
97
98             return null;
99         }
100         catch (ParseException e) {
101             return String.format("Error parsing stored date '%s'", e.getMessage());
102         }
103         finally {
104             Data.backgroundTaskFinished();
105         }
106     }
107 }