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