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.support.annotation.NonNull;
25 import android.support.annotation.Nullable;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.v7.widget.LinearLayoutManager;
28 import android.support.v7.widget.RecyclerView;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.model.Data;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
41 import net.ktnx.mobileledger.ui.RecyclerItemListener;
42 import net.ktnx.mobileledger.ui.activity.MainActivity;
43 import net.ktnx.mobileledger.utils.Colors;
45 import java.util.List;
46 import java.util.Observer;
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 private 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 root = mActivity.findViewById(R.id.account_root);
103 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
104 llm.setOrientation(LinearLayoutManager.VERTICAL);
105 root.setLayoutManager(llm);
106 root.setAdapter(modelAdapter);
108 fab = mActivity.findViewById(R.id.btn_add_transaction);
110 root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
111 new RecyclerItemListener.RecyclerTouchListener() {
113 public void onClickItem(View v, int position) {
114 Log.d("value", String.format("item %d clicked", position));
115 if (modelAdapter.isSelectionActive()) {
116 modelAdapter.selectItem(position);
119 List<LedgerAccount> accounts = Data.accounts.get();
120 if (accounts != null) {
121 LedgerAccount account = accounts.get(position);
123 mActivity.showAccountTransactions(account);
129 public void onLongClickItem(View v, int position) {
130 Log.d("value", String.format("item %d long-clicked", position));
131 modelAdapter.startSelection();
132 if (optMenu != null) {
133 optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
135 optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
137 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
140 if (fab != null) fab.hide();
145 mActivity.fabShouldShow();
146 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
148 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
150 if (dy < 0) mActivity.fabShouldShow();
151 if (dy > 0) fab.hide();
155 swiper = mActivity.findViewById(R.id.account_swiper);
156 Colors.themeWatch.addObserver(
157 (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
158 swiper.setColorSchemeColors(Colors.primary);
159 swiper.setOnRefreshListener(() -> {
160 Log.d("ui", "refreshing accounts via swipe");
161 mActivity.scheduleTransactionListRetrieval();
164 Data.accounts.addObserver(
165 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
166 Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(
167 AccountSummaryViewModel::scheduleAccountListReload));
168 update_account_table();
170 private void update_account_table() {
171 if (this.getContext() == null) return;
173 AccountSummaryViewModel.scheduleAccountListReload();
175 void stopSelection() {
176 modelAdapter.stopSelection();
177 if (optMenu != null) {
178 optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
179 optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
180 optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
183 if (fab != null) fab.show();
186 public void onCancelAccSelection(MenuItem item) {
189 public void onConfirmAccSelection(MenuItem item) {
190 AccountSummaryViewModel.commitSelections(mActivity);
194 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
195 // Inflate the menu; this adds items to the action bar if it is present.
196 inflater.inflate(R.menu.account_summary, menu);
199 mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
200 if (mShowOnlyStarred == null) throw new AssertionError();
201 MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
202 if (mCancelSelection == null) throw new AssertionError();
203 MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
204 if (mConfirmSelection == null) throw new AssertionError();
206 Data.optShowOnlyStarred.addObserver((o, arg) -> {
207 boolean newValue = Data.optShowOnlyStarred.get();
208 Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
209 mShowOnlyStarred.setChecked(newValue);
210 update_account_table();
213 mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
215 Log.d("menu", "Accounts: onCreateOptionsMenu called");
217 mShowOnlyStarred.setOnMenuItemClickListener(item -> {
218 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
219 SharedPreferences.Editor editor = pref.edit();
220 boolean flag = item.isChecked();
221 editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
223 "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
229 mCancelSelection.setOnMenuItemClickListener(item -> {
234 mConfirmSelection.setOnMenuItemClickListener(item -> {
235 AccountSummaryViewModel.commitSelections(mActivity);