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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.account_summary;
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;
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 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
35 import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding;
36 import net.ktnx.mobileledger.db.AccountWithAmounts;
37 import net.ktnx.mobileledger.db.DB;
38 import net.ktnx.mobileledger.db.Profile;
39 import net.ktnx.mobileledger.model.AccountListItem;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.LedgerAccount;
42 import net.ktnx.mobileledger.ui.FabManager;
43 import net.ktnx.mobileledger.ui.MainModel;
44 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
45 import net.ktnx.mobileledger.ui.activity.MainActivity;
46 import net.ktnx.mobileledger.utils.Colors;
48 import org.jetbrains.annotations.NotNull;
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.List;
54 import static net.ktnx.mobileledger.utils.Logger.debug;
56 public class AccountSummaryFragment extends MobileLedgerListFragment {
57 public AccountSummaryAdapter modelAdapter;
58 private AccountSummaryFragmentBinding b;
60 public void onCreate(@Nullable Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 debug("flow", "AccountSummaryFragment.onCreate()");
63 setHasOptionsMenu(true);
65 public void onAttach(@NotNull Context context) {
66 super.onAttach(context);
67 debug("flow", "AccountSummaryFragment.onAttach()");
70 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
71 @Nullable Bundle savedInstanceState) {
72 debug("flow", "AccountSummaryFragment.onCreateView()");
73 b = AccountSummaryFragmentBinding.inflate(inflater, container, false);
77 public SwipeRefreshLayout getRefreshLayout() {
78 return b.accountSwipeRefreshLayout;
81 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
82 debug("flow", "AccountSummaryFragment.onActivityCreated()");
83 super.onViewCreated(view, savedInstanceState);
85 MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
87 Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
88 this::onBackgroundTaskRunningChanged);
90 modelAdapter = new AccountSummaryAdapter();
91 MainActivity mainActivity = getMainActivity();
93 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
94 llm.setOrientation(RecyclerView.VERTICAL);
95 b.accountRoot.setLayoutManager(llm);
96 b.accountRoot.setAdapter(modelAdapter);
97 DividerItemDecoration did =
98 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
99 b.accountRoot.addItemDecoration(did);
101 mainActivity.fabShouldShow();
103 if (mainActivity instanceof FabManager.FabHandler)
104 FabManager.handle(mainActivity, b.accountRoot);
106 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
107 b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
108 debug("ui", "refreshing accounts via swipe");
109 model.scheduleTransactionListRetrieval();
112 Data.observeProfile(this, this::onProfileChanged);
114 private void onProfileChanged(Profile profile) {
120 .getAllWithAmounts(profile.getId())
121 .observe(getViewLifecycleOwner(), list -> AsyncTask.execute(() -> {
122 List<AccountListItem> adapterList = new ArrayList<>();
123 adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
124 HashMap<String, LedgerAccount> accMap = new HashMap<>();
125 for (AccountWithAmounts dbAcc : list) {
126 LedgerAccount parent = null;
127 String parentName = dbAcc.account.getParentName();
128 if (parentName != null)
129 parent = accMap.get(parentName);
131 parent.setHasSubAccounts(true);
132 final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent);
133 if (account.isVisible())
134 adapterList.add(new AccountListItem.Account(account));
135 accMap.put(dbAcc.account.getName(), account);
137 modelAdapter.setAccounts(adapterList);
138 Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);