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