]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
04cde356a6e4096c5899b1e2103462059106d2af
[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.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 import net.ktnx.mobileledger.utils.Colors;
44
45 import java.util.List;
46 import java.util.Observer;
47
48 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
49
50 public class AccountSummaryFragment extends MobileLedgerListFragment {
51
52     MenuItem mShowOnlyStarred;
53     private AccountSummaryAdapter modelAdapter;
54     private Menu optMenu;
55     private FloatingActionButton fab;
56     private Observer backgroundTaskCountObserver;
57     @Override
58     public void onDestroy() {
59         if (backgroundTaskCountObserver != null) {
60             Log.d("acc", "destroying background task count observer");
61             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
62         }
63         super.onDestroy();
64     }
65     @Override
66     public void onCreate(@Nullable Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         setHasOptionsMenu(true);
69
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);
79                 });
80             });
81         }
82     }
83     public void onAttach(Context context) {
84         super.onAttach(context);
85         mActivity = (MainActivity) context;
86     }
87     @Override
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);
92     }
93
94     @Override
95
96     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
97         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
98         super.onActivityCreated(savedInstanceState);
99
100         modelAdapter = new AccountSummaryAdapter();
101
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);
107
108         fab = mActivity.findViewById(R.id.btn_add_transaction);
109
110         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
111                 new RecyclerItemListener.RecyclerTouchListener() {
112                     @Override
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);
117                         }
118                         else {
119                             List<LedgerAccount> accounts = Data.accounts.get();
120                             if (accounts != null) {
121                                 LedgerAccount account = accounts.get(position);
122
123                                 mActivity.showAccountTransactions(account);
124                             }
125                         }
126                     }
127
128                     @Override
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)
134                                     .setVisible(true);
135                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
136                                     .setVisible(true);
137                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
138                         }
139                         {
140                             if (fab != null) fab.hide();
141                         }
142                     }
143                 }));
144
145         mActivity.fabShouldShow();
146         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
147             @Override
148             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
149                 if (fab != null) {
150                     if (dy < 0) mActivity.fabShouldShow();
151                     if (dy > 0) fab.hide();
152                 }
153             }
154         });
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();
162         });
163
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();
169     }
170     private void update_account_table() {
171         if (this.getContext() == null) return;
172
173         AccountSummaryViewModel.scheduleAccountListReload();
174     }
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);
181         }
182         {
183             if (fab != null) fab.show();
184         }
185     }
186     public void onCancelAccSelection(MenuItem item) {
187         stopSelection();
188     }
189     public void onConfirmAccSelection(MenuItem item) {
190         AccountSummaryViewModel.commitSelections(mActivity);
191         stopSelection();
192     }
193     @Override
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);
197         optMenu = menu;
198
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();
205
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();
211         });
212
213         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
214
215         Log.d("menu", "Accounts: onCreateOptionsMenu called");
216
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);
222             Log.d("pref",
223                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
224             editor.apply();
225
226             return true;
227         });
228
229         mCancelSelection.setOnMenuItemClickListener(item -> {
230             stopSelection();
231             return true;
232         });
233
234         mConfirmSelection.setOnMenuItemClickListener(item -> {
235             AccountSummaryViewModel.commitSelections(mActivity);
236             stopSelection();
237
238             return true;
239         });
240     }
241 }