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