unused variable
[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 = new Observer() {
75                 @Override
76                 public void update(Observable o, Object arg) {
77                     if (mActivity == null) return;
78                     if (swiper == null) return;
79                     mActivity.runOnUiThread(() -> {
80                         int cnt = Data.backgroundTaskCount.get();
81                         Log.d("acc", String.format("background task count changed to %d", cnt));
82                         swiper.setRefreshing(cnt > 0);
83                     });
84                 }
85             });
86         }
87     }
88     public void onAttach(Context context) {
89         super.onAttach(context);
90         mActivity = (MainActivity) context;
91     }
92     @Override
93     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
94                              @Nullable Bundle savedInstanceState) {
95         Log.d("flow", "AccountSummaryFragment.onCreateView()");
96         return inflater.inflate(R.layout.account_summary_fragment, container, false);
97     }
98
99     @Override
100
101     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
102         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
103         super.onActivityCreated(savedInstanceState);
104
105         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
106         modelAdapter = new AccountSummaryAdapter();
107
108         root = mActivity.findViewById(R.id.account_root);
109         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
110         llm.setOrientation(LinearLayoutManager.VERTICAL);
111         root.setLayoutManager(llm);
112         root.setAdapter(modelAdapter);
113
114         fab = mActivity.findViewById(R.id.btn_add_transaction);
115
116         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
117                 new RecyclerItemListener.RecyclerTouchListener() {
118                     @Override
119                     public void onClickItem(View v, int position) {
120                         Log.d("value", String.format("item %d clicked", position));
121                         if (modelAdapter.isSelectionActive()) {
122                             modelAdapter.selectItem(position);
123                         }
124                         else {
125                             List<LedgerAccount> accounts = Data.accounts.get();
126                             if (accounts != null) {
127                                 LedgerAccount account = accounts.get(position);
128
129                                 mActivity.showAccountTransactions(account);
130                             }
131                         }
132                     }
133
134                     @Override
135                     public void onLongClickItem(View v, int position) {
136                         Log.d("value", String.format("item %d long-clicked", position));
137                         modelAdapter.startSelection();
138                         if (optMenu != null) {
139                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
140                                     .setVisible(true);
141                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
142                                     .setVisible(true);
143                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
144                         }
145                         {
146                             if (fab != null) fab.hide();
147                         }
148                     }
149                 }));
150
151         fab.show();
152         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
153             @Override
154             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
155                 if (fab != null) {
156                     if (dy < 0) fab.show();
157                     if (dy > 0) fab.hide();
158                 }
159             }
160         });
161         swiper = mActivity.findViewById(R.id.account_swiper);
162         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
163         swiper.setOnRefreshListener(() -> {
164             Log.d("ui", "refreshing accounts via swipe");
165             mActivity.scheduleTransactionListRetrieval();
166         });
167
168         Data.accounts.addObserver(new Observer() {
169             @Override
170             public void update(Observable o, Object arg) {
171                 mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
172             }
173         });
174         Data.profile.addObserver(new Observer() {
175             @Override
176             public void update(Observable o, Object arg) {
177                 mActivity.runOnUiThread(() -> model.scheduleAccountListReload());
178             }
179         });
180         update_account_table();
181     }
182     private void update_account_table() {
183         if (this.getContext() == null) return;
184
185         model.scheduleAccountListReload();
186     }
187     void stopSelection() {
188         modelAdapter.stopSelection();
189         if (optMenu != null) {
190             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
191             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
192             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
193         }
194         {
195             if (fab != null) fab.show();
196         }
197     }
198     public void onCancelAccSelection(MenuItem item) {
199         stopSelection();
200     }
201     public void onConfirmAccSelection(MenuItem item) {
202         AccountSummaryViewModel.commitSelections(mActivity);
203         stopSelection();
204     }
205     @Override
206     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
207         // Inflate the menu; this adds items to the action bar if it is present.
208         inflater.inflate(R.menu.account_summary, menu);
209         optMenu = menu;
210
211         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
212         if (mShowOnlyStarred == null) throw new AssertionError();
213         MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
214         if (mCancelSelection == null) throw new AssertionError();
215         MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
216         if (mConfirmSelection == null) throw new AssertionError();
217
218         Data.optShowOnlyStarred.addObserver((o, arg) -> {
219             boolean newValue = Data.optShowOnlyStarred.get();
220             Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
221             mShowOnlyStarred.setChecked(newValue);
222             update_account_table();
223         });
224
225         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
226
227         Log.d("menu", "Accounts: onCreateOptionsMenu called");
228
229         mShowOnlyStarred.setOnMenuItemClickListener(item -> {
230             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
231             SharedPreferences.Editor editor = pref.edit();
232             boolean flag = item.isChecked();
233             editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
234             Log.d("pref",
235                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
236             editor.apply();
237
238             return true;
239         });
240
241         mCancelSelection.setOnMenuItemClickListener(item -> {
242             stopSelection();
243             return true;
244         });
245
246         mConfirmSelection.setOnMenuItemClickListener(item -> {
247             AccountSummaryViewModel.commitSelections(mActivity);
248             stopSelection();
249
250             return true;
251         });
252     }
253 }