]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
show the number of accounts in the account list head item
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
1 /*
2  * Copyright © 2020 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
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.model.AccountListItem;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.ui.MainModel;
37 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
38 import net.ktnx.mobileledger.ui.activity.MainActivity;
39 import net.ktnx.mobileledger.utils.Colors;
40 import net.ktnx.mobileledger.utils.Logger;
41
42 import org.jetbrains.annotations.NotNull;
43
44 import java.util.List;
45 import java.util.Locale;
46
47 import static net.ktnx.mobileledger.utils.Logger.debug;
48
49 public class AccountSummaryFragment extends MobileLedgerListFragment {
50     public AccountSummaryAdapter modelAdapter;
51     @Override
52     public void onCreate(@Nullable Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         debug("flow", "AccountSummaryFragment.onCreate()");
55         setHasOptionsMenu(true);
56     }
57     public void onAttach(@NotNull Context context) {
58         super.onAttach(context);
59         debug("flow", "AccountSummaryFragment.onAttach()");
60     }
61     @Override
62     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
63                              @Nullable Bundle savedInstanceState) {
64         debug("flow", "AccountSummaryFragment.onCreateView()");
65         return inflater.inflate(R.layout.account_summary_fragment, container, false);
66     }
67
68     @Override
69
70     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
71         debug("flow", "AccountSummaryFragment.onActivityCreated()");
72         super.onActivityCreated(savedInstanceState);
73
74         MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
75
76         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
77                 this::onBackgroundTaskRunningChanged);
78
79         modelAdapter = new AccountSummaryAdapter(model);
80         MainActivity mainActivity = getMainActivity();
81
82         root = mainActivity.findViewById(R.id.account_root);
83         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
84         llm.setOrientation(RecyclerView.VERTICAL);
85         root.setLayoutManager(llm);
86         root.setAdapter(modelAdapter);
87         DividerItemDecoration did =
88                 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
89         root.addItemDecoration(did);
90
91         mainActivity.fabShouldShow();
92
93         manageFabOnScroll();
94
95         refreshLayout = mainActivity.findViewById(R.id.account_swipe_refresh_layout);
96         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
97         refreshLayout.setOnRefreshListener(() -> {
98             debug("ui", "refreshing accounts via swipe");
99             model.scheduleTransactionListRetrieval();
100         });
101
102         model.getDisplayedAccounts()
103              .observe(getViewLifecycleOwner(), this::onAccountsChanged);
104     }
105     private void onAccountsChanged(List<AccountListItem> accounts) {
106         Logger.debug("async-acc",
107                 String.format(Locale.US, "fragment: got new account list (%d items)",
108                         accounts.size()));
109         modelAdapter.setAccounts(accounts);
110     }
111 }