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