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.Observer;
48 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
50 public class AccountSummaryFragment extends MobileLedgerListFragment {
52 MenuItem mShowOnlyStarred;
53 private AccountSummaryViewModel model;
54 private AccountSummaryAdapter modelAdapter;
56 private FloatingActionButton fab;
57 private Observer backgroundTaskCountObserver;
59 public void onDestroy() {
60 if (backgroundTaskCountObserver != null) {
61 Log.d("acc", "destroying background task count observer");
62 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
67 public void onCreate(@Nullable Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 setHasOptionsMenu(true);
71 if (backgroundTaskCountObserver == null) {
72 Log.d("acc", "creating background task count observer");
73 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
74 if (mActivity == null) return;
75 if (swiper == null) return;
76 mActivity.runOnUiThread(() -> {
77 int cnt = Data.backgroundTaskCount.get();
78 Log.d("acc", String.format("background task count changed to %d", cnt));
79 swiper.setRefreshing(cnt > 0);
84 public void onAttach(Context context) {
85 super.onAttach(context);
86 mActivity = (MainActivity) context;
89 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
90 @Nullable Bundle savedInstanceState) {
91 Log.d("flow", "AccountSummaryFragment.onCreateView()");
92 return inflater.inflate(R.layout.account_summary_fragment, container, false);
97 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
98 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
99 super.onActivityCreated(savedInstanceState);
101 model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
102 modelAdapter = new AccountSummaryAdapter();
104 root = mActivity.findViewById(R.id.account_root);
105 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
106 llm.setOrientation(LinearLayoutManager.VERTICAL);
107 root.setLayoutManager(llm);
108 root.setAdapter(modelAdapter);
110 fab = mActivity.findViewById(R.id.btn_add_transaction);
112 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
113 new RecyclerItemListener.RecyclerTouchListener() {
115 public void onClickItem(View v, int position) {
116 Log.d("value", String.format("item %d clicked", position));
117 if (modelAdapter.isSelectionActive()) {
118 modelAdapter.selectItem(position);
121 List<LedgerAccount> accounts = Data.accounts.get();
122 if (accounts != null) {
123 LedgerAccount account = accounts.get(position);
125 mActivity.showAccountTransactions(account);
131 public void onLongClickItem(View v, int position) {
132 Log.d("value", String.format("item %d long-clicked", position));
133 modelAdapter.startSelection();
134 if (optMenu != null) {
135 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
137 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
139 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
142 if (fab != null) fab.hide();
148 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
150 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
152 if (dy < 0) fab.show();
153 if (dy > 0) fab.hide();
157 swiper = mActivity.findViewById(R.id.account_swiper);
158 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
159 swiper.setOnRefreshListener(() -> {
160 Log.d("ui", "refreshing accounts via swipe");
161 mActivity.scheduleTransactionListRetrieval();
164 Data.accounts.addObserver(
165 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
166 Data.profile.addObserver(
167 (o, arg) -> mActivity.runOnUiThread(() -> model.scheduleAccountListReload()));
168 update_account_table();
170 private void update_account_table() {
171 if (this.getContext() == null) return;
173 model.scheduleAccountListReload();
175 void stopSelection() {
176 modelAdapter.stopSelection();
177 if (optMenu != null) {
178 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
179 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
180 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
183 if (fab != null) fab.show();
186 public void onCancelAccSelection(MenuItem item) {
189 public void onConfirmAccSelection(MenuItem item) {
190 AccountSummaryViewModel.commitSelections(mActivity);
194 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
195 // Inflate the menu; this adds items to the action bar if it is present.
196 inflater.inflate(R.menu.account_summary, menu);
199 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
200 if (mShowOnlyStarred == null) throw new AssertionError();
201 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
202 if (mCancelSelection == null) throw new AssertionError();
203 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
204 if (mConfirmSelection == null) throw new AssertionError();
206 Data.optShowOnlyStarred.addObserver((o, arg) -> {
207 boolean newValue = Data.optShowOnlyStarred.get();
208 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
209 mShowOnlyStarred.setChecked(newValue);
210 update_account_table();
213 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
215 Log.d("menu", "Accounts: onCreateOptionsMenu called");
217 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
218 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
219 SharedPreferences.Editor editor = pref.edit();
220 boolean flag = item.isChecked();
221 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
223 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
229 mCancelSelection.setOnMenuItemClickListener(item -> {
234 mConfirmSelection.setOnMenuItemClickListener(item -> {
235 AccountSummaryViewModel.commitSelections(mActivity);