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