]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
c84da7921b6ab343b2dbaed5033fd897e00d8466
[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.AdapterView;
38 import android.widget.AutoCompleteTextView;
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
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     private String mShowOnlyAccountName;
55     private MenuItem menuTransactionListFilter;
56     private View vAccountFilter;
57     private AutoCompleteTextView accNameFilter;
58     private static void update(Observable o, Object arg) {
59     }
60     public void setShowOnlyAccountName(String mShowOnlyAccountName) {
61         this.mShowOnlyAccountName = mShowOnlyAccountName;
62         if (modelAdapter != null) {
63             modelAdapter.setBoldAccountName(mShowOnlyAccountName);
64         }
65         if (accNameFilter != null) {
66             accNameFilter.setText(mShowOnlyAccountName, false);
67         }
68         if (vAccountFilter != null) {
69             vAccountFilter.setVisibility(
70                     ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
71                     ? View.VISIBLE : View.GONE);
72         }
73     }
74     @Override
75     public void setArguments(@Nullable Bundle args) {
76         super.setArguments(args);
77         mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
78     }
79     @Override
80     public void onCreate(@Nullable Bundle savedInstanceState) {
81         super.onCreate(savedInstanceState);
82         setHasOptionsMenu(true);
83     }
84     @Override
85     public void onAttach(Context context) {
86         super.onAttach(context);
87         mActivity = (MainActivity) context;
88     }
89     @Nullable
90     @Override
91     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
92                              @Nullable Bundle savedInstanceState) {
93         return inflater.inflate(R.layout.transaction_list_fragment, container, false);
94     }
95
96     @Override
97     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
98         Log.d("flow", "TransactionListFragment.onActivityCreated called");
99         super.onActivityCreated(savedInstanceState);
100
101         mActivity.markDrawerItemCurrent(R.id.nav_latest_transactions);
102
103         swiper = mActivity.findViewById(R.id.transaction_swipe);
104         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
105         root = mActivity.findViewById(R.id.transaction_root);
106         if (root == null)
107             throw new RuntimeException("Can't get hold on the transaction value view");
108         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
109         modelAdapter = new TransactionListAdapter();
110
111         modelAdapter.setBoldAccountName(mShowOnlyAccountName);
112
113         FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
114
115         RecyclerView root = mActivity.findViewById(R.id.transaction_root);
116         root.setAdapter(modelAdapter);
117
118         fab.show();
119         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
120             @Override
121             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
122                 if (fab != null) {
123                     if (dy < 0) fab.show();
124                     if (dy > 0) fab.hide();
125                 }
126             }
127         });
128
129         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
130
131         llm.setOrientation(LinearLayoutManager.VERTICAL);
132         root.setLayoutManager(llm);
133
134         swiper.setOnRefreshListener(() -> {
135             Log.d("ui", "refreshing transactions via swipe");
136             mActivity.update_transactions();
137         });
138
139         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
140
141         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
142         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
143
144         TransactionListFragment me = this;
145         MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name");
146         accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
147             @Override
148             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
149                 Log.d("tmp", "direct onItemClick");
150                 ((TransactionListViewModel) model).scheduleTransactionListReload(mActivity);
151                 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
152                 modelAdapter.setBoldAccountName(mc.getString(1));
153                 modelAdapter.notifyDataSetChanged();
154                 Globals.hideSoftKeyboard(mActivity);
155             }
156         });
157
158         if (mShowOnlyAccountName != null) {
159             accNameFilter.setText(mShowOnlyAccountName, false);
160             onShowFilterClick(null);
161             Log.d("flow", String.format("Account filter set to '%s'", mShowOnlyAccountName));
162         }
163
164         TransactionListViewModel.scheduleTransactionListReload(mActivity);
165         TransactionListViewModel.updating.addObserver(new Observer() {
166             @Override
167             public void update(Observable o, Object arg) {
168                 swiper.setRefreshing(TransactionListViewModel.updating.get());
169             }
170         });
171
172         Data.transactions.addObserver(new Observer() {
173             @Override
174             public void update(Observable o, Object arg) {
175                 mActivity.runOnUiThread(new Runnable() {
176                     @Override
177                     public void run() {
178                         modelAdapter.notifyDataSetChanged();
179                     }
180                 });
181             }
182         });
183     }
184     @Override
185     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
186         inflater.inflate(R.menu.transaction_list, menu);
187
188         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
189         if ((menuTransactionListFilter == null)) throw new AssertionError();
190
191         if (mShowOnlyAccountName != null) {
192             menuTransactionListFilter.setVisible(false);
193         }
194
195         super.onCreateOptionsMenu(menu, inflater);
196     }
197
198     public void onClearAccountNameClick(View view) {
199         vAccountFilter.setVisibility(View.GONE);
200         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
201         accNameFilter.setText(null);
202         mShowOnlyAccountName = null;
203         modelAdapter.resetBoldAccountName();
204         TransactionListViewModel.scheduleTransactionListReload(mActivity);
205         Globals.hideSoftKeyboard(mActivity);
206     }
207     public void onShowFilterClick(MenuItem menuItem) {
208         vAccountFilter.setVisibility(View.VISIBLE);
209         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
210         if (menuItem != null) {
211             accNameFilter.requestFocus();
212             InputMethodManager imm =
213                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
214             imm.showSoftInput(accNameFilter, 0);
215         }
216     }
217 }