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.
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.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
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;
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;
42 import org.jetbrains.annotations.NotNull;
44 import java.util.List;
45 import java.util.Locale;
47 import static net.ktnx.mobileledger.utils.Logger.debug;
49 public class AccountSummaryFragment extends MobileLedgerListFragment {
50 public AccountSummaryAdapter modelAdapter;
52 public void onCreate(@Nullable Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 debug("flow", "AccountSummaryFragment.onCreate()");
55 setHasOptionsMenu(true);
57 public void onAttach(@NotNull Context context) {
58 super.onAttach(context);
59 debug("flow", "AccountSummaryFragment.onAttach()");
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);
69 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
70 debug("flow", "AccountSummaryFragment.onActivityCreated()");
71 super.onViewCreated(view, savedInstanceState);
73 MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
75 Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
76 this::onBackgroundTaskRunningChanged);
78 modelAdapter = new AccountSummaryAdapter(model);
79 MainActivity mainActivity = getMainActivity();
81 root = view.findViewById(R.id.account_root);
82 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
83 llm.setOrientation(RecyclerView.VERTICAL);
84 root.setLayoutManager(llm);
85 root.setAdapter(modelAdapter);
86 DividerItemDecoration did =
87 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
88 root.addItemDecoration(did);
90 mainActivity.fabShouldShow();
94 refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout);
95 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
96 refreshLayout.setOnRefreshListener(() -> {
97 debug("ui", "refreshing accounts via swipe");
98 model.scheduleTransactionListRetrieval();
101 model.getDisplayedAccounts()
102 .observe(getViewLifecycleOwner(), this::onAccountsChanged);
104 private void onAccountsChanged(List<AccountListItem> accounts) {
105 Logger.debug("async-acc",
106 String.format(Locale.US, "fragment: got new account list (%d items)",
108 modelAdapter.setAccounts(accounts);