2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe 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 * MoLe 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 MoLe. 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.util.Log;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
32 import com.google.android.material.floatingactionbutton.FloatingActionButton;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerAccount;
37 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
38 import net.ktnx.mobileledger.ui.RecyclerItemListener;
39 import net.ktnx.mobileledger.ui.activity.MainActivity;
40 import net.ktnx.mobileledger.utils.Colors;
42 import java.util.List;
43 import java.util.Observer;
45 import androidx.annotation.NonNull;
46 import androidx.annotation.Nullable;
47 import androidx.recyclerview.widget.LinearLayoutManager;
48 import androidx.recyclerview.widget.RecyclerView;
50 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
52 public class AccountSummaryFragment extends MobileLedgerListFragment {
54 MenuItem mShowOnlyStarred;
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 = (o, arg) -> {
75 if (mActivity == null) return;
76 if (swiper == null) return;
77 mActivity.runOnUiThread(() -> {
78 int cnt = Data.backgroundTaskCount.get();
79 Log.d("acc", String.format("background task count changed to %d", cnt));
80 swiper.setRefreshing(cnt > 0);
85 public void onAttach(Context context) {
86 super.onAttach(context);
87 mActivity = (MainActivity) context;
90 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
91 @Nullable Bundle savedInstanceState) {
92 Log.d("flow", "AccountSummaryFragment.onCreateView()");
93 return inflater.inflate(R.layout.account_summary_fragment, container, false);
98 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
99 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
100 super.onActivityCreated(savedInstanceState);
102 modelAdapter = new AccountSummaryAdapter();
104 root = mActivity.findViewById(R.id.account_root);
105 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
106 llm.setOrientation(RecyclerView.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();
147 mActivity.fabShouldShow();
148 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
150 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
152 if (dy < 0) mActivity.fabShouldShow();
153 if (dy > 0) fab.hide();
157 swiper = mActivity.findViewById(R.id.account_swiper);
158 Colors.themeWatch.addObserver(
159 (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
160 swiper.setColorSchemeColors(Colors.primary);
161 swiper.setOnRefreshListener(() -> {
162 Log.d("ui", "refreshing accounts via swipe");
163 mActivity.scheduleTransactionListRetrieval();
166 Data.accounts.addObserver(
167 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
168 Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(
169 AccountSummaryViewModel::scheduleAccountListReload));
170 update_account_table();
172 private void update_account_table() {
173 if (this.getContext() == null) return;
175 AccountSummaryViewModel.scheduleAccountListReload();
177 void stopSelection() {
178 modelAdapter.stopSelection();
179 if (optMenu != null) {
180 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
181 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
182 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
185 if (fab != null) fab.show();
188 public void onCancelAccSelection(MenuItem item) {
191 public void onConfirmAccSelection(MenuItem item) {
192 AccountSummaryViewModel.commitSelections(mActivity);
196 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
197 // Inflate the menu; this adds items to the action bar if it is present.
198 inflater.inflate(R.menu.account_summary, menu);
201 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
202 if (mShowOnlyStarred == null) throw new AssertionError();
203 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
204 if (mCancelSelection == null) throw new AssertionError();
205 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
206 if (mConfirmSelection == null) throw new AssertionError();
208 Data.optShowOnlyStarred.addObserver((o, arg) -> {
209 boolean newValue = Data.optShowOnlyStarred.get();
210 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
211 mShowOnlyStarred.setChecked(newValue);
212 update_account_table();
215 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
217 Log.d("menu", "Accounts: onCreateOptionsMenu called");
219 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
220 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
221 SharedPreferences.Editor editor = pref.edit();
222 boolean flag = item.isChecked();
223 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
225 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
231 mCancelSelection.setOnMenuItemClickListener(item -> {
236 mConfirmSelection.setOnMenuItemClickListener(item -> {
237 AccountSummaryViewModel.commitSelections(mActivity);