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