]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
remove hard reference from MainAvtivity to AccountSummaryFragment
[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.ui.MobileLedgerListFragment;
35 import net.ktnx.mobileledger.ui.activity.MainActivity;
36 import net.ktnx.mobileledger.utils.Colors;
37
38 import org.jetbrains.annotations.NotNull;
39
40 import static net.ktnx.mobileledger.utils.Logger.debug;
41
42 public class AccountSummaryFragment extends MobileLedgerListFragment {
43     public AccountSummaryAdapter modelAdapter;
44     @Override
45     public void onCreate(@Nullable Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         debug("flow", "AccountSummaryFragment.onCreate()");
48         setHasOptionsMenu(true);
49     }
50     public void onAttach(@NotNull Context context) {
51         super.onAttach(context);
52         debug("flow", "AccountSummaryFragment.onAttach()");
53         mActivity = (MainActivity) context;
54     }
55     @Override
56     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
57                              @Nullable Bundle savedInstanceState) {
58         debug("flow", "AccountSummaryFragment.onCreateView()");
59         return inflater.inflate(R.layout.account_summary_fragment, container, false);
60     }
61
62     @Override
63
64     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
65         debug("flow", "AccountSummaryFragment.onActivityCreated()");
66         super.onActivityCreated(savedInstanceState);
67
68         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
69                 this::onBackgroundTaskRunningChanged);
70
71         modelAdapter = new AccountSummaryAdapter();
72
73         root = mActivity.findViewById(R.id.account_root);
74         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
75         llm.setOrientation(RecyclerView.VERTICAL);
76         root.setLayoutManager(llm);
77         root.setAdapter(modelAdapter);
78         DividerItemDecoration did =
79                 new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
80         root.addItemDecoration(did);
81
82         mActivity.fabShouldShow();
83
84         manageFabOnScroll();
85
86         swiper = mActivity.findViewById(R.id.account_swiper);
87         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
88         swiper.setOnRefreshListener(() -> {
89             debug("ui", "refreshing accounts via swipe");
90             Data.scheduleTransactionListRetrieval(mActivity);
91         });
92
93         Data.accounts.addObserver(
94                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
95     }
96 }