]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
private field
[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 org.jetbrains.annotations.NotNull;
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     private MenuItem mShowOnlyStarred;
53     public AccountSummaryAdapter modelAdapter;
54     private Menu optMenu;
55     private FloatingActionButton fab;
56     @Override
57     public void onCreate(@Nullable Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         Log.d("flow", "AccountSummaryFragment.onCreate()");
60         setHasOptionsMenu(true);
61
62         Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
63     }
64     public void onAttach(@NotNull Context context) {
65         super.onAttach(context);
66         Log.d("flow", "AccountSummaryFragment.onAttach()");
67         mActivity = (MainActivity) context;
68     }
69     @Override
70     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
71                              @Nullable Bundle savedInstanceState) {
72         Log.d("flow", "AccountSummaryFragment.onCreateView()");
73         return inflater.inflate(R.layout.account_summary_fragment, container, false);
74     }
75
76     @Override
77
78     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
79         Log.d("flow", "AccountSummaryFragment.onActivityCreated()");
80         super.onActivityCreated(savedInstanceState);
81
82         modelAdapter = new AccountSummaryAdapter();
83
84         mActivity.mAccountSummaryFragment = this;
85         root = mActivity.findViewById(R.id.account_root);
86         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
87         llm.setOrientation(RecyclerView.VERTICAL);
88         root.setLayoutManager(llm);
89         root.setAdapter(modelAdapter);
90         DividerItemDecoration did = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
91         root.addItemDecoration(did);
92
93         fab = mActivity.findViewById(R.id.btn_add_transaction);
94
95 //        root.addOnItemTouchListener(new RecyclerItemListener(mActivity, root,
96 //                new RecyclerItemListener.RecyclerTouchListener() {
97 //                    @Override
98 //                    public void onClickItem(View v, int position) {
99 //                        Log.d("value", String.format("item %d clicked", position));
100 //                        if (modelAdapter.isSelectionActive()) {
101 //                            modelAdapter.selectItem(position);
102 //                        }
103 //                        else {
104 //                            List<LedgerAccount> accounts = Data.accounts.get();
105 //                            if (accounts != null) {
106 //                                LedgerAccount account = accounts.get(position);
107 //
108 //                                mActivity.showAccountTransactions(account);
109 //                            }
110 //                        }
111 //                    }
112 //
113 //                    @Override
114 //                    public void onLongClickItem(View v, int position) {
115 //                        Log.d("value", String.format("item %d long-clicked", position));
116 //                        modelAdapter.startSelection();
117 //                        if (optMenu != null) {
118 //                            optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
119 //                                    .setVisible(true);
120 //                            optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
121 //                                    .setVisible(true);
122 //                            optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
123 //                        }
124 //                        {
125 //                            if (fab != null) fab.hide();
126 //                        }
127 //                    }
128 //                }));
129
130         mActivity.fabShouldShow();
131         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
132             @Override
133             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
134                 if (fab != null) {
135                     if (dy < 0) mActivity.fabShouldShow();
136                     if (dy > 0) fab.hide();
137                 }
138             }
139         });
140         swiper = mActivity.findViewById(R.id.account_swiper);
141         Colors.themeWatch.observe(this, this::themeChanged);
142         swiper.setOnRefreshListener(() -> {
143             Log.d("ui", "refreshing accounts via swipe");
144             mActivity.scheduleTransactionListRetrieval();
145         });
146
147         Data.accounts.addObserver(
148                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
149     }
150     private void update_account_table() {
151         if (this.getContext() == null) return;
152
153         AccountSummaryViewModel.scheduleAccountListReload();
154     }
155     void stopSelection() {
156         modelAdapter.stopSelection();
157         if (optMenu != null) {
158             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
159             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
160             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
161         }
162         {
163             if (fab != null) fab.show();
164         }
165     }
166     public void onCancelAccSelection(MenuItem item) {
167         stopSelection();
168     }
169     public void onConfirmAccSelection(MenuItem item) {
170         AccountSummaryViewModel.commitSelections(mActivity);
171         stopSelection();
172     }
173     @Override
174     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
175         // Inflate the menu; this adds items to the action bar if it is present.
176         inflater.inflate(R.menu.account_summary, menu);
177         optMenu = menu;
178
179         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
180         if (mShowOnlyStarred == null) throw new AssertionError();
181         MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
182         if (mCancelSelection == null) throw new AssertionError();
183         MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
184         if (mConfirmSelection == null) throw new AssertionError();
185
186         Data.optShowOnlyStarred.addObserver((o, arg) -> {
187             boolean newValue = Data.optShowOnlyStarred.get();
188             Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
189             mShowOnlyStarred.setChecked(newValue);
190             update_account_table();
191         });
192
193         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
194
195         Log.d("menu", "Accounts: onCreateOptionsMenu called");
196
197         mShowOnlyStarred.setOnMenuItemClickListener(item -> {
198             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
199             SharedPreferences.Editor editor = pref.edit();
200             boolean flag = item.isChecked();
201             editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
202             Log.d("pref",
203                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
204             editor.apply();
205
206             return true;
207         });
208
209         mCancelSelection.setOnMenuItemClickListener(item -> {
210             stopSelection();
211             return true;
212         });
213
214         mConfirmSelection.setOnMenuItemClickListener(item -> {
215             AccountSummaryViewModel.commitSelections(mActivity);
216             stopSelection();
217
218             return true;
219         });
220     }
221 }