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