]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java
async account list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateAccountsTask.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.LedgerAccount;
27 import net.ktnx.mobileledger.utils.MLDB;
28
29 import java.util.ArrayList;
30
31 public class UpdateAccountsTask extends AsyncTask<Boolean, Void, ArrayList<LedgerAccount>> {
32     protected ArrayList<LedgerAccount> doInBackground(Boolean[] onlyStarred) {
33         Data.backgroundTaskCount.incrementAndGet();
34         try {
35             ArrayList<LedgerAccount> newList = new ArrayList<>();
36
37             String sql = "SELECT name, hidden FROM accounts";
38             if (onlyStarred[0]) sql += " WHERE hidden = 0";
39             sql += " ORDER BY name";
40
41             SQLiteDatabase db = MLDB.getReadableDatabase();
42             try (Cursor cursor = db.rawQuery(sql, null)) {
43                 while (cursor.moveToNext()) {
44                     LedgerAccount acc = new LedgerAccount(cursor.getString(0));
45                     acc.setHidden(cursor.getInt(1) == 1);
46                     try (Cursor c2 = db.rawQuery(
47                             "SELECT value, currency FROM account_values " + "WHERE account = ?",
48                             new String[]{acc.getName()}))
49                     {
50                         while (c2.moveToNext()) {
51                             acc.addAmount(c2.getFloat(0), c2.getString(1));
52                         }
53                     }
54                     newList.add(acc);
55                 }
56             }
57
58             Data.accounts.set(newList);
59
60             return newList;
61         }
62         finally {
63             Log.d("UAT", "decrementing background task count");
64             Data.backgroundTaskCount.decrementAndGet();
65         }
66     }
67 }