]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
one more lambda
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.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.account_summary;
19
20 import android.arch.lifecycle.ViewModelProviders;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.os.Bundle;
24 import android.preference.PreferenceManager;
25 import android.support.annotation.NonNull;
26 import android.support.annotation.Nullable;
27 import android.support.design.widget.FloatingActionButton;
28 import android.support.v7.widget.LinearLayoutManager;
29 import android.support.v7.widget.RecyclerView;
30 import android.util.Log;
31 import android.view.LayoutInflater;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
42 import net.ktnx.mobileledger.ui.RecyclerItemListener;
43 import net.ktnx.mobileledger.ui.activity.MainActivity;
44
45 import java.util.List;
46 import java.util.Observable;
47 import java.util.Observer;
48
49 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
50
51 public class AccountSummaryFragment extends MobileLedgerListFragment {
52
53     MenuItem mShowOnlyStarred;
54     private AccountSummaryViewModel model;
55     private AccountSummaryAdapter modelAdapter;
56     private Menu optMenu;
57     private FloatingActionButton fab;
58     private Observer backgroundTaskCountObserver;
59     @Override
60     public void onDestroy() {
61         if (backgroundTaskCountObserver != null) {
62             Log.d("acc", "destroying background task count observer");
63             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
64         }
65         super.onDestroy();
66     }
67     @Override
68     public void onCreate(@Nullable Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setHasOptionsMenu(true);
71
72         if (backgroundTaskCountObserver == null) {
73             Log.d("acc", "creating background task count observer");
74             Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
75                 if (mActivity == null) return;
76                 if (swiper == null) return;
77                 mActivity.runOnUiThread(() -> {
78                     int cnt = Data.backgroundTaskCount.get();
79                     Log.d("acc", String.format("background task count changed to %d", cnt));
80                     swiper.setRefreshing(cnt > 0);
81                 });
82             });
83         }
84     }
85     public void onAttach(Context context) {
86         super.onAttach(context);
87         mActivity = (MainActivity) context;
88     }
89     @Override
90     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
91                              @Nullable Bundle savedInstanceState) {
92         Log.d("flow", "AccountSummaryFragment.onCreateView()");
93         return inflater.inflate(R.layout.account_summary_fragment, container, false);
94     }
95
96     @Override
97
98     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
99         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
100         super.onActivityCreated(savedInstanceState);
101
102         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
103         modelAdapter = new AccountSummaryAdapter();
104
105         root = mActivity.findViewById(R.id.account_root);
106         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
107         llm.setOrientation(LinearLayoutManager.VERTICAL);
108         root.setLayoutManager(llm);
109         root.setAdapter(modelAdapter);
110
111         fab = mActivity.findViewById(R.id.btn_add_transaction);
112
113         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
114                 new RecyclerItemListener.RecyclerTouchListener() {
115                     @Override
116                     public void onClickItem(View v, int position) {
117                         Log.d("value", String.format("item %d clicked", position));
118                         if (modelAdapter.isSelectionActive()) {
119                             modelAdapter.selectItem(position);
120                         }
121                         else {
122                             List<LedgerAccount> accounts = Data.accounts.get();
123                             if (accounts != null) {
124                                 LedgerAccount account = accounts.get(position);
125
126                                 mActivity.showAccountTransactions(account);
127                             }
128                         }
129                     }
130
131                     @Override
132                     public void onLongClickItem(View v, int position) {
133                         Log.d("value", String.format("item %d long-clicked", position));
134                         modelAdapter.startSelection();
135                         if (optMenu != null) {
136                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
137                                     .setVisible(true);
138                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
139                                     .setVisible(true);
140                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
141                         }
142                         {
143                             if (fab != null) fab.hide();
144                         }
145                     }
146                 }));
147
148         fab.show();
149         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
150             @Override
151             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
152                 if (fab != null) {
153                     if (dy < 0) fab.show();
154                     if (dy > 0) fab.hide();
155                 }
156             }
157         });
158         swiper = mActivity.findViewById(R.id.account_swiper);
159         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
160         swiper.setOnRefreshListener(() -> {
161             Log.d("ui", "refreshing accounts via swipe");
162             mActivity.scheduleTransactionListRetrieval();
163         });
164
165         Data.accounts.addObserver(new Observer() {
166             @Override
167             public void update(Observable o, Object arg) {
168                 mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
169             }
170         });
171         Data.profile.addObserver(new Observer() {
172             @Override
173             public void update(Observable o, Object arg) {
174                 mActivity.runOnUiThread(() -> model.scheduleAccountListReload());
175             }
176         });
177         update_account_table();
178     }
179     private void update_account_table() {
180         if (this.getContext() == null) return;
181
182         model.scheduleAccountListReload();
183     }
184     void stopSelection() {
185         modelAdapter.stopSelection();
186         if (optMenu != null) {
187             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
188             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
189             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
190         }
191         {
192             if (fab != null) fab.show();
193         }
194     }
195     public void onCancelAccSelection(MenuItem item) {
196         stopSelection();
197     }
198     public void onConfirmAccSelection(MenuItem item) {
199         AccountSummaryViewModel.commitSelections(mActivity);
200         stopSelection();
201     }
202     @Override
203     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
204         // Inflate the menu; this adds items to the action bar if it is present.
205         inflater.inflate(R.menu.account_summary, menu);
206         optMenu = menu;
207
208         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
209         if (mShowOnlyStarred == null) throw new AssertionError();
210         MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
211         if (mCancelSelection == null) throw new AssertionError();
212         MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
213         if (mConfirmSelection == null) throw new AssertionError();
214
215         Data.optShowOnlyStarred.addObserver((o, arg) -> {
216             boolean newValue = Data.optShowOnlyStarred.get();
217             Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
218             mShowOnlyStarred.setChecked(newValue);
219             update_account_table();
220         });
221
222         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
223
224         Log.d("menu", "Accounts: onCreateOptionsMenu called");
225
226         mShowOnlyStarred.setOnMenuItemClickListener(item -> {
227             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
228             SharedPreferences.Editor editor = pref.edit();
229             boolean flag = item.isChecked();
230             editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
231             Log.d("pref",
232                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
233             editor.apply();
234
235             return true;
236         });
237
238         mCancelSelection.setOnMenuItemClickListener(item -> {
239             stopSelection();
240             return true;
241         });
242
243         mConfirmSelection.setOnMenuItemClickListener(item -> {
244             AccountSummaryViewModel.commitSelections(mActivity);
245             stopSelection();
246
247             return true;
248         });
249     }
250 }