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