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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.account_summary;
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;
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;
48 import java.lang.ref.WeakReference;
49 import java.util.Date;
50 import java.util.List;
52 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
54 public class AccountSummaryFragment extends Fragment {
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;
63 private MainActivity mActivity;
64 private FloatingActionButton fab;
65 private SwipeRefreshLayout swiper;
67 public void onCreate(@Nullable Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 setHasOptionsMenu(true);
71 public void onAttach(Context context) {
72 super.onAttach(context);
73 mActivity = (MainActivity) context;
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);
82 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
83 super.onActivityCreated(savedInstanceState);
85 mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
87 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
88 List<LedgerAccount> accounts = model.getAccounts(this.getContext());
89 modelAdapter = new AccountSummaryAdapter(accounts);
91 RecyclerView root = mActivity.findViewById(R.id.account_root);
92 root.setAdapter(modelAdapter);
94 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
95 llm.setOrientation(LinearLayoutManager.VERTICAL);
96 root.setLayoutManager(llm);
98 fab = mActivity.findViewById(R.id.btn_add_transaction);
100 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
101 new RecyclerItemListener.RecyclerTouchListener() {
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);
109 List<LedgerAccount> accounts = model.getAccounts(mActivity);
110 if (accounts != null) {
111 LedgerAccount account = accounts.get(position);
113 mActivity.showAccountTransactions(account);
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)
125 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
127 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
130 if (fab != null) fab.hide();
136 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
138 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
140 if (dy < 0) fab.show();
141 if (dy > 0) fab.hide();
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);
152 // update_account_table();
153 update_accounts(false);
156 private void prepare_db() {
157 account_list_last_updated = MLDB.get_option_value(MLDB.OPT_LAST_REFRESH, (long) 0);
160 private void update_accounts(boolean force) {
161 long now = new Date().getTime();
162 if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
164 "accounts last updated at " + account_list_last_updated + " and now is " + now +
170 private void update_accounts() {
171 RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(this));
173 task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
177 public void onAccountRefreshDone(int error) {
178 swiper.setRefreshing(false);
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();
185 MLDB.set_option_value(MLDB.OPT_LAST_REFRESH, new Date().getTime());
186 update_account_table();
189 private void update_account_table() {
190 if (this.getContext() == null) return;
192 model.reloadAccounts(this.getContext());
193 modelAdapter.notifyDataSetChanged();
195 public void onRefreshAccountSummaryClicked(MenuItem mi) {
196 update_accounts(true);
199 public void onShowOnlyStarredClicked(MenuItem mi) {
200 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
201 boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
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"));
208 update_account_table();
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);
219 if (fab != null) fab.show();
222 public void onCancelAccSelection(MenuItem item) {
225 public void onConfirmAccSelection(MenuItem item) {
226 model.commitSelections(mActivity);
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);
235 mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
236 if (mShowHiddenAccounts == null) throw new AssertionError();
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);
243 mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
245 Log.d("menu", "MainActivity: onCreateOptionsMenu called");