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