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 private AccountSummaryViewModel model;
55 private AccountSummaryAdapter modelAdapter;
57 private FloatingActionButton fab;
58 private Observer backgroundTaskCountObserver;
60 public void onDestroy() {
61 if (backgroundTaskCountObserver != null) {
62 Log.d("acc", "destroying background task count observer");
63 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
68 public void onCreate(@Nullable Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setHasOptionsMenu(true);
72 if (backgroundTaskCountObserver == null) {
73 Log.d("acc", "creating background task count observer");
74 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
76 public void update(Observable o, Object arg) {
77 if (mActivity == null) return;
78 if (swiper == null) return;
79 mActivity.runOnUiThread(() -> {
80 int cnt = Data.backgroundTaskCount.get();
81 Log.d("acc", String.format("background task count changed to %d", cnt));
82 swiper.setRefreshing(cnt > 0);
88 public void onAttach(Context context) {
89 super.onAttach(context);
90 mActivity = (MainActivity) context;
93 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
94 @Nullable Bundle savedInstanceState) {
95 Log.d("flow", "AccountSummaryFragment.onCreateView()");
96 return inflater.inflate(R.layout.account_summary_fragment, container, false);
101 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
102 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
103 super.onActivityCreated(savedInstanceState);
105 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
106 modelAdapter = new AccountSummaryAdapter();
108 root = mActivity.findViewById(R.id.account_root);
109 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
110 llm.setOrientation(LinearLayoutManager.VERTICAL);
111 root.setLayoutManager(llm);
112 root.setAdapter(modelAdapter);
114 fab = mActivity.findViewById(R.id.btn_add_transaction);
116 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
117 new RecyclerItemListener.RecyclerTouchListener() {
119 public void onClickItem(View v, int position) {
120 Log.d("value", String.format("item %d clicked", position));
121 if (modelAdapter.isSelectionActive()) {
122 modelAdapter.selectItem(position);
125 List<LedgerAccount> accounts = Data.accounts.get();
126 if (accounts != null) {
127 LedgerAccount account = accounts.get(position);
129 mActivity.showAccountTransactions(account);
135 public void onLongClickItem(View v, int position) {
136 Log.d("value", String.format("item %d long-clicked", position));
137 modelAdapter.startSelection();
138 if (optMenu != null) {
139 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
141 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
143 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
146 if (fab != null) fab.hide();
152 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
154 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
156 if (dy < 0) fab.show();
157 if (dy > 0) fab.hide();
161 swiper = mActivity.findViewById(R.id.account_swiper);
162 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
163 swiper.setOnRefreshListener(() -> {
164 Log.d("ui", "refreshing accounts via swipe");
165 mActivity.scheduleTransactionListRetrieval();
168 Data.accounts.addObserver(new Observer() {
170 public void update(Observable o, Object arg) {
171 mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged());
174 Data.profile.addObserver(new Observer() {
176 public void update(Observable o, Object arg) {
177 mActivity.runOnUiThread(() -> model.scheduleAccountListReload());
180 update_account_table();
182 private void update_account_table() {
183 if (this.getContext() == null) return;
185 model.scheduleAccountListReload();
187 void stopSelection() {
188 modelAdapter.stopSelection();
189 if (optMenu != null) {
190 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
191 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
192 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
195 if (fab != null) fab.show();
198 public void onCancelAccSelection(MenuItem item) {
201 public void onConfirmAccSelection(MenuItem item) {
202 AccountSummaryViewModel.commitSelections(mActivity);
206 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
207 // Inflate the menu; this adds items to the action bar if it is present.
208 inflater.inflate(R.menu.account_summary, menu);
211 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
212 if (mShowOnlyStarred == null) throw new AssertionError();
213 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
214 if (mCancelSelection == null) throw new AssertionError();
215 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
216 if (mConfirmSelection == null) throw new AssertionError();
218 Data.optShowOnlyStarred.addObserver((o, arg) -> {
219 boolean newValue = Data.optShowOnlyStarred.get();
220 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
221 mShowOnlyStarred.setChecked(newValue);
222 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"));
241 mCancelSelection.setOnMenuItemClickListener(item -> {
246 mConfirmSelection.setOnMenuItemClickListener(item -> {
247 AccountSummaryViewModel.commitSelections(mActivity);