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