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.ui.MobileLedgerListFragment;
37 import net.ktnx.mobileledger.ui.activity.MainActivity;
38 import net.ktnx.mobileledger.utils.Colors;
40 import java.util.Observer;
42 import androidx.annotation.NonNull;
43 import androidx.annotation.Nullable;
44 import androidx.recyclerview.widget.DividerItemDecoration;
45 import androidx.recyclerview.widget.LinearLayoutManager;
46 import androidx.recyclerview.widget.RecyclerView;
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 public AccountSummaryAdapter modelAdapter;
55 private FloatingActionButton fab;
56 private Observer backgroundTaskCountObserver;
58 public void onDestroy() {
59 if (backgroundTaskCountObserver != null) {
60 Log.d("acc", "destroying background task count observer");
61 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
66 public void onCreate(@Nullable Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 Log.d("flow", "AccountSummaryFragment.onCreate()");
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 Log.d("flow", "AccountSummaryFragment.onAttach()");
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 mActivity.mAccountSummaryFragment = this;
105 root = mActivity.findViewById(R.id.account_root);
106 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
107 llm.setOrientation(RecyclerView.VERTICAL);
108 root.setLayoutManager(llm);
109 root.setAdapter(modelAdapter);
110 DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
111 root.addItemDecoration(did);
113 fab = mActivity.findViewById(R.id.btn_add_transaction);
115 // root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
116 // new RecyclerItemListener.RecyclerTouchListener() {
118 // public void onClickItem(View v, int position) {
119 // Log.d("value", String.format("item %d clicked", position));
120 // if (modelAdapter.isSelectionActive()) {
121 // modelAdapter.selectItem(position);
124 // List<LedgerAccount> accounts = Data.accounts.get();
125 // if (accounts != null) {
126 // LedgerAccount account = accounts.get(position);
128 // mActivity.showAccountTransactions(account);
134 // public void onLongClickItem(View v, int position) {
135 // Log.d("value", String.format("item %d long-clicked", position));
136 // modelAdapter.startSelection();
137 // if (optMenu != null) {
138 // optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
139 // .setVisible(true);
140 // optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
141 // .setVisible(true);
142 // optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
145 // if (fab != null) fab.hide();
150 mActivity.fabShouldShow();
151 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
153 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
155 if (dy < 0) mActivity.fabShouldShow();
156 if (dy > 0) fab.hide();
160 swiper = mActivity.findViewById(R.id.account_swiper);
161 Colors.themeWatch.addObserver((o, arg) -> swiper.setColorSchemeColors(Colors.primary));
162 swiper.setColorSchemeColors(Colors.primary);
163 swiper.setOnRefreshListener(() -> {
164 Log.d("ui", "refreshing accounts via swipe");
165 mActivity.scheduleTransactionListRetrieval();
168 Data.accounts.addObserver(
169 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
171 private void update_account_table() {
172 if (this.getContext() == null) return;
174 AccountSummaryViewModel.scheduleAccountListReload();
176 void stopSelection() {
177 modelAdapter.stopSelection();
178 if (optMenu != null) {
179 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
180 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
181 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
184 if (fab != null) fab.show();
187 public void onCancelAccSelection(MenuItem item) {
190 public void onConfirmAccSelection(MenuItem item) {
191 AccountSummaryViewModel.commitSelections(mActivity);
195 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
196 // Inflate the menu; this adds items to the action bar if it is present.
197 inflater.inflate(R.menu.account_summary, menu);
200 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
201 if (mShowOnlyStarred == null) throw new AssertionError();
202 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
203 if (mCancelSelection == null) throw new AssertionError();
204 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
205 if (mConfirmSelection == null) throw new AssertionError();
207 Data.optShowOnlyStarred.addObserver((o, arg) -> {
208 boolean newValue = Data.optShowOnlyStarred.get();
209 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
210 mShowOnlyStarred.setChecked(newValue);
211 update_account_table();
214 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
216 Log.d("menu", "Accounts: onCreateOptionsMenu called");
218 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
219 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
220 SharedPreferences.Editor editor = pref.edit();
221 boolean flag = item.isChecked();
222 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
224 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
230 mCancelSelection.setOnMenuItemClickListener(item -> {
235 mConfirmSelection.setOnMenuItemClickListener(item -> {
236 AccountSummaryViewModel.commitSelections(mActivity);