]> git.ktnx.net Git - mobile-ledger.git/blobdiff - 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
index 178db99fb473776bf4ad5cfaf71cf45724b58001..b039b115316e17d842d58318564c238640901e62 100644 (file)
@@ -24,28 +24,33 @@ import android.util.Log;
 
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
+import net.ktnx.mobileledger.model.TransactionListItem;
+import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.MLDB;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
-public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerTransaction>> {
-    protected List<LedgerTransaction> doInBackground(String[] filterAccName) {
+public class UpdateTransactionsTask extends AsyncTask<String, Void, List<TransactionListItem>> {
+    protected List<TransactionListItem> doInBackground(String[] filterAccName) {
         Data.backgroundTaskCount.incrementAndGet();
         String profile_uuid = Data.profile.get().getUuid();
         try {
-            ArrayList<LedgerTransaction> newList = new ArrayList<>();
+            ArrayList<TransactionListItem> newList = new ArrayList<>();
 
             String sql;
             String[] params;
 
             if (filterAccName[0] == null) {
-                sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc";
+                sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
+                      "desc";
                 params = new String[]{profile_uuid};
 
             }
             else {
-                sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
+                sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
+                      "transaction_accounts ta " +
                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
                       "and ta.account_name LIKE ?||'%' AND ta" +
                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
@@ -54,13 +59,27 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerT
 
             Log.d("UTT", sql);
             SQLiteDatabase db = MLDB.getReadableDatabase();
+            Date lastDate = null;
             try (Cursor cursor = db.rawQuery(sql, params)) {
                 while (cursor.moveToNext()) {
                     if (isCancelled()) return null;
 
                     int transaction_id = cursor.getInt(0);
-                    newList.add(new LedgerTransaction(transaction_id));
+                    String dateString = cursor.getString(1);
+                    Date date = Globals.parseLedgerDate(dateString);
+
+                    if ((lastDate == null) || !lastDate.equals(date)) {
+                        boolean showMonth = (lastDate == null) || (date != null) &&
+                                                                  (date.getMonth() !=
+                                                                   lastDate.getMonth() ||
+                                                                   date.getYear() !=
+                                                                   lastDate.getYear());
+                        newList.add(new TransactionListItem(date, showMonth));
+                    }
+                    newList.add(new TransactionListItem(new LedgerTransaction(transaction_id)));
 //                    Log.d("UTT", String.format("got transaction %d", transaction_id));
+
+                    lastDate = date;
                 }
                 Data.transactions.set(newList);
                 Log.d("UTT", "transaction list value updated");