/*
- * Copyright © 2020 Damyan Ivanov.
+ * Copyright © 2021 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
+import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
-
-import net.ktnx.mobileledger.R;
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
+
+import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
+import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding;
+import net.ktnx.mobileledger.db.AccountWithAmounts;
+import net.ktnx.mobileledger.db.DB;
+import net.ktnx.mobileledger.db.Profile;
+import net.ktnx.mobileledger.model.AccountListItem;
import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.ui.FabManager;
+import net.ktnx.mobileledger.ui.MainModel;
import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
import net.ktnx.mobileledger.ui.activity.MainActivity;
import net.ktnx.mobileledger.utils.Colors;
import org.jetbrains.annotations.NotNull;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
import static net.ktnx.mobileledger.utils.Logger.debug;
public class AccountSummaryFragment extends MobileLedgerListFragment {
public AccountSummaryAdapter modelAdapter;
+ private AccountSummaryFragmentBinding b;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onAttach(@NotNull Context context) {
super.onAttach(context);
debug("flow", "AccountSummaryFragment.onAttach()");
- mActivity = (MainActivity) context;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
debug("flow", "AccountSummaryFragment.onCreateView()");
- return inflater.inflate(R.layout.account_summary_fragment, container, false);
+ b = AccountSummaryFragmentBinding.inflate(inflater, container, false);
+ return b.getRoot();
}
-
@Override
-
- public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+ public SwipeRefreshLayout getRefreshLayout() {
+ return b.accountSwipeRefreshLayout;
+ }
+ @Override
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
debug("flow", "AccountSummaryFragment.onActivityCreated()");
- super.onActivityCreated(savedInstanceState);
+ super.onViewCreated(view, savedInstanceState);
+
+ MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
this::onBackgroundTaskRunningChanged);
modelAdapter = new AccountSummaryAdapter();
+ MainActivity mainActivity = getMainActivity();
- root = mActivity.findViewById(R.id.account_root);
- LinearLayoutManager llm = new LinearLayoutManager(mActivity);
+ LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
llm.setOrientation(RecyclerView.VERTICAL);
- root.setLayoutManager(llm);
- root.setAdapter(modelAdapter);
+ b.accountRoot.setLayoutManager(llm);
+ b.accountRoot.setAdapter(modelAdapter);
DividerItemDecoration did =
- new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
- root.addItemDecoration(did);
+ new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
+ b.accountRoot.addItemDecoration(did);
- mActivity.fabShouldShow();
+ mainActivity.fabShouldShow();
- manageFabOnScroll();
+ if (mainActivity instanceof FabManager.FabHandler)
+ FabManager.handle(mainActivity, b.accountRoot);
- swiper = mActivity.findViewById(R.id.account_swiper);
Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
- swiper.setOnRefreshListener(() -> {
+ b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
debug("ui", "refreshing accounts via swipe");
- Data.scheduleTransactionListRetrieval(mActivity);
+ model.scheduleTransactionListRetrieval();
});
- Data.accounts.addObserver(
- (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
+ Data.observeProfile(this, this::onProfileChanged);
+ }
+ private void onProfileChanged(Profile profile) {
+ if (profile == null)
+ return;
+
+ DB.get()
+ .getAccountDAO()
+ .getAllWithAmounts(profile.getId())
+ .observe(getViewLifecycleOwner(), list -> GeneralBackgroundTasks.run(() -> {
+ List<AccountListItem> adapterList = new ArrayList<>();
+ adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
+ HashMap<String, LedgerAccount> accMap = new HashMap<>();
+ for (AccountWithAmounts dbAcc : list) {
+ LedgerAccount parent = null;
+ String parentName = dbAcc.account.getParentName();
+ if (parentName != null)
+ parent = accMap.get(parentName);
+ if (parent != null)
+ parent.setHasSubAccounts(true);
+ final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent);
+ if (account.isVisible())
+ adapterList.add(new AccountListItem.Account(account));
+ accMap.put(dbAcc.account.getName(), account);
+ }
+ modelAdapter.setAccounts(adapterList);
+ Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
+ }));
}
}