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;
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;
49 public class AccountSummaryFragment extends MobileLedgerListFragment {
51 MenuItem mShowOnlyStarred;
52 public AccountSummaryAdapter modelAdapter;
54 private FloatingActionButton fab;
56 public void onCreate(@Nullable Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 Log.d("flow", "AccountSummaryFragment.onCreate()");
59 setHasOptionsMenu(true);
61 Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
63 public void onAttach(Context context) {
64 super.onAttach(context);
65 Log.d("flow", "AccountSummaryFragment.onAttach()");
66 mActivity = (MainActivity) context;
69 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
70 @Nullable Bundle savedInstanceState) {
71 Log.d("flow", "AccountSummaryFragment.onCreateView()");
72 return inflater.inflate(R.layout.account_summary_fragment, container, false);
77 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
78 Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
79 super.onActivityCreated(savedInstanceState);
81 modelAdapter = new AccountSummaryAdapter();
83 mActivity.mAccountSummaryFragment = this;
84 root = mActivity.findViewById(R.id.account_root);
85 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
86 llm.setOrientation(RecyclerView.VERTICAL);
87 root.setLayoutManager(llm);
88 root.setAdapter(modelAdapter);
89 DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
90 root.addItemDecoration(did);
92 fab = mActivity.findViewById(R.id.btn_add_transaction);
94 // root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
95 // new RecyclerItemListener.RecyclerTouchListener() {
97 // public void onClickItem(View v, int position) {
98 // Log.d("value", String.format("item %d clicked", position));
99 // if (modelAdapter.isSelectionActive()) {
100 // modelAdapter.selectItem(position);
103 // List<LedgerAccount> accounts = Data.accounts.get();
104 // if (accounts != null) {
105 // LedgerAccount account = accounts.get(position);
107 // mActivity.showAccountTransactions(account);
113 // public void onLongClickItem(View v, int position) {
114 // Log.d("value", String.format("item %d long-clicked", position));
115 // modelAdapter.startSelection();
116 // if (optMenu != null) {
117 // optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
118 // .setVisible(true);
119 // optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
120 // .setVisible(true);
121 // optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
124 // if (fab != null) fab.hide();
129 mActivity.fabShouldShow();
130 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
132 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
134 if (dy < 0) mActivity.fabShouldShow();
135 if (dy > 0) fab.hide();
139 swiper = mActivity.findViewById(R.id.account_swiper);
140 Colors.themeWatch.observe(this, this::themeChanged);
141 swiper.setOnRefreshListener(() -> {
142 Log.d("ui", "refreshing accounts via swipe");
143 mActivity.scheduleTransactionListRetrieval();
146 Data.accounts.addObserver(
147 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
149 private void update_account_table() {
150 if (this.getContext() == null) return;
152 AccountSummaryViewModel.scheduleAccountListReload();
154 void stopSelection() {
155 modelAdapter.stopSelection();
156 if (optMenu != null) {
157 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
158 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
159 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
162 if (fab != null) fab.show();
165 public void onCancelAccSelection(MenuItem item) {
168 public void onConfirmAccSelection(MenuItem item) {
169 AccountSummaryViewModel.commitSelections(mActivity);
173 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
174 // Inflate the menu; this adds items to the action bar if it is present.
175 inflater.inflate(R.menu.account_summary, menu);
178 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
179 if (mShowOnlyStarred == null) throw new AssertionError();
180 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
181 if (mCancelSelection == null) throw new AssertionError();
182 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
183 if (mConfirmSelection == null) throw new AssertionError();
185 Data.optShowOnlyStarred.addObserver((o, arg) -> {
186 boolean newValue = Data.optShowOnlyStarred.get();
187 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
188 mShowOnlyStarred.setChecked(newValue);
189 update_account_table();
192 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
194 Log.d("menu", "Accounts: onCreateOptionsMenu called");
196 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
197 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
198 SharedPreferences.Editor editor = pref.edit();
199 boolean flag = item.isChecked();
200 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
202 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
208 mCancelSelection.setOnMenuItemClickListener(item -> {
213 mConfirmSelection.setOnMenuItemClickListener(item -> {
214 AccountSummaryViewModel.commitSelections(mActivity);