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