X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Ftransaction_list%2FTransactionListViewModel.java;h=e88481cfba902de387b9f005b25b8b31b708fcf0;hp=d0c979e6df9b06ea6325fa80c3e906f5f3a78e62;hb=0fc2ddc465cd9b9314ae336e69535020a96a7fbc;hpb=324f40ffba80292007999d9812929acc6f6a36f8 diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index d0c979e6..e88481cf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -1,70 +1,56 @@ /* - * Copyright © 2018 Damyan Ivanov. - * This file is part of Mobile-Ledger. - * Mobile-Ledger is free software: you can distribute it and/or modify it + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your opinion), any later version. * - * Mobile-Ledger is distributed in the hope that it will be useful, + * MoLe is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License terms for details. * * You should have received a copy of the GNU General Public License - * along with Mobile-Ledger. If not, see . + * along with MoLe. If not, see . */ package net.ktnx.mobileledger.ui.transaction_list; -import android.arch.lifecycle.ViewModel; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; -import net.ktnx.mobileledger.model.LedgerTransaction; -import net.ktnx.mobileledger.model.LedgerTransactionItem; -import net.ktnx.mobileledger.utils.MobileLedgerDatabase; +import net.ktnx.mobileledger.async.UpdateTransactionsTask; +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.model.TransactionListItem; +import net.ktnx.mobileledger.utils.LockHolder; +import net.ktnx.mobileledger.utils.ObservableValue; -import java.util.ArrayList; -import java.util.List; +import androidx.lifecycle.ViewModel; public class TransactionListViewModel extends ViewModel { + public static ObservableValue updating = new ObservableValue<>(); + public static ObservableValue updateError = new ObservableValue<>(); - private List transactions; + public static void scheduleTransactionListReload() { + if (Data.profile.getValue() == null) return; - public List getTransactions(MobileLedgerDatabase dbh) { - if (transactions == null) { - transactions = new ArrayList<>(); - reloadTransactions(dbh); + String filter = Data.accountFilter.getValue(); + AsyncTask task = new UTT(); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filter); + } + public static TransactionListItem getTransactionListItem(int position) { + try(LockHolder lh = Data.transactions.lockForReading()) { + if (Data.transactions == null) return null; + if (position >= Data.transactions.size() + 1) return null; + if (position == Data.transactions.size()) return new TransactionListItem(); + return Data.transactions.get(position); } - - return transactions; } - private void reloadTransactions(MobileLedgerDatabase dbh) { - transactions.clear(); - String sql = "SELECT id, date, description FROM transactions"; - sql += " ORDER BY date desc, id desc"; - - try (SQLiteDatabase db = dbh.getReadableDatabase()) { - try (Cursor cursor = db.rawQuery(sql, null)) { - while (cursor.moveToNext()) { - LedgerTransaction tr = - new LedgerTransaction(cursor.getString(0), cursor.getString(1), - cursor.getString(2)); - try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " + - "transaction_accounts WHERE transaction_id = ?", - new String[]{tr.getId()})) - { - while (cAcc.moveToNext()) { - tr.add_item( - new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1), - cAcc.getString(2))); - } - } - transactions.add(tr); - } - } + private static class UTT extends UpdateTransactionsTask { + @Override + protected void onPostExecute(String error) { + super.onPostExecute(error); + if (error != null) updateError.set(error); } - } }