]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
whitespace
[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     public AccountSummaryAdapter modelAdapter;
46     /*
47         private MenuItem mShowOnlyStarred;
48         private Menu optMenu;
49     */
50     private FloatingActionButton fab;
51     @Override
52     public void onCreate(@Nullable Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         debug("flow", "AccountSummaryFragment.onCreate()");
55         setHasOptionsMenu(true);
56
57         Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
58     }
59     public void onAttach(@NotNull Context context) {
60         super.onAttach(context);
61         debug("flow", "AccountSummaryFragment.onAttach()");
62         mActivity = (MainActivity) context;
63     }
64     @Override
65     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
66                              @Nullable Bundle savedInstanceState) {
67         debug("flow", "AccountSummaryFragment.onCreateView()");
68         return inflater.inflate(R.layout.account_summary_fragment, container, false);
69     }
70
71     @Override
72
73     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
74         debug("flow", "AccountSummaryFragment.onActivityCreated()");
75         super.onActivityCreated(savedInstanceState);
76
77         modelAdapter = new AccountSummaryAdapter();
78
79         mActivity.mAccountSummaryFragment = this;
80         root = mActivity.findViewById(R.id.account_root);
81         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
82         llm.setOrientation(RecyclerView.VERTICAL);
83         root.setLayoutManager(llm);
84         root.setAdapter(modelAdapter);
85         DividerItemDecoration did =
86                 new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
87         root.addItemDecoration(did);
88
89         fab = mActivity.findViewById(R.id.btn_add_transaction);
90
91         mActivity.fabShouldShow();
92
93         manageFabOnScroll();
94
95         swiper = mActivity.findViewById(R.id.account_swiper);
96         Colors.themeWatch.observe(this, this::themeChanged);
97         swiper.setOnRefreshListener(() -> {
98             debug("ui", "refreshing accounts via swipe");
99             Data.scheduleTransactionListRetrieval(mActivity);
100         });
101
102         Data.accounts.addObserver(
103                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
104     }
105 /*
106     void stopSelection() {
107         modelAdapter.stopSelection();
108         if (optMenu != null) {
109             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
110             optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
111             optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
112         }
113         {
114             if (fab != null) fab.show();
115         }
116     }
117     public void onCancelAccSelection(MenuItem item) {
118         stopSelection();
119     }
120     public void onConfirmAccSelection(MenuItem item) {
121         AccountSummaryViewModel.commitSelections(mActivity);
122         stopSelection();
123     }
124     @Override
125     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
126         // Inflate the menu; this adds items to the action bar if it is present.
127         inflater.inflate(R.menu.account_summary, menu);
128         optMenu = menu;
129
130         mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
131         if (mShowOnlyStarred == null) throw new AssertionError();
132         MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
133         if (mCancelSelection == null) throw new AssertionError();
134         MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
135         if (mConfirmSelection == null) throw new AssertionError();
136
137         Data.optShowOnlyStarred.addObserver((o, arg) -> {
138             boolean newValue = Data.optShowOnlyStarred.get();
139             debug("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
140             mShowOnlyStarred.setChecked(newValue);
141             update_account_table();
142         });
143
144         mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
145
146         debug("menu", "Accounts: onCreateOptionsMenu called");
147
148         mShowOnlyStarred.setOnMenuItemClickListener(item -> {
149             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
150             SharedPreferences.Editor editor = pref.edit();
151             boolean flag = item.isChecked();
152             editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
153             debug("pref",
154                     "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
155             editor.apply();
156
157             return true;
158         });
159
160         mCancelSelection.setOnMenuItemClickListener(item -> {
161             stopSelection();
162             return true;
163         });
164
165         mConfirmSelection.setOnMenuItemClickListener(item -> {
166             AccountSummaryViewModel.commitSelections(mActivity);
167             stopSelection();
168
169             return true;
170         });
171     }
172 */
173 }