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