From: Damyan Ivanov Date: Sat, 24 Apr 2021 18:15:18 +0000 (+0000) Subject: put conversion from DB objects to LedgerTransaction in a thread X-Git-Tag: v0.18.0~27 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=b7a79ded94e99d06567b6617c7caa926799dc90c;p=mobile-ledger.git put conversion from DB objects to LedgerTransaction in a thread seems to take a non-trivial amount of time --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index d9ac65b7..5ca25061 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -71,6 +71,7 @@ import net.ktnx.mobileledger.ui.templates.TemplatesActivity; import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment; import net.ktnx.mobileledger.utils.Colors; import net.ktnx.mobileledger.utils.Logger; +import net.ktnx.mobileledger.utils.Misc; import org.jetbrains.annotations.NotNull; @@ -91,6 +92,7 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa public static final String STATE_ACC_FILTER = "account_filter"; private static final boolean FAB_HIDDEN = false; private static final boolean FAB_SHOWN = true; + private ConverterThread converterThread = null; private SectionsPagerAdapter mSectionsPagerAdapter; private ProfilesRecyclerViewAdapter mProfileListAdapter; private int mCurrentPage; @@ -471,38 +473,40 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa } mainModel.getAccountFilter() - .observe(this, accFilter -> { - Logger.debug(TAG, "account filter changed, reloading transactions"); -// mainModel.scheduleTransactionListReload(); - LiveData> transactions = - new MutableLiveData<>(new ArrayList()); - if (profile != null) { - if (accFilter == null || accFilter.isEmpty()) { - transactions = DB.get() - .getTransactionDAO() - .getAllWithAccounts(profile.getId()); - } - else { - transactions = DB.get() - .getTransactionDAO() - .getAllWithAccountsFiltered(profile.getId(), - accFilter); - } - } - - transactions.observe(this, list -> { - TransactionAccumulator accumulator = new TransactionAccumulator(accFilter); - - for (TransactionWithAccounts tr : list) - accumulator.put(new LedgerTransaction(tr)); - - accumulator.publishResults(mainModel); - }); - }); + .observe(this, this::onAccountFilterChanged); mainModel.stopTransactionsRetrieval(); mainModel.clearTransactions(); } + private void onAccountFilterChanged(String accFilter) { + Logger.debug(TAG, "account filter changed, reloading transactions"); +// mainModel.scheduleTransactionListReload(); + LiveData> transactions = + new MutableLiveData<>(new ArrayList()); + if (profile != null) { + if (accFilter == null || accFilter.isEmpty()) { + transactions = DB.get() + .getTransactionDAO() + .getAllWithAccounts(profile.getId()); + } + else { + transactions = DB.get() + .getTransactionDAO() + .getAllWithAccountsFiltered(profile.getId(), accFilter); + } + } + + transactions.observe(this, list -> { + Logger.debug(TAG, + String.format(Locale.ROOT, "got transaction list from DB (%d transactions)", + list.size())); + + if (converterThread != null) + converterThread.interrupt(); + converterThread = new ConverterThread(mainModel, list, accFilter); + converterThread.start(); + }); + } private void profileThemeChanged() { // un-hook all observed LiveData Data.removeProfileObservers(this); @@ -756,4 +760,37 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa return 2; } } + + static private class ConverterThread extends Thread { + private final List list; + private final MainModel model; + private final String accFilter; + public ConverterThread(@NonNull MainModel model, + @NonNull List list, String accFilter) { + this.model = model; + this.list = list; + this.accFilter = accFilter; + } + @Override + public void run() { + TransactionAccumulator accumulator = new TransactionAccumulator(accFilter); + + for (TransactionWithAccounts tr : list) { + if (isInterrupted()) { + Logger.debug(TAG, "ConverterThread bailing out on interrupt"); + return; + } + accumulator.put(new LedgerTransaction(tr)); + } + + if (isInterrupted()) { + Logger.debug(TAG, "ConverterThread bailing out on interrupt"); + return; + } + + Logger.debug(TAG, "ConverterThread publishing results"); + + Misc.onMainThread(() -> accumulator.publishResults(model)); + } + } }