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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.util.Log;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.utils.MLDB;
29 import java.util.ArrayList;
30 import java.util.List;
32 public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerTransaction>> {
33 protected List<LedgerTransaction> doInBackground(String[] filterAccName) {
34 Data.backgroundTaskCount.incrementAndGet();
36 ArrayList<LedgerTransaction> newList = new ArrayList<>();
38 boolean hasFilter = (filterAccName != null) && (filterAccName.length > 0) &&
39 (filterAccName[0] != null) && !filterAccName[0].isEmpty();
44 sql = "SELECT id FROM transactions ORDER BY date desc, id desc";
48 sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
49 "ON ta.transaction_id=tr.id WHERE ta.account_name LIKE ?||'%' AND ta" +
50 ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
51 params = filterAccName;
55 SQLiteDatabase db = MLDB.getReadableDatabase();
56 try (Cursor cursor = db.rawQuery(sql, params)) {
57 while (cursor.moveToNext()) {
58 if (isCancelled()) return null;
60 newList.add(new LedgerTransaction(cursor.getInt(0)));
62 Data.transactions.set(newList);
63 Log.d("transactions", "transaction value updated");
69 Data.backgroundTaskCount.decrementAndGet();