]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
Show only starred menu now works
[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     SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
55     private AccountSummaryViewModel model;
56     private AccountSummaryAdapter modelAdapter;
57     private Menu optMenu;
58     private FloatingActionButton fab;
59     private Observer backgroundTaskCountObserver;
60     @Override
61     public void onDestroy() {
62         if (backgroundTaskCountObserver != null) {
63             Log.d("acc", "destroying background task count observer");
64             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
65         }
66         super.onDestroy();
67     }
68     @Override
69     public void onCreate(@Nullable Bundle savedInstanceState) {
70         super.onCreate(savedInstanceState);
71         setHasOptionsMenu(true);
72
73         if (backgroundTaskCountObserver == null) {
74             Log.d("acc", "creating background task count observer");
75             Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
76                 @Override
77                 public void update(Observable o, Object arg) {
78                     if (mActivity == null) return;
79                     if (swiper == null) return;
80                     mActivity.runOnUiThread(() -> {
81                         int cnt = Data.backgroundTaskCount.get();
82                         Log.d("acc", String.format("background task count changed to %d", cnt));
83                         swiper.setRefreshing(cnt > 0);
84                     });
85                 }
86             });
87         }
88     }
89     public void onAttach(Context context) {
90         super.onAttach(context);
91         mActivity = (MainActivity) context;
92     }
93     @Override
94     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
95                              @Nullable Bundle savedInstanceState) {
96         Log.d("flow", "AccountSummaryFragment.onCreateView()");
97         return inflater.inflate(R.layout.account_summary_fragment, container, false);
98     }
99
100     @Override
101
102     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
103         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
104         super.onActivityCreated(savedInstanceState);
105
106         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
107         modelAdapter = new AccountSummaryAdapter();
108
109         root = mActivity.findViewById(R.id.account_root);
110         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
111         llm.setOrientation(LinearLayoutManager.VERTICAL);
112         root.setLayoutManager(llm);
113         root.setAdapter(modelAdapter);
114
115         fab = mActivity.findViewById(R.id.btn_add_transaction);
116
117         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
118                 new RecyclerItemListener.RecyclerTouchListener() {
119                     @Override
120                     public void onClickItem(View v, int position) {
121                         Log.d("value", String.format("item %d clicked", position));
122                         if (modelAdapter.isSelectionActive()) {
123                             modelAdapter.selectItem(position);
124                         }
125                         else {
126                             List<LedgerAccount> accounts = Data.accounts.get();
127                             if (accounts != null) {
128                                 LedgerAccount account = accounts.get(position);
129
130                                 mActivity.showAccountTransactions(account);
131                             }
132                         }
133                     }
134
135                     @Override
136                     public void onLongClickItem(View v, int position) {
137                         Log.d("value", String.format("item %d long-clicked", position));
138                         modelAdapter.startSelection();
139                         if (optMenu != null) {
140                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
141                                     .setVisible(true);
142                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
143                                     .setVisible(true);
144                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
145                         }
146                         {
147                             if (fab != null) fab.hide();
148                         }
149                     }
150                 }));
151
152         fab.show();
153         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
154             @Override
155             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
156                 if (fab != null) {
157                     if (dy < 0) fab.show();
158                     if (dy > 0) fab.hide();
159                 }
160             }
161         });
162         swiper = mActivity.findViewById(R.id.account_swiper);
163         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
164         swiper.setOnRefreshListener(() -> {
165             Log.d("ui", "refreshing accounts via swipe");
166             mActivity.scheduleTransactionListRetrieval();
167         });
168
169         Data.accounts.addObserver(new Observer() {
170             @Override
171             public void update(Observable o, Object arg) {
172                 mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
173             }
174         });
175         Data.profile.addObserver(new Observer() {
176             @Override
177             public void update(Observable o, Object arg) {
178                 mActivity.runOnUiThread(() -> model.scheduleAccountListReload(mActivity));
179             }
180         });
181         update_account_table();
182     }
183     private void update_account_table() {
184         if (this.getContext() == null) return;
185
186         model.scheduleAccountListReload(this.getContext());
187     }
188     void stopSelection() {
189         modelAdapter.stopSelection();
190         if (optMenu != null) {
191             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
192             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
193             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
194         }
195         {
196             if (fab != null) fab.show();
197         }
198     }
199     public void onCancelAccSelection(MenuItem item) {
200         stopSelection();
201     }
202     public void onConfirmAccSelection(MenuItem item) {
203         AccountSummaryViewModel.commitSelections(mActivity);
204         stopSelection();
205     }
206     @Override
207     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
208         // Inflate the menu; this adds items to the action bar if it is present.
209         inflater.inflate(R.menu.account_summary, menu);
210         optMenu = menu;
211
212         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
213         if (mShowOnlyStarred == null) throw new AssertionError();
214
215         Data.optShowOnlyStarred.addObserver(new Observer() {
216             @Override
217             public void update(Observable o, Object arg) {
218                 boolean newValue = Data.optShowOnlyStarred.get();
219                 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
220                 mShowOnlyStarred.setChecked(newValue);
221                 update_account_table();
222             }
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
239             return true;
240         });
241     }
242 }