]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
setupTheme routine with a profile argument
[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.model.LedgerAccount;
37 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
38 import net.ktnx.mobileledger.ui.RecyclerItemListener;
39 import net.ktnx.mobileledger.ui.activity.MainActivity;
40 import net.ktnx.mobileledger.utils.Colors;
41
42 import java.util.List;
43 import java.util.Observer;
44
45 import androidx.annotation.NonNull;
46 import androidx.annotation.Nullable;
47 import androidx.recyclerview.widget.LinearLayoutManager;
48 import androidx.recyclerview.widget.RecyclerView;
49
50 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
51
52 public class AccountSummaryFragment extends MobileLedgerListFragment {
53
54     MenuItem mShowOnlyStarred;
55     private AccountSummaryAdapter modelAdapter;
56     private Menu optMenu;
57     private FloatingActionButton fab;
58     private Observer backgroundTaskCountObserver;
59     @Override
60     public void onDestroy() {
61         if (backgroundTaskCountObserver != null) {
62             Log.d("acc", "destroying background task count observer");
63             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
64         }
65         super.onDestroy();
66     }
67     @Override
68     public void onCreate(@Nullable Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setHasOptionsMenu(true);
71
72         if (backgroundTaskCountObserver == null) {
73             Log.d("acc", "creating background task count observer");
74             Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
75                 if (mActivity == null) return;
76                 if (swiper == null) return;
77                 mActivity.runOnUiThread(() -> {
78                     int cnt = Data.backgroundTaskCount.get();
79                     Log.d("acc", String.format("background task count changed to %d", cnt));
80                     swiper.setRefreshing(cnt > 0);
81                 });
82             });
83         }
84     }
85     public void onAttach(Context context) {
86         super.onAttach(context);
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         root = mActivity.findViewById(R.id.account_root);
105         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
106         llm.setOrientation(RecyclerView.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         mActivity.fabShouldShow();
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) mActivity.fabShouldShow();
153                     if (dy > 0) fab.hide();
154                 }
155             }
156         });
157         swiper = mActivity.findViewById(R.id.account_swiper);
158         Colors.themeWatch.addObserver(
159                 (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 }