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.model.Data;
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;
45 import java.util.List;
46 import java.util.Observable;
47 import java.util.Observer;
49 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
51 public class AccountSummaryFragment extends MobileLedgerListFragment {
53 private static long account_list_last_updated;
54 private static boolean account_list_needs_update = true;
55 MenuItem mShowHiddenAccounts;
56 SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
57 private AccountSummaryViewModel model;
58 private AccountSummaryAdapter modelAdapter;
60 private FloatingActionButton fab;
61 private Observer backgroundTaskCountObserver;
63 public void onDestroy() {
64 if(backgroundTaskCountObserver!= null) {
65 Log.d("acc", "destroying background task count observer");
66 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
71 public void onCreate(@Nullable Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73 setHasOptionsMenu(true);
75 if (backgroundTaskCountObserver == null) {
76 Log.d("acc", "creating background task count observer");
77 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
79 public void update(Observable o, Object arg) {
80 if (mActivity == null) return;
81 if (swiper == null) return;
82 mActivity.runOnUiThread(new Runnable() {
85 int cnt = Data.backgroundTaskCount.get();
86 Log.d("acc", String.format("background task count changed to %d", cnt));
87 swiper.setRefreshing(cnt > 0);
94 public void onAttach(Context context) {
95 super.onAttach(context);
96 mActivity = (MainActivity) context;
99 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
100 @Nullable Bundle savedInstanceState) {
101 return inflater.inflate(R.layout.account_summary_fragment, container, false);
105 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
106 super.onActivityCreated(savedInstanceState);
108 mActivity.markDrawerItemCurrent(R.id.nav_account_summary);
110 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
111 modelAdapter = new AccountSummaryAdapter();
113 RecyclerView root = mActivity.findViewById(R.id.account_root);
114 root.setAdapter(modelAdapter);
116 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
117 llm.setOrientation(LinearLayoutManager.VERTICAL);
118 root.setLayoutManager(llm);
120 fab = mActivity.findViewById(R.id.btn_add_transaction);
122 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
123 new RecyclerItemListener.RecyclerTouchListener() {
125 public void onClickItem(View v, int position) {
126 Log.d("value", String.format("item %d clicked", position));
127 if (modelAdapter.isSelectionActive()) {
128 modelAdapter.selectItem(position);
131 List<LedgerAccount> accounts = Data.accounts.get();
132 if (accounts != null) {
133 LedgerAccount account = accounts.get(position);
135 mActivity.showAccountTransactions(account);
141 public void onLongClickItem(View v, int position) {
142 Log.d("value", String.format("item %d long-clicked", position));
143 modelAdapter.startSelection();
144 if (optMenu != null) {
145 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
147 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
149 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
152 if (fab != null) fab.hide();
158 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
160 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
162 if (dy < 0) fab.show();
163 if (dy > 0) fab.hide();
167 swiper = mActivity.findViewById(R.id.account_swiper);
168 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
169 swiper.setOnRefreshListener(() -> {
170 Log.d("ui", "refreshing accounts via swipe");
171 mActivity.scheduleTransactionListRetrieval();
174 Data.accounts.addObserver(new Observer() {
176 public void update(Observable o, Object arg) {
177 mActivity.runOnUiThread(new Runnable() {
180 modelAdapter.notifyDataSetChanged();
185 update_account_table();
187 private void update_account_table() {
188 if (this.getContext() == null) return;
190 model.scheduleAccountListReload(this.getContext());
192 public void onShowOnlyStarredClicked(MenuItem mi) {
193 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
194 boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
196 SharedPreferences.Editor editor = pref.edit();
197 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
198 Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
201 update_account_table();
204 void stopSelection() {
205 modelAdapter.stopSelection();
206 if (optMenu != null) {
207 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
208 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
209 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
212 if (fab != null) fab.show();
215 public void onCancelAccSelection(MenuItem item) {
218 public void onConfirmAccSelection(MenuItem item) {
219 AccountSummaryViewModel.commitSelections(mActivity);
223 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
224 // Inflate the menu; this adds items to the action bar if it is present.
225 inflater.inflate(R.menu.account_summary, menu);
228 mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
229 if (mShowHiddenAccounts == null) throw new AssertionError();
231 sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
232 .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
233 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
234 pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
236 mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
238 Log.d("menu", "MainActivity: onCreateOptionsMenu called");