X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Faccount_summary%2FAccountSummaryFragment.java;h=8ad6da6ff0fa7233613d1ffaa53fd694e99d805e;hb=faa01b22a0f23efe6302d4787c244bb4f241b8da;hp=bb484929d9f221236bde6b05209cc5b07f5c25d4;hpb=6ab7b1bb9b7b5ffbbcc21db1d1e467e3d9752b6e;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index bb484929..8ad6da6f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 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 @@ -18,48 +18,51 @@ package net.ktnx.mobileledger.ui.account_summary; import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import com.google.android.material.floatingactionbutton.FloatingActionButton; +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 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 androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.recyclerview.widget.DividerItemDecoration; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; +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 MenuItem mShowOnlyStarred; - private Menu optMenu; - */ - private FloatingActionButton fab; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); debug("flow", "AccountSummaryFragment.onCreate()"); setHasOptionsMenu(true); - - Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged); } 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, @@ -69,105 +72,66 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { } @Override - - public void onActivityCreated(@Nullable Bundle savedInstanceState) { + 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(); - mActivity.mAccountSummaryFragment = this; - root = mActivity.findViewById(R.id.account_root); - LinearLayoutManager llm = new LinearLayoutManager(mActivity); + root = view.findViewById(R.id.account_root); + LinearLayoutManager llm = new LinearLayoutManager(mainActivity); llm.setOrientation(RecyclerView.VERTICAL); root.setLayoutManager(llm); root.setAdapter(modelAdapter); DividerItemDecoration did = - new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL); + new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL); root.addItemDecoration(did); - fab = mActivity.findViewById(R.id.btn_add_transaction); + mainActivity.fabShouldShow(); - mActivity.fabShouldShow(); + if (mainActivity instanceof FabManager.FabHandler) + FabManager.handle(mainActivity, root); - manageFabOnScroll(); - - swiper = mActivity.findViewById(R.id.account_swiper); - Colors.themeWatch.observe(this, this::themeChanged); - swiper.setOnRefreshListener(() -> { + refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout); + Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged); + refreshLayout.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); } -/* - void stopSelection() { - modelAdapter.stopSelection(); - if (optMenu != null) { - optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false); - optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false); - optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true); - } - { - if (fab != null) fab.show(); - } - } - public void onCancelAccSelection(MenuItem item) { - stopSelection(); - } - public void onConfirmAccSelection(MenuItem item) { - AccountSummaryViewModel.commitSelections(mActivity); - stopSelection(); - } - @Override - public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) { - // Inflate the menu; this adds items to the action bar if it is present. - inflater.inflate(R.menu.account_summary, menu); - optMenu = menu; - - mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred); - if (mShowOnlyStarred == null) throw new AssertionError(); - MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection); - if (mCancelSelection == null) throw new AssertionError(); - MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection); - if (mConfirmSelection == null) throw new AssertionError(); - - Data.optShowOnlyStarred.addObserver((o, arg) -> { - boolean newValue = Data.optShowOnlyStarred.get(); - debug("pref", String.format("pref change came (%s)", newValue ? "true" : "false")); - mShowOnlyStarred.setChecked(newValue); - update_account_table(); - }); - - mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get()); - - debug("menu", "Accounts: onCreateOptionsMenu called"); - - mShowOnlyStarred.setOnMenuItemClickListener(item -> { - SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity); - SharedPreferences.Editor editor = pref.edit(); - boolean flag = item.isChecked(); - editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag); - debug("pref", - "Setting show only starred accounts pref to " + (flag ? "false" : "true")); - editor.apply(); - - return true; - }); - - mCancelSelection.setOnMenuItemClickListener(item -> { - stopSelection(); - return true; - }); - - mConfirmSelection.setOnMenuItemClickListener(item -> { - AccountSummaryViewModel.commitSelections(mActivity); - stopSelection(); - - return true; - }); + private void onProfileChanged(Profile profile) { + if (profile == null) + return; + + DB.get() + .getAccountDAO() + .getAllWithAmounts(profile.getId()) + .observe(getViewLifecycleOwner(), list -> AsyncTask.execute(() -> { + List adapterList = new ArrayList<>(); + adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText)); + HashMap 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); + })); } -*/ }