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.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;
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;
46 import java.lang.ref.WeakReference;
47 import java.util.Date;
48 import java.util.List;
50 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
52 public class AccountSummaryFragment extends MobileLedgerListFragment {
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;
61 private FloatingActionButton fab;
63 public void onCreate(@Nullable Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65 setHasOptionsMenu(true);
67 public void onAttach(Context context) {
68 super.onAttach(context);
69 mActivity = (MainActivity) context;
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);
78 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
79 super.onActivityCreated(savedInstanceState);
81 mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
83 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
84 List<LedgerAccount> accounts = model.getAccounts(this.getContext());
85 modelAdapter = new AccountSummaryAdapter(accounts);
87 RecyclerView root = mActivity.findViewById(R.id.account_root);
88 root.setAdapter(modelAdapter);
90 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
91 llm.setOrientation(LinearLayoutManager.VERTICAL);
92 root.setLayoutManager(llm);
94 fab = mActivity.findViewById(R.id.btn_add_transaction);
96 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
97 new RecyclerItemListener.RecyclerTouchListener() {
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);
105 List<LedgerAccount> accounts = model.getAccounts(mActivity);
106 if (accounts != null) {
107 LedgerAccount account = accounts.get(position);
109 mActivity.showAccountTransactions(account);
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)
121 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
123 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
126 if (fab != null) fab.hide();
132 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
134 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
136 if (dy < 0) fab.show();
137 if (dy > 0) fab.hide();
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);
148 // update_account_table();
149 update_accounts(false);
152 private void prepare_db() {
153 account_list_last_updated = MLDB.get_option_value(MLDB.OPT_LAST_REFRESH, (long) 0);
156 private void update_accounts(boolean force) {
157 long now = new Date().getTime();
158 if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
160 "accounts last updated at " + account_list_last_updated + " and now is " + now +
166 private void update_accounts() {
167 RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(mActivity));
169 task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
173 private void update_account_table() {
174 if (this.getContext() == null) return;
176 model.reloadAccounts(this.getContext());
177 modelAdapter.notifyDataSetChanged();
179 public void onShowOnlyStarredClicked(MenuItem mi) {
180 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
181 boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
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"));
188 update_account_table();
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);
199 if (fab != null) fab.show();
202 public void onCancelAccSelection(MenuItem item) {
205 public void onConfirmAccSelection(MenuItem item) {
206 model.commitSelections(mActivity);
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);
215 mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
216 if (mShowHiddenAccounts == null) throw new AssertionError();
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);
223 mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
225 Log.d("menu", "MainActivity: onCreateOptionsMenu called");