]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
toCamelCase
[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
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 onActivityCreated(@Nullable Bundle savedInstanceState) {
120         Log.d("flow", "TransactionListFragment.onActivityCreated called");
121         super.onActivityCreated(savedInstanceState);
122
123         swiper = mActivity.findViewById(R.id.transaction_swipe);
124         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
125         root = mActivity.findViewById(R.id.transaction_root);
126         if (root == null)
127             throw new RuntimeException("Can't get hold on the transaction value view");
128         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
129         modelAdapter = new TransactionListAdapter();
130
131         modelAdapter.setBoldAccountName(mShowOnlyAccountName);
132         root.setAdapter(modelAdapter);
133
134         FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
135
136         fab.show();
137         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
138             @Override
139             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
140                 if (dy < 0) fab.show();
141                 if (dy > 0) fab.hide();
142             }
143         });
144
145         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
146
147         llm.setOrientation(LinearLayoutManager.VERTICAL);
148         root.setLayoutManager(llm);
149
150         swiper.setOnRefreshListener(() -> {
151             Log.d("ui", "refreshing transactions via swipe");
152             mActivity.scheduleTransactionListRetrieval();
153         });
154
155         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
156
157         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
158         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
159
160         TransactionListFragment me = this;
161         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
162         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
163 //                Log.d("tmp", "direct onItemClick");
164             TransactionListViewModel.scheduleTransactionListReload();
165             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
166             accountFilter.set(mc.getString(1));
167             Globals.hideSoftKeyboard(mActivity);
168         });
169
170         accountFilter.addObserver((o, arg) -> {
171             String accountName = accountFilter.get();
172             modelAdapter.setBoldAccountName(accountName);
173             setShowOnlyAccountName(accountName);
174             TransactionListViewModel.scheduleTransactionListReload();
175             if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
176         });
177
178         Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(() -> {
179             Log.d("transactions", "requesting list reload");
180             TransactionListViewModel.scheduleTransactionListReload();
181         }));
182
183         TransactionListViewModel.scheduleTransactionListReload();
184         TransactionListViewModel.updating.addObserver(
185                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
186
187         Data.transactions.addObserver(
188                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
189
190         mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
191             vAccountFilter.setVisibility(View.GONE);
192             if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
193             accountFilter.set(null);
194             accNameFilter.setText(null);
195             TransactionListViewModel.scheduleTransactionListReload();
196             Globals.hideSoftKeyboard(mActivity);
197         });
198     }
199     @Override
200     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
201         inflater.inflate(R.menu.transaction_list, menu);
202
203         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
204         if ((menuTransactionListFilter == null)) throw new AssertionError();
205
206         if (mShowOnlyAccountName != null) {
207             menuTransactionListFilter.setVisible(false);
208         }
209
210         super.onCreateOptionsMenu(menu, inflater);
211
212         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
213             vAccountFilter.setVisibility(View.VISIBLE);
214             if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
215             accNameFilter.requestFocus();
216             InputMethodManager imm =
217                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
218             imm.showSoftInput(accNameFilter, 0);
219
220             return true;
221         });
222     }
223 }