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.
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.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;
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 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
86 List<LedgerAccount> accounts = model.getAccounts(this.getContext());
87 modelAdapter = new AccountSummaryAdapter(accounts);
89 RecyclerView root = mActivity.findViewById(R.id.account_root);
90 root.setAdapter(modelAdapter);
92 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
93 llm.setOrientation(LinearLayoutManager.VERTICAL);
94 root.setLayoutManager(llm);
96 fab = mActivity.findViewById(R.id.btn_add_transaction);
98 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
99 new RecyclerItemListener.RecyclerTouchListener() {
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);
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)
115 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
117 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
120 if (fab != null) fab.hide();
125 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
127 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
129 if (dy < 0) fab.show();
130 if (dy > 0) fab.hide();
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);
141 // update_account_table();
142 update_accounts(false);
145 private void prepare_db() {
146 account_list_last_updated = MLDB.get_option_value(mActivity, "last_refresh", (long) 0);
149 private void update_accounts(boolean force) {
150 long now = new Date().getTime();
151 if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
153 "accounts last updated at " + account_list_last_updated + " and now is " + now +
159 private void update_accounts() {
160 RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(this));
162 task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
166 public void onAccountRefreshDone(int error) {
167 swiper.setRefreshing(false);
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();
174 MLDB.set_option_value(mActivity, "last_refresh", new Date().getTime());
175 update_account_table();
178 private void update_account_table() {
179 if (this.getContext() == null) return;
181 model.reloadAccounts(this.getContext());
182 modelAdapter.notifyDataSetChanged();
184 public void onRefreshAccountSummaryClicked(MenuItem mi) {
185 update_accounts(true);
188 public void onShowOnlyStarredClicked(MenuItem mi) {
189 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
190 boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
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"));
197 update_account_table();
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);
208 if (fab != null) fab.show();
211 public void onCancelAccSelection(MenuItem item) {
214 public void onConfirmAccSelection(MenuItem item) {
215 model.commitSelections(mActivity);
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);
224 mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
225 if (mShowHiddenAccounts == null) throw new AssertionError();
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);
232 mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
234 Log.d("menu", "MainActivity: onCreateOptionsMenu called");