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.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
26 import com.google.android.material.floatingactionbutton.FloatingActionButton;
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.Data;
30 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
31 import net.ktnx.mobileledger.ui.activity.MainActivity;
32 import net.ktnx.mobileledger.utils.Colors;
34 import org.jetbrains.annotations.NotNull;
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.recyclerview.widget.DividerItemDecoration;
39 import androidx.recyclerview.widget.LinearLayoutManager;
40 import androidx.recyclerview.widget.RecyclerView;
42 import static net.ktnx.mobileledger.utils.Logger.debug;
44 public class AccountSummaryFragment extends MobileLedgerListFragment {
46 public AccountSummaryAdapter modelAdapter;
48 private MenuItem mShowOnlyStarred;
51 private FloatingActionButton fab;
53 public void onCreate(@Nullable Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 debug("flow", "AccountSummaryFragment.onCreate()");
56 setHasOptionsMenu(true);
58 Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
60 public void onAttach(@NotNull Context context) {
61 super.onAttach(context);
62 debug("flow", "AccountSummaryFragment.onAttach()");
63 mActivity = (MainActivity) context;
66 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
67 @Nullable Bundle savedInstanceState) {
68 debug("flow", "AccountSummaryFragment.onCreateView()");
69 return inflater.inflate(R.layout.account_summary_fragment, container, false);
74 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
75 debug("flow", "AccountSummaryFragment.onActivityCreated()");
76 super.onActivityCreated(savedInstanceState);
78 modelAdapter = new AccountSummaryAdapter();
80 mActivity.mAccountSummaryFragment = this;
81 root = mActivity.findViewById(R.id.account_root);
82 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
83 llm.setOrientation(RecyclerView.VERTICAL);
84 root.setLayoutManager(llm);
85 root.setAdapter(modelAdapter);
86 DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
87 root.addItemDecoration(did);
89 fab = mActivity.findViewById(R.id.btn_add_transaction);
91 // root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
92 // new RecyclerItemListener.RecyclerTouchListener() {
94 // public void onClickItem(View v, int position) {
95 // debug("value", String.format("item %d clicked", position));
96 // if (modelAdapter.isSelectionActive()) {
97 // modelAdapter.selectItem(position);
100 // List<LedgerAccount> accounts = Data.accounts.get();
101 // if (accounts != null) {
102 // LedgerAccount account = accounts.get(position);
104 // mActivity.showAccountTransactions(account);
110 // public void onLongClickItem(View v, int position) {
111 // debug("value", String.format("item %d long-clicked", position));
112 // modelAdapter.startSelection();
113 // if (optMenu != null) {
114 // optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
115 // .setVisible(true);
116 // optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
117 // .setVisible(true);
118 // optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
121 // if (fab != null) fab.hide();
126 mActivity.fabShouldShow();
127 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
129 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
131 if (dy < 0) mActivity.fabShouldShow();
132 if (dy > 0) fab.hide();
136 swiper = mActivity.findViewById(R.id.account_swiper);
137 Colors.themeWatch.observe(this, this::themeChanged);
138 swiper.setOnRefreshListener(() -> {
139 debug("ui", "refreshing accounts via swipe");
140 mActivity.scheduleTransactionListRetrieval();
143 Data.accounts.addObserver(
144 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
147 void stopSelection() {
148 modelAdapter.stopSelection();
149 if (optMenu != null) {
150 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
151 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
152 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
155 if (fab != null) fab.show();
158 public void onCancelAccSelection(MenuItem item) {
161 public void onConfirmAccSelection(MenuItem item) {
162 AccountSummaryViewModel.commitSelections(mActivity);
166 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
167 // Inflate the menu; this adds items to the action bar if it is present.
168 inflater.inflate(R.menu.account_summary, menu);
171 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
172 if (mShowOnlyStarred == null) throw new AssertionError();
173 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
174 if (mCancelSelection == null) throw new AssertionError();
175 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
176 if (mConfirmSelection == null) throw new AssertionError();
178 Data.optShowOnlyStarred.addObserver((o, arg) -> {
179 boolean newValue = Data.optShowOnlyStarred.get();
180 debug("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
181 mShowOnlyStarred.setChecked(newValue);
182 update_account_table();
185 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
187 debug("menu", "Accounts: onCreateOptionsMenu called");
189 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
190 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
191 SharedPreferences.Editor editor = pref.edit();
192 boolean flag = item.isChecked();
193 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
195 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
201 mCancelSelection.setOnMenuItemClickListener(item -> {
206 mConfirmSelection.setOnMenuItemClickListener(item -> {
207 AccountSummaryViewModel.commitSelections(mActivity);