]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java
f1c6df01348e000947d75fb5f314ea60014de7c4
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListViewModel.java
1 /*
2  * Copyright © 2018 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.ui.transaction_list;
19
20 import android.app.Activity;
21 import android.arch.lifecycle.ViewModel;
22 import android.os.AsyncTask;
23 import android.view.View;
24 import android.widget.AutoCompleteTextView;
25
26 import net.ktnx.mobileledger.R;
27 import net.ktnx.mobileledger.async.UpdateTransactionsTask;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.model.LedgerTransaction;
30 import net.ktnx.mobileledger.model.ObservableValue;
31
32 import java.util.List;
33
34 public class TransactionListViewModel extends ViewModel {
35     public static ObservableValue<Boolean> updating = new ObservableValue<>();
36
37     public static void scheduleTransactionListReload(Activity act) {
38         boolean hasFilter =
39                 act.findViewById(R.id.transaction_list_account_name_filter).getVisibility() ==
40                 View.VISIBLE;
41         String accFilter = hasFilter ? String.valueOf(
42                 ((AutoCompleteTextView) act.findViewById(R.id.transaction_filter_account_name))
43                         .getText()) : null;
44         updating.set(true);
45         AsyncTask<String, Void, List<LedgerTransaction>> task = new UTT();
46         task.execute(accFilter);
47     }
48     public static LedgerTransaction getTransaction(int position) {
49         List<LedgerTransaction> transactions = Data.transactions.get();
50         if (position >= transactions.size()) return null;
51         return transactions.get(position);
52     }
53     public static int getTransactionCount() {
54         List<LedgerTransaction> transactions = Data.transactions.get();
55         if (transactions == null) return 0;
56         return transactions.size();
57     }
58     private static class UTT extends UpdateTransactionsTask {
59         @Override
60         protected void onPostExecute(List<LedgerTransaction> list) {
61             super.onPostExecute(list);
62             updating.set(false);
63             if (list != null) Data.transactions.set(list);
64         }
65         @Override
66         protected void onCancelled(List<LedgerTransaction> ledgerTransactions) {
67             super.onCancelled(ledgerTransactions);
68             updating.set(false);
69         }
70         @Override
71         protected void onCancelled() {
72             super.onCancelled();
73             updating.set(false);
74         }
75     }
76 }