From be3bfabca0462fc0c8ca7ab241da304b68971056 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 3 May 2019 18:12:01 +0300 Subject: [PATCH] move initiation of back-end data retrieval to Data --- .../net/ktnx/mobileledger/model/Data.java | 23 +++++++++++++++++++ .../ui/activity/MainActivity.java | 15 +++--------- .../TransactionListFragment.java | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 5e08297e..64804aeb 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -19,7 +19,10 @@ package net.ktnx.mobileledger.model; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import net.ktnx.mobileledger.async.RetrieveTransactionsTask; +import net.ktnx.mobileledger.ui.activity.MainActivity; import net.ktnx.mobileledger.utils.LockHolder; import net.ktnx.mobileledger.utils.Locker; import net.ktnx.mobileledger.utils.Logger; @@ -27,8 +30,10 @@ import net.ktnx.mobileledger.utils.MLDB; import net.ktnx.mobileledger.utils.ObservableList; import net.ktnx.mobileledger.utils.ObservableValue; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; @@ -49,6 +54,7 @@ public final class Data { public static MutableLiveData accountFilter = new MutableLiveData<>(); private static AtomicInteger backgroundTaskCount = new AtomicInteger(0); private static Locker profilesLocker = new Locker(); + private static RetrieveTransactionsTask retrieveTransactionsTask; public static void backgroundTaskStarted() { int cnt = backgroundTaskCount.incrementAndGet(); debug("data", @@ -124,4 +130,21 @@ public final class Data { } return profile; } + public synchronized static void scheduleTransactionListRetrieval(MainActivity activity) { + if (retrieveTransactionsTask != null) { + Logger.debug("db", "Ignoring request for transaction retrieval - already active"); + return; + } + retrieveTransactionsTask = + new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue()); + Logger.debug("db", "Created a background transaction retrieval task"); + + retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + public static synchronized void stopTransactionsRetrieval() { + if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false); + } + public static void transactionRetrievalDone() { + retrieveTransactionsTask = null; + } } 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 3f0a427a..e6309036 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 @@ -56,12 +56,10 @@ import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment; import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel; import net.ktnx.mobileledger.utils.Colors; import net.ktnx.mobileledger.utils.LockHolder; -import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.MLDB; import org.jetbrains.annotations.NotNull; -import java.lang.ref.WeakReference; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; @@ -91,7 +89,6 @@ public class MainActivity extends ProfileThemedActivity { private LinearLayout profileListContainer; private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel; private FragmentManager fragmentManager; - private RetrieveTransactionsTask retrieveTransactionsTask; private View bTransactionListCancelDownload; private ProgressBar progressBar; private LinearLayout progressLayout; @@ -312,7 +309,7 @@ public class MainActivity extends ProfileThemedActivity { "WEB data last fetched at %1.3f and now is %1.3f. re-fetching", lastUpdate.getTime() / 1000f, now / 1000f)); - scheduleTransactionListRetrieval(); + Data.scheduleTransactionListRetrieval(this); } } private void createShortcuts(List list) { @@ -534,19 +531,13 @@ public class MainActivity extends ProfileThemedActivity { Data.lastUpdateDate.postValue(new Date(last_update)); } } - public void scheduleTransactionListRetrieval() { - if (Data.profile.get() == null) return; - - retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this)); - - retrieveTransactionsTask.execute(); - } public void onStopTransactionRefreshClick(View view) { debug("interactive", "Cancelling transactions refresh"); - if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false); + Data.stopTransactionsRetrieval(); bTransactionListCancelDownload.setEnabled(false); } public void onRetrieveDone(String error) { + Data.transactionRetrievalDone(); progressLayout.setVisibility(View.GONE); if (error == null) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index aa5a2dfe..7ebf4e91 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -108,7 +108,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { swiper.setOnRefreshListener(() -> { debug("ui", "refreshing transactions via swipe"); - mActivity.scheduleTransactionListRetrieval(); + Data.scheduleTransactionListRetrieval(mActivity); }); Colors.themeWatch.observe(this, this::themeChanged); -- 2.39.5