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.Data;
41 import net.ktnx.mobileledger.model.LedgerAccount;
42 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
43 import net.ktnx.mobileledger.ui.RecyclerItemListener;
44 import net.ktnx.mobileledger.ui.activity.MainActivity;
46 import java.lang.ref.WeakReference;
47 import java.util.Date;
48 import java.util.List;
49 import java.util.Observable;
50 import java.util.Observer;
52 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
54 public class AccountSummaryFragment extends MobileLedgerListFragment {
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 FloatingActionButton fab;
64 private Observer backgroundTaskCountObserver;
66 public void onDestroy() {
67 if(backgroundTaskCountObserver!= null) {
68 Log.d("acc", "destroying background task count observer");
69 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
74 public void onCreate(@Nullable Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
76 setHasOptionsMenu(true);
78 if (backgroundTaskCountObserver == null) {
79 Log.d("acc", "creating background task count observer");
80 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
82 public void update(Observable o, Object arg) {
83 if (mActivity == null) return;
84 if (swiper == null) return;
85 mActivity.runOnUiThread(new Runnable() {
88 int cnt = Data.backgroundTaskCount.get();
89 Log.d("acc", String.format("background task count changed to %d", cnt));
90 swiper.setRefreshing(cnt > 0);
97 public void onAttach(Context context) {
98 super.onAttach(context);
99 mActivity = (MainActivity) context;
102 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
103 @Nullable Bundle savedInstanceState) {
104 return inflater.inflate(R.layout.account_summary_fragment, container, false);
108 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
109 super.onActivityCreated(savedInstanceState);
111 mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
113 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
114 modelAdapter = new AccountSummaryAdapter();
116 RecyclerView root = mActivity.findViewById(R.id.account_root);
117 root.setAdapter(modelAdapter);
119 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
120 llm.setOrientation(LinearLayoutManager.VERTICAL);
121 root.setLayoutManager(llm);
123 fab = mActivity.findViewById(R.id.btn_add_transaction);
125 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
126 new RecyclerItemListener.RecyclerTouchListener() {
128 public void onClickItem(View v, int position) {
129 Log.d("value", String.format("item %d clicked", position));
130 if (modelAdapter.isSelectionActive()) {
131 modelAdapter.selectItem(position);
134 List<LedgerAccount> accounts = Data.accounts.get();
135 if (accounts != null) {
136 LedgerAccount account = accounts.get(position);
138 mActivity.showAccountTransactions(account);
144 public void onLongClickItem(View v, int position) {
145 Log.d("value", String.format("item %d long-clicked", position));
146 modelAdapter.startSelection();
147 if (optMenu != null) {
148 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
150 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
152 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
155 if (fab != null) fab.hide();
161 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
163 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
165 if (dy < 0) fab.show();
166 if (dy > 0) fab.hide();
170 swiper = mActivity.findViewById(R.id.account_swiper);
171 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
172 swiper.setOnRefreshListener(() -> {
173 Log.d("ui", "refreshing accounts via swipe");
174 update_accounts(true);
177 Data.accounts.addObserver(new Observer() {
179 public void update(Observable o, Object arg) {
180 mActivity.runOnUiThread(new Runnable() {
183 modelAdapter.notifyDataSetChanged();
188 update_account_table();
191 private void update_accounts(boolean force) {
192 long now = new Date().getTime();
193 if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
195 "accounts last updated at " + account_list_last_updated + " and now is " + now +
201 private void update_accounts() {
202 RetrieveAccountsTask task = new RetrieveAccountsTask(new WeakReference<>(mActivity));
204 task.setPref(PreferenceManager.getDefaultSharedPreferences(mActivity));
208 private void update_account_table() {
209 if (this.getContext() == null) return;
211 model.scheduleAccountListReload(this.getContext());
213 public void onShowOnlyStarredClicked(MenuItem mi) {
214 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
215 boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
217 SharedPreferences.Editor editor = pref.edit();
218 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
219 Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
222 update_account_table();
225 void stopSelection() {
226 modelAdapter.stopSelection();
227 if (optMenu != null) {
228 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
229 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
230 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
233 if (fab != null) fab.show();
236 public void onCancelAccSelection(MenuItem item) {
239 public void onConfirmAccSelection(MenuItem item) {
240 AccountSummaryViewModel.commitSelections(mActivity);
244 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
245 // Inflate the menu; this adds items to the action bar if it is present.
246 inflater.inflate(R.menu.account_summary, menu);
249 mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
250 if (mShowHiddenAccounts == null) throw new AssertionError();
252 sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
253 .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
254 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
255 pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
257 mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
259 Log.d("menu", "MainActivity: onCreateOptionsMenu called");