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 setHasOptionsMenu(true);
70 if (backgroundTaskCountObserver == null) {
71 Log.d("acc", "creating background task count observer");
72 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
73 if (mActivity == null) return;
74 if (swiper == null) return;
75 mActivity.runOnUiThread(() -> {
76 int cnt = Data.backgroundTaskCount.get();
77 Log.d("acc", String.format("background task count changed to %d", cnt));
78 swiper.setRefreshing(cnt > 0);
83 public void onAttach(Context context) {
84 super.onAttach(context);
85 mActivity = (MainActivity) context;
88 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
89 @Nullable Bundle savedInstanceState) {
90 Log.d("flow", "AccountSummaryFragment.onCreateView()");
91 return inflater.inflate(R.layout.account_summary_fragment, container, false);
96 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
97 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
98 super.onActivityCreated(savedInstanceState);
100 modelAdapter = new AccountSummaryAdapter();
102 mActivity.mAccountSummaryFragment = this;
103 root = mActivity.findViewById(R.id.account_root);
104 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
105 llm.setOrientation(RecyclerView.VERTICAL);
106 root.setLayoutManager(llm);
107 root.setAdapter(modelAdapter);
108 DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
109 root.addItemDecoration(did);
111 fab = mActivity.findViewById(R.id.btn_add_transaction);
113 // root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
114 // new RecyclerItemListener.RecyclerTouchListener() {
116 // public void onClickItem(View v, int position) {
117 // Log.d("value", String.format("item %d clicked", position));
118 // if (modelAdapter.isSelectionActive()) {
119 // modelAdapter.selectItem(position);
122 // List<LedgerAccount> accounts = Data.accounts.get();
123 // if (accounts != null) {
124 // LedgerAccount account = accounts.get(position);
126 // mActivity.showAccountTransactions(account);
132 // public void onLongClickItem(View v, int position) {
133 // Log.d("value", String.format("item %d long-clicked", position));
134 // modelAdapter.startSelection();
135 // if (optMenu != null) {
136 // optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
137 // .setVisible(true);
138 // optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
139 // .setVisible(true);
140 // optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
143 // if (fab != null) fab.hide();
148 mActivity.fabShouldShow();
149 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
151 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
153 if (dy < 0) mActivity.fabShouldShow();
154 if (dy > 0) fab.hide();
158 swiper = mActivity.findViewById(R.id.account_swiper);
159 Colors.themeWatch.addObserver((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);