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.content.Context;
21 import android.content.SharedPreferences;
22 import android.os.Bundle;
23 import android.preference.PreferenceManager;
24 import android.support.annotation.NonNull;
25 import android.support.annotation.Nullable;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.v7.widget.LinearLayoutManager;
28 import android.support.v7.widget.RecyclerView;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.model.Data;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
41 import net.ktnx.mobileledger.ui.RecyclerItemListener;
42 import net.ktnx.mobileledger.ui.activity.MainActivity;
44 import java.util.List;
45 import java.util.Observer;
47 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
49 public class AccountSummaryFragment extends MobileLedgerListFragment {
51 MenuItem mShowOnlyStarred;
52 private AccountSummaryAdapter modelAdapter;
54 private FloatingActionButton fab;
55 private Observer backgroundTaskCountObserver;
57 public void onDestroy() {
58 if (backgroundTaskCountObserver != null) {
59 Log.d("acc", "destroying background task count observer");
60 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
65 public void onCreate(@Nullable Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 setHasOptionsMenu(true);
69 if (backgroundTaskCountObserver == null) {
70 Log.d("acc", "creating background task count observer");
71 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
72 if (mActivity == null) return;
73 if (swiper == null) return;
74 mActivity.runOnUiThread(() -> {
75 int cnt = Data.backgroundTaskCount.get();
76 Log.d("acc", String.format("background task count changed to %d", cnt));
77 swiper.setRefreshing(cnt > 0);
82 public void onAttach(Context context) {
83 super.onAttach(context);
84 mActivity = (MainActivity) context;
87 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
88 @Nullable Bundle savedInstanceState) {
89 Log.d("flow", "AccountSummaryFragment.onCreateView()");
90 return inflater.inflate(R.layout.account_summary_fragment, container, false);
95 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
96 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
97 super.onActivityCreated(savedInstanceState);
99 modelAdapter = new AccountSummaryAdapter();
101 root = mActivity.findViewById(R.id.account_root);
102 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
103 llm.setOrientation(LinearLayoutManager.VERTICAL);
104 root.setLayoutManager(llm);
105 root.setAdapter(modelAdapter);
107 fab = mActivity.findViewById(R.id.btn_add_transaction);
109 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
110 new RecyclerItemListener.RecyclerTouchListener() {
112 public void onClickItem(View v, int position) {
113 Log.d("value", String.format("item %d clicked", position));
114 if (modelAdapter.isSelectionActive()) {
115 modelAdapter.selectItem(position);
118 List<LedgerAccount> accounts = Data.accounts.get();
119 if (accounts != null) {
120 LedgerAccount account = accounts.get(position);
122 mActivity.showAccountTransactions(account);
128 public void onLongClickItem(View v, int position) {
129 Log.d("value", String.format("item %d long-clicked", position));
130 modelAdapter.startSelection();
131 if (optMenu != null) {
132 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
134 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
136 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
139 if (fab != null) fab.hide();
144 mActivity.fabShouldShow();
145 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
147 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
149 if (dy < 0) mActivity.fabShouldShow();
150 if (dy > 0) fab.hide();
154 swiper = mActivity.findViewById(R.id.account_swiper);
155 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
156 swiper.setOnRefreshListener(() -> {
157 Log.d("ui", "refreshing accounts via swipe");
158 mActivity.scheduleTransactionListRetrieval();
161 Data.accounts.addObserver(
162 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
163 Data.profile.addObserver(
164 (o, arg) -> mActivity.runOnUiThread(() -> AccountSummaryViewModel.scheduleAccountListReload()));
165 update_account_table();
167 private void update_account_table() {
168 if (this.getContext() == null) return;
170 AccountSummaryViewModel.scheduleAccountListReload();
172 void stopSelection() {
173 modelAdapter.stopSelection();
174 if (optMenu != null) {
175 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
176 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
177 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
180 if (fab != null) fab.show();
183 public void onCancelAccSelection(MenuItem item) {
186 public void onConfirmAccSelection(MenuItem item) {
187 AccountSummaryViewModel.commitSelections(mActivity);
191 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
192 // Inflate the menu; this adds items to the action bar if it is present.
193 inflater.inflate(R.menu.account_summary, menu);
196 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
197 if (mShowOnlyStarred == null) throw new AssertionError();
198 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
199 if (mCancelSelection == null) throw new AssertionError();
200 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
201 if (mConfirmSelection == null) throw new AssertionError();
203 Data.optShowOnlyStarred.addObserver((o, arg) -> {
204 boolean newValue = Data.optShowOnlyStarred.get();
205 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
206 mShowOnlyStarred.setChecked(newValue);
207 update_account_table();
210 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
212 Log.d("menu", "Accounts: onCreateOptionsMenu called");
214 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
215 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
216 SharedPreferences.Editor editor = pref.edit();
217 boolean flag = item.isChecked();
218 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
220 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
226 mCancelSelection.setOnMenuItemClickListener(item -> {
231 mConfirmSelection.setOnMenuItemClickListener(item -> {
232 AccountSummaryViewModel.commitSelections(mActivity);