]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
two more lambdas
[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.arch.lifecycle.ViewModelProviders;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.os.Bundle;
24 import android.preference.PreferenceManager;
25 import android.support.annotation.NonNull;
26 import android.support.annotation.Nullable;
27 import android.support.design.widget.FloatingActionButton;
28 import android.support.v7.widget.LinearLayoutManager;
29 import android.support.v7.widget.RecyclerView;
30 import android.util.Log;
31 import android.view.LayoutInflater;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
42 import net.ktnx.mobileledger.ui.RecyclerItemListener;
43 import net.ktnx.mobileledger.ui.activity.MainActivity;
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 AccountSummaryViewModel model;
54     private AccountSummaryAdapter modelAdapter;
55     private Menu optMenu;
56     private FloatingActionButton fab;
57     private Observer backgroundTaskCountObserver;
58     @Override
59     public void onDestroy() {
60         if (backgroundTaskCountObserver != null) {
61             Log.d("acc", "destroying background task count observer");
62             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
63         }
64         super.onDestroy();
65     }
66     @Override
67     public void onCreate(@Nullable Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         setHasOptionsMenu(true);
70
71         if (backgroundTaskCountObserver == null) {
72             Log.d("acc", "creating background task count observer");
73             Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
74                 if (mActivity == null) return;
75                 if (swiper == null) return;
76                 mActivity.runOnUiThread(() -> {
77                     int cnt = Data.backgroundTaskCount.get();
78                     Log.d("acc", String.format("background task count changed to %d", cnt));
79                     swiper.setRefreshing(cnt > 0);
80                 });
81             });
82         }
83     }
84     public void onAttach(Context context) {
85         super.onAttach(context);
86         mActivity = (MainActivity) context;
87     }
88     @Override
89     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
90                              @Nullable Bundle savedInstanceState) {
91         Log.d("flow", "AccountSummaryFragment.onCreateView()");
92         return inflater.inflate(R.layout.account_summary_fragment, container, false);
93     }
94
95     @Override
96
97     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
98         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
99         super.onActivityCreated(savedInstanceState);
100
101         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
102         modelAdapter = new AccountSummaryAdapter();
103
104         root = mActivity.findViewById(R.id.account_root);
105         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
106         llm.setOrientation(LinearLayoutManager.VERTICAL);
107         root.setLayoutManager(llm);
108         root.setAdapter(modelAdapter);
109
110         fab = mActivity.findViewById(R.id.btn_add_transaction);
111
112         root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
113                 new RecyclerItemListener.RecyclerTouchListener() {
114                     @Override
115                     public void onClickItem(View v, int position) {
116                         Log.d("value", String.format("item %d clicked", position));
117                         if (modelAdapter.isSelectionActive()) {
118                             modelAdapter.selectItem(position);
119                         }
120                         else {
121                             List<LedgerAccount> accounts = Data.accounts.get();
122                             if (accounts != null) {
123                                 LedgerAccount account = accounts.get(position);
124
125                                 mActivity.showAccountTransactions(account);
126                             }
127                         }
128                     }
129
130                     @Override
131                     public void onLongClickItem(View v, int position) {
132                         Log.d("value", String.format("item %d long-clicked", position));
133                         modelAdapter.startSelection();
134                         if (optMenu != null) {
135                             optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
136                                     .setVisible(true);
137                             optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
138                                     .setVisible(true);
139                             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
140                         }
141                         {
142                             if (fab != null) fab.hide();
143                         }
144                     }
145                 }));
146
147         fab.show();
148         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
149             @Override
150             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
151                 if (fab != null) {
152                     if (dy < 0) fab.show();
153                     if (dy > 0) fab.hide();
154                 }
155             }
156         });
157         swiper = mActivity.findViewById(R.id.account_swiper);
158         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
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(
167                 (o, arg) -> mActivity.runOnUiThread(() -> model.scheduleAccountListReload()));
168         update_account_table();
169     }
170     private void update_account_table() {
171         if (this.getContext() == null) return;
172
173         model.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 }