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