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