drop redundant setting of swiper color
[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.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.inputmethod.InputMethodManager;
30 import android.widget.AutoCompleteTextView;
31 import android.widget.Toast;
32
33 import com.google.android.material.floatingactionbutton.FloatingActionButton;
34
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
38 import net.ktnx.mobileledger.ui.activity.MainActivity;
39 import net.ktnx.mobileledger.utils.Colors;
40 import net.ktnx.mobileledger.utils.Globals;
41 import net.ktnx.mobileledger.utils.MLDB;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import androidx.annotation.NonNull;
46 import androidx.annotation.Nullable;
47 import androidx.recyclerview.widget.LinearLayoutManager;
48 import androidx.recyclerview.widget.RecyclerView;
49
50 import static android.content.Context.INPUT_METHOD_SERVICE;
51 import static net.ktnx.mobileledger.utils.Logger.debug;
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         debug("flow", "TransactionListFragment.onResume()");
78     }
79     @Override
80     public void onStop() {
81         super.onStop();
82         debug("flow", "TransactionListFragment.onStop()");
83     }
84     @Override
85     public void onPause() {
86         super.onPause();
87         debug("flow", "TransactionListFragment.onPause()");
88     }
89     @Override
90     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
91         debug("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             debug("ui", "refreshing transactions via swipe");
120             mActivity.scheduleTransactionListRetrieval();
121         });
122
123         Colors.themeWatch.observe(this, this::themeChanged);
124
125         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
126         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
127
128         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
129         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
130 //                debug("tmp", "direct onItemClick");
131             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
132             Data.accountFilter.setValue(mc.getString(1));
133             Globals.hideSoftKeyboard(mActivity);
134         });
135
136         Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
137
138         TransactionListViewModel.updating.addObserver(
139                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
140         TransactionListViewModel.updateError.addObserver((o, arg) -> {
141             String err = TransactionListViewModel.updateError.get();
142             if (err == null) return;
143
144             Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
145             TransactionListViewModel.updateError.set(null);
146         });
147         Data.transactions.addObserver(
148                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
149
150         mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
151             Data.accountFilter.setValue(null);
152             vAccountFilter.setVisibility(View.GONE);
153             menuTransactionListFilter.setVisible(true);
154             Globals.hideSoftKeyboard(mActivity);
155         });
156     }
157     private void onAccountNameFilterChanged(String accName) {
158         final String fieldText = accNameFilter.getText().toString();
159         if ((accName == null) && (fieldText.equals(""))) return;
160
161         if (accNameFilter != null) {
162             accNameFilter.setText(accName, false);
163         }
164         final boolean filterActive = (accName != null) && !accName.isEmpty();
165         if (vAccountFilter != null) {
166             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
167         }
168         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
169
170         TransactionListViewModel.scheduleTransactionListReload();
171
172     }
173     @Override
174     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
175         inflater.inflate(R.menu.transaction_list, menu);
176
177         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
178         if ((menuTransactionListFilter == null)) throw new AssertionError();
179
180         if (Data.accountFilter.getValue() != null) {
181             menuTransactionListFilter.setVisible(false);
182         }
183
184         super.onCreateOptionsMenu(menu, inflater);
185
186         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
187             vAccountFilter.setVisibility(View.VISIBLE);
188             if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
189             accNameFilter.requestFocus();
190             InputMethodManager imm =
191                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
192             imm.showSoftInput(accNameFilter, 0);
193
194             return true;
195         });
196     }
197 }