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 MenuItem mShowOnlyStarred;
54 SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
55 private AccountSummaryViewModel model;
56 private AccountSummaryAdapter modelAdapter;
58 private FloatingActionButton fab;
59 private Observer backgroundTaskCountObserver;
61 public void onDestroy() {
62 if (backgroundTaskCountObserver != null) {
63 Log.d("acc", "destroying background task count observer");
64 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
69 public void onCreate(@Nullable Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 setHasOptionsMenu(true);
73 if (backgroundTaskCountObserver == null) {
74 Log.d("acc", "creating background task count observer");
75 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
77 public void update(Observable o, Object arg) {
78 if (mActivity == null) return;
79 if (swiper == null) return;
80 mActivity.runOnUiThread(() -> {
81 int cnt = Data.backgroundTaskCount.get();
82 Log.d("acc", String.format("background task count changed to %d", cnt));
83 swiper.setRefreshing(cnt > 0);
89 public void onAttach(Context context) {
90 super.onAttach(context);
91 mActivity = (MainActivity) context;
94 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
95 @Nullable Bundle savedInstanceState) {
96 Log.d("flow", "AccountSummaryFragment.onCreateView()");
97 return inflater.inflate(R.layout.account_summary_fragment, container, false);
102 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
103 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
104 super.onActivityCreated(savedInstanceState);
106 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
107 modelAdapter = new AccountSummaryAdapter();
109 root = mActivity.findViewById(R.id.account_root);
110 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
111 llm.setOrientation(LinearLayoutManager.VERTICAL);
112 root.setLayoutManager(llm);
113 root.setAdapter(modelAdapter);
115 fab = mActivity.findViewById(R.id.btn_add_transaction);
117 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
118 new RecyclerItemListener.RecyclerTouchListener() {
120 public void onClickItem(View v, int position) {
121 Log.d("value", String.format("item %d clicked", position));
122 if (modelAdapter.isSelectionActive()) {
123 modelAdapter.selectItem(position);
126 List<LedgerAccount> accounts = Data.accounts.get();
127 if (accounts != null) {
128 LedgerAccount account = accounts.get(position);
130 mActivity.showAccountTransactions(account);
136 public void onLongClickItem(View v, int position) {
137 Log.d("value", String.format("item %d long-clicked", position));
138 modelAdapter.startSelection();
139 if (optMenu != null) {
140 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
142 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
144 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
147 if (fab != null) fab.hide();
153 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
155 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
157 if (dy < 0) fab.show();
158 if (dy > 0) fab.hide();
162 swiper = mActivity.findViewById(R.id.account_swiper);
163 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
164 swiper.setOnRefreshListener(() -> {
165 Log.d("ui", "refreshing accounts via swipe");
166 mActivity.scheduleTransactionListRetrieval();
169 Data.accounts.addObserver(new Observer() {
171 public void update(Observable o, Object arg) {
172 mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
175 Data.profile.addObserver(new Observer() {
177 public void update(Observable o, Object arg) {
178 mActivity.runOnUiThread(() -> model.scheduleAccountListReload(mActivity));
181 update_account_table();
183 private void update_account_table() {
184 if (this.getContext() == null) return;
186 model.scheduleAccountListReload(this.getContext());
188 void stopSelection() {
189 modelAdapter.stopSelection();
190 if (optMenu != null) {
191 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
192 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
193 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
196 if (fab != null) fab.show();
199 public void onCancelAccSelection(MenuItem item) {
202 public void onConfirmAccSelection(MenuItem item) {
203 AccountSummaryViewModel.commitSelections(mActivity);
207 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
208 // Inflate the menu; this adds items to the action bar if it is present.
209 inflater.inflate(R.menu.account_summary, menu);
212 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
213 if (mShowOnlyStarred == null) throw new AssertionError();
215 Data.optShowOnlyStarred.addObserver(new Observer() {
217 public void update(Observable o, Object arg) {
218 boolean newValue = Data.optShowOnlyStarred.get();
219 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
220 mShowOnlyStarred.setChecked(newValue);
221 update_account_table();
225 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
227 Log.d("menu", "Accounts: onCreateOptionsMenu called");
229 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
230 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
231 SharedPreferences.Editor editor = pref.edit();
232 boolean flag = item.isChecked();
233 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
235 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));