]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
19048c91b8d895fe3444d114f617b3d93d4c511c
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
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;
31
32 import com.google.android.material.floatingactionbutton.FloatingActionButton;
33
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;
39
40
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;
46
47 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
48
49 public class AccountSummaryFragment extends MobileLedgerListFragment {
50
51     MenuItem mShowOnlyStarred;
52     public AccountSummaryAdapter modelAdapter;
53     private Menu optMenu;
54     private FloatingActionButton fab;
55     @Override
56     public void onCreate(@Nullable Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         Log.d("flow", "AccountSummaryFragment.onCreate()");
59         setHasOptionsMenu(true);
60
61         Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
62     }
63     public void onAttach(Context context) {
64         super.onAttach(context);
65         Log.d("flow", "AccountSummaryFragment.onAttach()");
66         mActivity = (MainActivity) context;
67     }
68     @Override
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);
73     }
74
75     @Override
76
77     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
78         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
79         super.onActivityCreated(savedInstanceState);
80
81         modelAdapter = new AccountSummaryAdapter();
82
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);
91
92         fab = mActivity.findViewById(R.id.btn_add_transaction);
93
94 //        root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
95 //                new RecyclerItemListener.RecyclerTouchListener() {
96 //                    @Override
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);
101 //                        }
102 //                        else {
103 //                            List<LedgerAccount> accounts = Data.accounts.get();
104 //                            if (accounts != null) {
105 //                                LedgerAccount account = accounts.get(position);
106 //
107 //                                mActivity.showAccountTransactions(account);
108 //                            }
109 //                        }
110 //                    }
111 //
112 //                    @Override
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);
122 //                        }
123 //                        {
124 //                            if (fab != null) fab.hide();
125 //                        }
126 //                    }
127 //                }));
128
129         mActivity.fabShouldShow();
130         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
131             @Override
132             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
133                 if (fab != null) {
134                     if (dy < 0) mActivity.fabShouldShow();
135                     if (dy > 0) fab.hide();
136                 }
137             }
138         });
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();
144         });
145
146         Data.accounts.addObserver(
147                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
148     }
149     private void update_account_table() {
150         if (this.getContext() == null) return;
151
152         AccountSummaryViewModel.scheduleAccountListReload();
153     }
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);
160         }
161         {
162             if (fab != null) fab.show();
163         }
164     }
165     public void onCancelAccSelection(MenuItem item) {
166         stopSelection();
167     }
168     public void onConfirmAccSelection(MenuItem item) {
169         AccountSummaryViewModel.commitSelections(mActivity);
170         stopSelection();
171     }
172     @Override
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);
176         optMenu = menu;
177
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();
184
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();
190         });
191
192         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
193
194         Log.d("menu", "Accounts: onCreateOptionsMenu called");
195
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);
201             Log.d("pref",
202                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
203             editor.apply();
204
205             return true;
206         });
207
208         mCancelSelection.setOnMenuItemClickListener(item -> {
209             stopSelection();
210             return true;
211         });
212
213         mConfirmSelection.setOnMenuItemClickListener(item -> {
214             AccountSummaryViewModel.commitSelections(mActivity);
215             stopSelection();
216
217             return true;
218         });
219     }
220 }