]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
b6943adc41ae8d89d994d25a1c28c91f79665c31
[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 Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. 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.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;
36
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
44 import java.util.List;
45 import java.util.Observer;
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     private AccountSummaryAdapter modelAdapter;
53     private Menu optMenu;
54     private FloatingActionButton fab;
55     private Observer backgroundTaskCountObserver;
56     @Override
57     public void onDestroy() {
58         if (backgroundTaskCountObserver != null) {
59             Log.d("acc", "destroying background task count observer");
60             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
61         }
62         super.onDestroy();
63     }
64     @Override
65     public void onCreate(@Nullable Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67         setHasOptionsMenu(true);
68
69         if (backgroundTaskCountObserver == null) {
70             Log.d("acc", "creating background task count observer");
71             Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
72                 if (mActivity == null) return;
73                 if (swiper == null) return;
74                 mActivity.runOnUiThread(() -> {
75                     int cnt = Data.backgroundTaskCount.get();
76                     Log.d("acc", String.format("background task count changed to %d", cnt));
77                     swiper.setRefreshing(cnt > 0);
78                 });
79             });
80         }
81     }
82     public void onAttach(Context context) {
83         super.onAttach(context);
84         mActivity = (MainActivity) context;
85     }
86     @Override
87     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
88                              @Nullable Bundle savedInstanceState) {
89         Log.d("flow", "AccountSummaryFragment.onCreateView()");
90         return inflater.inflate(R.layout.account_summary_fragment, container, false);
91     }
92
93     @Override
94
95     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
96         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
97         super.onActivityCreated(savedInstanceState);
98
99         modelAdapter = new AccountSummaryAdapter();
100
101         root = mActivity.findViewById(R.id.account_root);
102         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
103         llm.setOrientation(LinearLayoutManager.VERTICAL);
104         root.setLayoutManager(llm);
105         root.setAdapter(modelAdapter);
106
107         fab = mActivity.findViewById(R.id.btn_add_transaction);
108
109         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
110                 new RecyclerItemListener.RecyclerTouchListener() {
111                     @Override
112                     public void onClickItem(View v, int position) {
113                         Log.d("value", String.format("item %d clicked", position));
114                         if (modelAdapter.isSelectionActive()) {
115                             modelAdapter.selectItem(position);
116                         }
117                         else {
118                             List<LedgerAccount> accounts = Data.accounts.get();
119                             if (accounts != null) {
120                                 LedgerAccount account = accounts.get(position);
121
122                                 mActivity.showAccountTransactions(account);
123                             }
124                         }
125                     }
126
127                     @Override
128                     public void onLongClickItem(View v, int position) {
129                         Log.d("value", String.format("item %d long-clicked", position));
130                         modelAdapter.startSelection();
131                         if (optMenu != null) {
132                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
133                                     .setVisible(true);
134                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
135                                     .setVisible(true);
136                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
137                         }
138                         {
139                             if (fab != null) fab.hide();
140                         }
141                     }
142                 }));
143
144         mActivity.fabShouldShow();
145         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
146             @Override
147             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
148                 if (fab != null) {
149                     if (dy < 0) mActivity.fabShouldShow();
150                     if (dy > 0) fab.hide();
151                 }
152             }
153         });
154         swiper = mActivity.findViewById(R.id.account_swiper);
155         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
156         swiper.setOnRefreshListener(() -> {
157             Log.d("ui", "refreshing accounts via swipe");
158             mActivity.scheduleTransactionListRetrieval();
159         });
160
161         Data.accounts.addObserver(
162                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
163         Data.profile.addObserver(
164                 (o, arg) -> mActivity.runOnUiThread(() -> AccountSummaryViewModel.scheduleAccountListReload()));
165         update_account_table();
166     }
167     private void update_account_table() {
168         if (this.getContext() == null) return;
169
170         AccountSummaryViewModel.scheduleAccountListReload();
171     }
172     void stopSelection() {
173         modelAdapter.stopSelection();
174         if (optMenu != null) {
175             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
176             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
177             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
178         }
179         {
180             if (fab != null) fab.show();
181         }
182     }
183     public void onCancelAccSelection(MenuItem item) {
184         stopSelection();
185     }
186     public void onConfirmAccSelection(MenuItem item) {
187         AccountSummaryViewModel.commitSelections(mActivity);
188         stopSelection();
189     }
190     @Override
191     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
192         // Inflate the menu; this adds items to the action bar if it is present.
193         inflater.inflate(R.menu.account_summary, menu);
194         optMenu = menu;
195
196         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
197         if (mShowOnlyStarred == null) throw new AssertionError();
198         MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
199         if (mCancelSelection == null) throw new AssertionError();
200         MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
201         if (mConfirmSelection == null) throw new AssertionError();
202
203         Data.optShowOnlyStarred.addObserver((o, arg) -> {
204             boolean newValue = Data.optShowOnlyStarred.get();
205             Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
206             mShowOnlyStarred.setChecked(newValue);
207             update_account_table();
208         });
209
210         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
211
212         Log.d("menu", "Accounts: onCreateOptionsMenu called");
213
214         mShowOnlyStarred.setOnMenuItemClickListener(item -> {
215             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
216             SharedPreferences.Editor editor = pref.edit();
217             boolean flag = item.isChecked();
218             editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
219             Log.d("pref",
220                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
221             editor.apply();
222
223             return true;
224         });
225
226         mCancelSelection.setOnMenuItemClickListener(item -> {
227             stopSelection();
228             return true;
229         });
230
231         mConfirmSelection.setOnMenuItemClickListener(item -> {
232             AccountSummaryViewModel.commitSelections(mActivity);
233             stopSelection();
234
235             return true;
236         });
237     }
238 }