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