remove the day separator at the top of 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.model.MobileLedgerProfile;
28 import net.ktnx.mobileledger.model.TransactionListItem;
29 import net.ktnx.mobileledger.utils.Globals;
30 import net.ktnx.mobileledger.utils.MLDB;
31
32 import java.text.ParseException;
33 import java.util.ArrayList;
34 import java.util.Date;
35
36 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
37     protected String doInBackground(String[] filterAccName) {
38         Data.backgroundTaskCount.incrementAndGet();
39         final MobileLedgerProfile profile = Data.profile.get();
40         if (profile == null) return "Profile not configured";
41
42         String profile_uuid = profile.getUuid();
43         try {
44             ArrayList<TransactionListItem> newList = new ArrayList<>();
45
46             String sql;
47             String[] params;
48
49             if (filterAccName[0] == null) {
50                 sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
51                       "desc";
52                 params = new String[]{profile_uuid};
53
54             }
55             else {
56                 sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
57                       "transaction_accounts ta " +
58                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
59                       "and ta.account_name LIKE ?||'%' AND ta" +
60                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
61                 params = new String[]{profile_uuid, filterAccName[0]};
62             }
63
64             Log.d("UTT", sql);
65             SQLiteDatabase db = MLDB.getReadableDatabase();
66             String lastDateString = Globals.formatLedgerDate(new Date());
67             Date lastDate = Globals.parseLedgerDate(lastDateString);
68             boolean odd = true;
69             try (Cursor cursor = db.rawQuery(sql, params)) {
70                 while (cursor.moveToNext()) {
71                     if (isCancelled()) return null;
72
73                     int transaction_id = cursor.getInt(0);
74                     String dateString = cursor.getString(1);
75                     Date date = Globals.parseLedgerDate(dateString);
76
77                     if (!lastDateString.equals(dateString)) {
78                         boolean showMonth = (date.getMonth() != lastDate.getMonth() ||
79                                              date.getYear() != lastDate.getYear());
80                         newList.add(new TransactionListItem(date, showMonth));
81                     }
82                     newList.add(
83                             new TransactionListItem(new LedgerTransaction(transaction_id), odd));
84 //                    Log.d("UTT", String.format("got transaction %d", transaction_id));
85
86                     lastDate = date;
87                     lastDateString = dateString;
88                     odd = !odd;
89                 }
90                 Data.transactions.set(newList);
91                 Log.d("UTT", "transaction list value updated");
92             }
93
94             return null;
95         }
96         catch (ParseException e) {
97             return String.format("Error parsing stored date '%s'", e.getMessage());
98         }
99         finally {
100             Data.backgroundTaskCount.decrementAndGet();
101         }
102     }
103 }