]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
separate FAB management in a helper class
[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.FabManager;
37 import net.ktnx.mobileledger.ui.MainModel;
38 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
39 import net.ktnx.mobileledger.ui.activity.MainActivity;
40 import net.ktnx.mobileledger.utils.Colors;
41 import net.ktnx.mobileledger.utils.Logger;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import java.util.List;
46 import java.util.Locale;
47
48 import static net.ktnx.mobileledger.utils.Logger.debug;
49
50 public class AccountSummaryFragment extends MobileLedgerListFragment {
51     public AccountSummaryAdapter modelAdapter;
52     @Override
53     public void onCreate(@Nullable Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         debug("flow", "AccountSummaryFragment.onCreate()");
56         setHasOptionsMenu(true);
57     }
58     public void onAttach(@NotNull Context context) {
59         super.onAttach(context);
60         debug("flow", "AccountSummaryFragment.onAttach()");
61     }
62     @Override
63     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
64                              @Nullable Bundle savedInstanceState) {
65         debug("flow", "AccountSummaryFragment.onCreateView()");
66         return inflater.inflate(R.layout.account_summary_fragment, container, false);
67     }
68
69     @Override
70     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
71         debug("flow", "AccountSummaryFragment.onActivityCreated()");
72         super.onViewCreated(view, 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 = view.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         if (mainActivity instanceof FabManager.FabHandler)
94             FabManager.handle((FabManager.FabHandler) mainActivity, root);
95
96         refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout);
97         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
98         refreshLayout.setOnRefreshListener(() -> {
99             debug("ui", "refreshing accounts via swipe");
100             model.scheduleTransactionListRetrieval();
101         });
102
103         model.getDisplayedAccounts()
104              .observe(getViewLifecycleOwner(), this::onAccountsChanged);
105     }
106     private void onAccountsChanged(List<AccountListItem> accounts) {
107         Logger.debug("async-acc",
108                 String.format(Locale.US, "fragment: got new account list (%d items)",
109                         accounts.size()));
110         modelAdapter.setAccounts(accounts);
111     }
112 }