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.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
31 import com.google.android.material.floatingactionbutton.FloatingActionButton;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
36 import net.ktnx.mobileledger.ui.activity.MainActivity;
37 import net.ktnx.mobileledger.utils.Colors;
39 import org.jetbrains.annotations.NotNull;
41 import androidx.annotation.NonNull;
42 import androidx.annotation.Nullable;
43 import androidx.recyclerview.widget.DividerItemDecoration;
44 import androidx.recyclerview.widget.LinearLayoutManager;
45 import androidx.recyclerview.widget.RecyclerView;
47 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
48 import static net.ktnx.mobileledger.utils.Logger.debug;
50 public class AccountSummaryFragment extends MobileLedgerListFragment {
52 private MenuItem mShowOnlyStarred;
53 public AccountSummaryAdapter modelAdapter;
55 private FloatingActionButton fab;
57 public void onCreate(@Nullable Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59 debug("flow", "AccountSummaryFragment.onCreate()");
60 setHasOptionsMenu(true);
62 Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
64 public void onAttach(@NotNull Context context) {
65 super.onAttach(context);
66 debug("flow", "AccountSummaryFragment.onAttach()");
67 mActivity = (MainActivity) context;
70 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
71 @Nullable Bundle savedInstanceState) {
72 debug("flow", "AccountSummaryFragment.onCreateView()");
73 return inflater.inflate(R.layout.account_summary_fragment, container, false);
78 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
79 debug("flow", "AccountSummaryFragment.onActivityCreated()");
80 super.onActivityCreated(savedInstanceState);
82 modelAdapter = new AccountSummaryAdapter();
84 mActivity.mAccountSummaryFragment = this;
85 root = mActivity.findViewById(R.id.account_root);
86 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
87 llm.setOrientation(RecyclerView.VERTICAL);
88 root.setLayoutManager(llm);
89 root.setAdapter(modelAdapter);
90 DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
91 root.addItemDecoration(did);
93 fab = mActivity.findViewById(R.id.btn_add_transaction);
95 // root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
96 // new RecyclerItemListener.RecyclerTouchListener() {
98 // public void onClickItem(View v, int position) {
99 // debug("value", String.format("item %d clicked", position));
100 // if (modelAdapter.isSelectionActive()) {
101 // modelAdapter.selectItem(position);
104 // List<LedgerAccount> accounts = Data.accounts.get();
105 // if (accounts != null) {
106 // LedgerAccount account = accounts.get(position);
108 // mActivity.showAccountTransactions(account);
114 // public void onLongClickItem(View v, int position) {
115 // debug("value", String.format("item %d long-clicked", position));
116 // modelAdapter.startSelection();
117 // if (optMenu != null) {
118 // optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
119 // .setVisible(true);
120 // optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
121 // .setVisible(true);
122 // optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
125 // if (fab != null) fab.hide();
130 mActivity.fabShouldShow();
131 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
133 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
135 if (dy < 0) mActivity.fabShouldShow();
136 if (dy > 0) fab.hide();
140 swiper = mActivity.findViewById(R.id.account_swiper);
141 Colors.themeWatch.observe(this, this::themeChanged);
142 swiper.setOnRefreshListener(() -> {
143 debug("ui", "refreshing accounts via swipe");
144 mActivity.scheduleTransactionListRetrieval();
147 Data.accounts.addObserver(
148 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
150 private void update_account_table() {
151 if (this.getContext() == null) return;
153 AccountSummaryViewModel.scheduleAccountListReload();
155 void stopSelection() {
156 modelAdapter.stopSelection();
157 if (optMenu != null) {
158 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
159 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
160 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
163 if (fab != null) fab.show();
166 public void onCancelAccSelection(MenuItem item) {
169 public void onConfirmAccSelection(MenuItem item) {
170 AccountSummaryViewModel.commitSelections(mActivity);
174 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
175 // Inflate the menu; this adds items to the action bar if it is present.
176 inflater.inflate(R.menu.account_summary, menu);
179 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
180 if (mShowOnlyStarred == null) throw new AssertionError();
181 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
182 if (mCancelSelection == null) throw new AssertionError();
183 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
184 if (mConfirmSelection == null) throw new AssertionError();
186 Data.optShowOnlyStarred.addObserver((o, arg) -> {
187 boolean newValue = Data.optShowOnlyStarred.get();
188 debug("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
189 mShowOnlyStarred.setChecked(newValue);
190 update_account_table();
193 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
195 debug("menu", "Accounts: onCreateOptionsMenu called");
197 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
198 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
199 SharedPreferences.Editor editor = pref.edit();
200 boolean flag = item.isChecked();
201 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
203 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
209 mCancelSelection.setOnMenuItemClickListener(item -> {
214 mConfirmSelection.setOnMenuItemClickListener(item -> {
215 AccountSummaryViewModel.commitSelections(mActivity);