]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
major refactor, make account summary and transaction list fragments, part of the...
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
1 /*
2  * Copyright © 2018 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.ui.RecyclerItemListener;
43 import net.ktnx.mobileledger.async.RetrieveAccountsTask;
44 import net.ktnx.mobileledger.model.LedgerAccount;
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         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
86         List<LedgerAccount> accounts = model.getAccounts(this.getContext());
87         modelAdapter = new AccountSummaryAdapter(accounts);
88
89         RecyclerView root = mActivity.findViewById(R.id.account_root);
90         root.setAdapter(modelAdapter);
91
92         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
93         llm.setOrientation(LinearLayoutManager.VERTICAL);
94         root.setLayoutManager(llm);
95
96         fab = mActivity.findViewById(R.id.btn_add_transaction);
97
98         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
99                 new RecyclerItemListener.RecyclerTouchListener() {
100                     @Override
101                     public void onClickItem(View v, int position) {
102                         Log.d("list", String.format("item %d clicked", position));
103                         if (modelAdapter.isSelectionActive()) {
104                             modelAdapter.selectItem(position);
105                         }
106                     }
107
108                     @Override
109                     public void onLongClickItem(View v, int position) {
110                         Log.d("list", String.format("item %d long-clicked", position));
111                         modelAdapter.startSelection();
112                         if (optMenu != null) {
113                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
114                                     .setVisible(true);
115                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
116                                     .setVisible(true);
117                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
118                         }
119                         {
120                             if (fab != null) fab.hide();
121                         }
122                     }
123                 }));
124
125         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
126             @Override
127             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
128                 if (fab != null) {
129                     if (dy < 0) fab.show();
130                     if (dy > 0) fab.hide();
131                 }
132             }
133         });
134         swiper = mActivity.findViewById(R.id.account_swiper);
135         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
136         swiper.setOnRefreshListener(() -> {
137             Log.d("ui", "refreshing accounts via swipe");
138             update_accounts(true);
139         });
140         prepare_db();
141 //        update_account_table();
142         update_accounts(false);
143
144     }
145     private void prepare_db() {
146         account_list_last_updated = MLDB.get_option_value(mActivity, "last_refresh", (long) 0);
147     }
148
149     private void update_accounts(boolean force) {
150         long now = new Date().getTime();
151         if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
152             Log.d("db",
153                     "accounts last updated at " + account_list_last_updated + " and now is " + now +
154                     ". re-fetching");
155             update_accounts();
156         }
157     }
158
159     private void update_accounts() {
160         RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(this));
161
162         task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
163         task.execute();
164
165     }
166     public void onAccountRefreshDone(int error) {
167         swiper.setRefreshing(false);
168         if (error != 0) {
169             String err_text = getResources().getString(error);
170             Log.d("visual", String.format("showing snackbar: %s", err_text));
171             Snackbar.make(swiper, err_text, Snackbar.LENGTH_LONG).show();
172         }
173         else {
174             MLDB.set_option_value(mActivity, "last_refresh", new Date().getTime());
175             update_account_table();
176         }
177     }
178     private void update_account_table() {
179         if (this.getContext() == null) return;
180
181         model.reloadAccounts(this.getContext());
182         modelAdapter.notifyDataSetChanged();
183     }
184     public void onRefreshAccountSummaryClicked(MenuItem mi) {
185         update_accounts(true);
186     }
187
188     public void onShowOnlyStarredClicked(MenuItem mi) {
189         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
190         boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
191
192         SharedPreferences.Editor editor = pref.edit();
193         editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
194         Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
195         editor.apply();
196
197         update_account_table();
198     }
199
200     void stopSelection() {
201         modelAdapter.stopSelection();
202         if (optMenu != null) {
203             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
204             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
205             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
206         }
207         {
208             if (fab != null) fab.show();
209         }
210     }
211     public void onCancelAccSelection(MenuItem item) {
212         stopSelection();
213     }
214     public void onConfirmAccSelection(MenuItem item) {
215         model.commitSelections(mActivity);
216         stopSelection();
217     }
218     @Override
219     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
220         // Inflate the menu; this adds items to the action bar if it is present.
221         inflater.inflate(R.menu.account_summary, menu);
222         optMenu = menu;
223
224         mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
225         if (mShowHiddenAccounts == null) throw new AssertionError();
226
227         sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
228                 .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
229         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
230         pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
231
232         mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
233
234         Log.d("menu", "MainActivity: onCreateOptionsMenu called");
235     }
236 }