]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
move initiation of back-end data retrieval to Data
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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  * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
20 import android.content.Context;
21 import android.database.MatrixCursor;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.inputmethod.InputMethodManager;
30 import android.widget.AutoCompleteTextView;
31 import android.widget.Toast;
32
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
36 import net.ktnx.mobileledger.ui.activity.MainActivity;
37 import net.ktnx.mobileledger.utils.Colors;
38 import net.ktnx.mobileledger.utils.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
40
41 import org.jetbrains.annotations.NotNull;
42
43 import androidx.annotation.NonNull;
44 import androidx.annotation.Nullable;
45 import androidx.recyclerview.widget.LinearLayoutManager;
46 import androidx.recyclerview.widget.RecyclerView;
47
48 import static android.content.Context.INPUT_METHOD_SERVICE;
49 import static net.ktnx.mobileledger.utils.Logger.debug;
50
51 public class TransactionListFragment extends MobileLedgerListFragment {
52     private MenuItem menuTransactionListFilter;
53     private View vAccountFilter;
54     private AutoCompleteTextView accNameFilter;
55     @Override
56     public void onCreate(@Nullable Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setHasOptionsMenu(true);
59         Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
60     }
61     @Override
62     public void onAttach(@NotNull Context context) {
63         super.onAttach(context);
64         mActivity = (MainActivity) context;
65     }
66     @Nullable
67     @Override
68     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
69                              @Nullable Bundle savedInstanceState) {
70         return inflater.inflate(R.layout.transaction_list_fragment, container, false);
71     }
72     @Override
73     public void onResume() {
74         super.onResume();
75         debug("flow", "TransactionListFragment.onResume()");
76     }
77     @Override
78     public void onStop() {
79         super.onStop();
80         debug("flow", "TransactionListFragment.onStop()");
81     }
82     @Override
83     public void onPause() {
84         super.onPause();
85         debug("flow", "TransactionListFragment.onPause()");
86     }
87     @Override
88     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
89         debug("flow", "TransactionListFragment.onActivityCreated called");
90         super.onActivityCreated(savedInstanceState);
91
92         swiper = mActivity.findViewById(R.id.transaction_swipe);
93         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
94         root = mActivity.findViewById(R.id.transaction_root);
95         if (root == null)
96             throw new RuntimeException("Can't get hold on the transaction value view");
97         modelAdapter = new TransactionListAdapter();
98         root.setAdapter(modelAdapter);
99
100         mActivity.fabShouldShow();
101
102         manageFabOnScroll();
103
104         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
105
106         llm.setOrientation(RecyclerView.VERTICAL);
107         root.setLayoutManager(llm);
108
109         swiper.setOnRefreshListener(() -> {
110             debug("ui", "refreshing transactions via swipe");
111             Data.scheduleTransactionListRetrieval(mActivity);
112         });
113
114         Colors.themeWatch.observe(this, this::themeChanged);
115
116         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
117         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
118
119         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
120         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
121 //                debug("tmp", "direct onItemClick");
122             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
123             Data.accountFilter.setValue(mc.getString(1));
124             Globals.hideSoftKeyboard(mActivity);
125         });
126
127         Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
128
129         TransactionListViewModel.updating.addObserver(
130                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
131         TransactionListViewModel.updateError.addObserver((o, arg) -> {
132             String err = TransactionListViewModel.updateError.get();
133             if (err == null) return;
134
135             Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
136             TransactionListViewModel.updateError.set(null);
137         });
138         Data.transactions.addObserver(
139                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
140
141         mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
142             Data.accountFilter.setValue(null);
143             vAccountFilter.setVisibility(View.GONE);
144             menuTransactionListFilter.setVisible(true);
145             Globals.hideSoftKeyboard(mActivity);
146         });
147     }
148     private void onAccountNameFilterChanged(String accName) {
149         final String fieldText = accNameFilter.getText().toString();
150         if ((accName == null) && (fieldText.equals(""))) return;
151
152         if (accNameFilter != null) {
153             accNameFilter.setText(accName, false);
154         }
155         final boolean filterActive = (accName != null) && !accName.isEmpty();
156         if (vAccountFilter != null) {
157             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
158         }
159         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
160
161         TransactionListViewModel.scheduleTransactionListReload();
162
163     }
164     @Override
165     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
166         inflater.inflate(R.menu.transaction_list, menu);
167
168         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
169         if ((menuTransactionListFilter == null)) throw new AssertionError();
170
171         if (Data.accountFilter.getValue() != null) {
172             menuTransactionListFilter.setVisible(false);
173         }
174
175         super.onCreateOptionsMenu(menu, inflater);
176
177         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
178             vAccountFilter.setVisibility(View.VISIBLE);
179             if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
180             accNameFilter.requestFocus();
181             InputMethodManager imm =
182                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
183             imm.showSoftInput(accNameFilter, 0);
184
185             return true;
186         });
187     }
188 }