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);
70 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
71 debug("flow", "AccountSummaryFragment.onActivityCreated()");
72 super.onActivityCreated(savedInstanceState);
74 MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
76 Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
77 this::onBackgroundTaskRunningChanged);
79 modelAdapter = new AccountSummaryAdapter(model);
80 MainActivity mainActivity = getMainActivity();
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);
91 mainActivity.fabShouldShow();
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();
102 model.getDisplayedAccounts()
103 .observe(getViewLifecycleOwner(), this::onAccountsChanged);
105 private void onAccountsChanged(List<AccountListItem> accounts) {
106 Logger.debug("async-acc",
107 String.format(Locale.US, "fragment: got new account list (%d items)",
109 modelAdapter.setAccounts(accounts);