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