]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
work around trans. list menu items visible with setOffscreenPageLimit(1)
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
1 /*
2  * Copyright © 2021 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.AsyncTask;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.lifecycle.ViewModelProvider;
30 import androidx.recyclerview.widget.DividerItemDecoration;
31 import androidx.recyclerview.widget.LinearLayoutManager;
32 import androidx.recyclerview.widget.RecyclerView;
33
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.db.AccountWithAmounts;
36 import net.ktnx.mobileledger.db.DB;
37 import net.ktnx.mobileledger.db.Profile;
38 import net.ktnx.mobileledger.model.AccountListItem;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.ui.FabManager;
42 import net.ktnx.mobileledger.ui.MainModel;
43 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
44 import net.ktnx.mobileledger.ui.activity.MainActivity;
45 import net.ktnx.mobileledger.utils.Colors;
46
47 import org.jetbrains.annotations.NotNull;
48
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52
53 import static net.ktnx.mobileledger.utils.Logger.debug;
54
55 public class AccountSummaryFragment extends MobileLedgerListFragment {
56     public AccountSummaryAdapter modelAdapter;
57     @Override
58     public void onCreate(@Nullable Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         debug("flow", "AccountSummaryFragment.onCreate()");
61         setHasOptionsMenu(true);
62     }
63     public void onAttach(@NotNull Context context) {
64         super.onAttach(context);
65         debug("flow", "AccountSummaryFragment.onAttach()");
66     }
67     @Override
68     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
69                              @Nullable Bundle savedInstanceState) {
70         debug("flow", "AccountSummaryFragment.onCreateView()");
71         return inflater.inflate(R.layout.account_summary_fragment, container, false);
72     }
73
74     @Override
75     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
76         debug("flow", "AccountSummaryFragment.onActivityCreated()");
77         super.onViewCreated(view, savedInstanceState);
78
79         MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
80
81         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
82                 this::onBackgroundTaskRunningChanged);
83
84         modelAdapter = new AccountSummaryAdapter();
85         MainActivity mainActivity = getMainActivity();
86
87         root = view.findViewById(R.id.account_root);
88         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
89         llm.setOrientation(RecyclerView.VERTICAL);
90         root.setLayoutManager(llm);
91         root.setAdapter(modelAdapter);
92         DividerItemDecoration did =
93                 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
94         root.addItemDecoration(did);
95
96         mainActivity.fabShouldShow();
97
98         if (mainActivity instanceof FabManager.FabHandler)
99             FabManager.handle(mainActivity, root);
100
101         refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout);
102         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
103         refreshLayout.setOnRefreshListener(() -> {
104             debug("ui", "refreshing accounts via swipe");
105             model.scheduleTransactionListRetrieval();
106         });
107
108         Data.observeProfile(this, this::onProfileChanged);
109     }
110     private void onProfileChanged(Profile profile) {
111         if (profile == null)
112             return;
113
114         DB.get()
115           .getAccountDAO()
116           .getAllWithAmounts(profile.getId())
117           .observe(getViewLifecycleOwner(), list -> AsyncTask.execute(() -> {
118               List<AccountListItem> adapterList = new ArrayList<>();
119               adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
120               HashMap<String, LedgerAccount> accMap = new HashMap<>();
121               for (AccountWithAmounts dbAcc : list) {
122                   LedgerAccount parent = null;
123                   String parentName = dbAcc.account.getParentName();
124                   if (parentName != null)
125                       parent = accMap.get(parentName);
126                   if (parent != null)
127                       parent.setHasSubAccounts(true);
128                   final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent);
129                   if (account.isVisible())
130                       adapterList.add(new AccountListItem.Account(account));
131                   accMap.put(dbAcc.account.getName(), account);
132               }
133               modelAdapter.setAccounts(adapterList);
134               Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
135           }));
136     }
137 }