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