]> git.ktnx.net Git - mobile-ledger.git/commitdiff
move initiation of back-end data retrieval to Data
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 3 May 2019 15:12:01 +0000 (18:12 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 3 May 2019 15:12:01 +0000 (18:12 +0300)
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index 5e08297e3752ca9e0576c9e6e9fd0aa6a75332dd..64804aeb3426dd93c43a4dfc1558f8f492759cfe 100644 (file)
@@ -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<String> 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;
+    }
 }
index 3f0a427a4ebca858bf471ed83837e04f4f6122fb..e63090366a36e1eeb8a4d74caea544100144bf40 100644 (file)
@@ -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<MobileLedgerProfile> 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) {
index aa5a2dfec277600f47e4b730c155b5f4534a8128..7ebf4e9161a409185475f63dcbff384682329e0e 100644 (file)
@@ -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);