]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
replace AsyncTask.execute with GeneralBackgroundTasks.run
[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.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.lifecycle.ViewModelProvider;
29 import androidx.recyclerview.widget.DividerItemDecoration;
30 import androidx.recyclerview.widget.LinearLayoutManager;
31 import androidx.recyclerview.widget.RecyclerView;
32 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
33
34 import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
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;
47
48 import org.jetbrains.annotations.NotNull;
49
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.List;
53
54 import static net.ktnx.mobileledger.utils.Logger.debug;
55
56 public class AccountSummaryFragment extends MobileLedgerListFragment {
57     public AccountSummaryAdapter modelAdapter;
58     private AccountSummaryFragmentBinding b;
59     @Override
60     public void onCreate(@Nullable Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         debug("flow", "AccountSummaryFragment.onCreate()");
63         setHasOptionsMenu(true);
64     }
65     public void onAttach(@NotNull Context context) {
66         super.onAttach(context);
67         debug("flow", "AccountSummaryFragment.onAttach()");
68     }
69     @Override
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);
74         return b.getRoot();
75     }
76     @Override
77     public SwipeRefreshLayout getRefreshLayout() {
78         return b.accountSwipeRefreshLayout;
79     }
80     @Override
81     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
82         debug("flow", "AccountSummaryFragment.onActivityCreated()");
83         super.onViewCreated(view, savedInstanceState);
84
85         MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
86
87         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
88                 this::onBackgroundTaskRunningChanged);
89
90         modelAdapter = new AccountSummaryAdapter();
91         MainActivity mainActivity = getMainActivity();
92
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);
100
101         mainActivity.fabShouldShow();
102
103         if (mainActivity instanceof FabManager.FabHandler)
104             FabManager.handle(mainActivity, b.accountRoot);
105
106         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
107         b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
108             debug("ui", "refreshing accounts via swipe");
109             model.scheduleTransactionListRetrieval();
110         });
111
112         Data.observeProfile(this, this::onProfileChanged);
113     }
114     private void onProfileChanged(Profile profile) {
115         if (profile == null)
116             return;
117
118         DB.get()
119           .getAccountDAO()
120           .getAllWithAmounts(profile.getId())
121           .observe(getViewLifecycleOwner(), list -> GeneralBackgroundTasks.run(() -> {
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);
130                   if (parent != null)
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);
136               }
137               modelAdapter.setAccounts(adapterList);
138               Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
139           }));
140     }
141 }