]> git.ktnx.net Git - mobile-ledger.git/commitdiff
rework transaction retrieval without MainActivity reference
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 26 Aug 2020 20:03:05 +0000 (20:03 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 26 Aug 2020 20:03:05 +0000 (20:03 +0000)
use a public LiveData to which interested parties can subscribe

app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.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 043dcce431ac2e25ff267a64134e75d48f9d150d..288301f0771cf71b3f81f296ffb4f06f9da2a5c8 100644 (file)
@@ -36,14 +36,12 @@ import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.lang.ref.WeakReference;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URLDecoder;
@@ -72,15 +70,12 @@ public class RetrieveTransactionsTask
     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
     private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
     private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
-    private WeakReference<MainActivity> contextRef;
     // %3A is '='
     private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
     private Pattern reAccountValue = Pattern.compile(
             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
     private MobileLedgerProfile profile;
-    public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef,
-                                    @NonNull MobileLedgerProfile profile) {
-        this.contextRef = contextRef;
+    public RetrieveTransactionsTask(@NonNull MobileLedgerProfile profile) {
         this.profile = profile;
     }
     private static void L(String msg) {
@@ -119,14 +114,6 @@ public class RetrieveTransactionsTask
         Data.backgroundTaskProgress.postValue(values[0]);
     }
     @Override
-    protected void onPreExecute() {
-        super.onPreExecute();
-        MainActivity context = getContext();
-        if (context == null)
-            return;
-        context.onRetrieveStart();
-    }
-    @Override
     protected void onPostExecute(String error) {
         super.onPostExecute(error);
         Progress progress = new Progress();
@@ -549,9 +536,6 @@ public class RetrieveTransactionsTask
             Data.backgroundTaskFinished();
         }
     }
-    private MainActivity getContext() {
-        return contextRef.get();
-    }
     private void throwIfCancelled() {
         if (isCancelled())
             throw new OperationCanceledException(null);
@@ -601,6 +585,7 @@ public class RetrieveTransactionsTask
         protected void setTotal(int total) {
             this.total = total;
             state = ProgressState.RUNNING;
+            indeterminate = total == -1;
         }
         private void ensureState(ProgressState wanted) {
             if (state != wanted)
index aaaf4991878d580a716b5f3cc3326e67e6cb58b9..c538f57a2deb266ebf1f323c2d7993efd75d6d44 100644 (file)
@@ -29,7 +29,6 @@ import androidx.lifecycle.Observer;
 
 import net.ktnx.mobileledger.App;
 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;
@@ -37,7 +36,6 @@ import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.ObservableList;
 import net.ktnx.mobileledger.utils.SimpleDate;
 
-import java.lang.ref.WeakReference;
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -167,7 +165,7 @@ public final class Data {
         }
         return profile;
     }
-    public synchronized static void scheduleTransactionListRetrieval(MainActivity activity) {
+    public synchronized static void scheduleTransactionListRetrieval() {
         if (retrieveTransactionsTask != null) {
             Logger.debug("db", "Ignoring request for transaction retrieval - already active");
             return;
@@ -178,8 +176,7 @@ public final class Data {
             return;
         }
 
-        retrieveTransactionsTask =
-                new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue());
+        retrieveTransactionsTask = new RetrieveTransactionsTask(profile.getValue());
         Logger.debug("db", "Created a background transaction retrieval task");
 
         retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
index 174a4b2c775f0ed43fd25c3ac85bfbcfc0061800..63c7455df543f5ed45c9aae7d22e50da5d56ef3f 100644 (file)
@@ -93,7 +93,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
         refreshLayout.setOnRefreshListener(() -> {
             debug("ui", "refreshing accounts via swipe");
-            Data.scheduleTransactionListRetrieval(mainActivity);
+            Data.scheduleTransactionListRetrieval();
         });
 
         MobileLedgerProfile profile = Data.getProfile();
index 25fb12a2faf4b59423e1913ecb4bc3855480661c..91951306ea029ddb7cb1cd49fd212ac2c1e62a39 100644 (file)
@@ -174,6 +174,7 @@ public class MainActivity extends ProfileThemedActivity {
 
         Data.profiles.observe(this, this::onProfileListChanged);
         Data.backgroundTaskProgress.observe(this, this::onRetrieveProgress);
+        Data.backgroundTasksRunning.observe(this, this::onRetrieveRunningChanged);
 
         if (barDrawerToggle == null) {
             barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
@@ -331,9 +332,12 @@ public class MainActivity extends ProfileThemedActivity {
                         "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
                         lastUpdate.getTime() / 1000f, now / 1000f));
 
-            Data.scheduleTransactionListRetrieval(this);
+            scheduleDataRetrieval();
         }
     }
+    public void scheduleDataRetrieval() {
+        Data.scheduleTransactionListRetrieval();
+    }
     private void createShortcuts(List<MobileLedgerProfile> list) {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
             return;
@@ -570,23 +574,32 @@ public class MainActivity extends ProfileThemedActivity {
         Data.stopTransactionsRetrieval();
         bTransactionListCancelDownload.setEnabled(false);
     }
-    public void onRetrieveStart() {
-        ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
-        bTransactionListCancelDownload.setEnabled(true);
-        ColorStateList csl = Colors.getColorStateList();
-        progressBar.setIndeterminateTintList(csl);
-        progressBar.setProgressTintList(csl);
-        progressBar.setIndeterminate(true);
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
-            progressBar.setProgress(0, false);
-        else
-            progressBar.setProgress(0);
-        findViewById(R.id.transaction_progress_layout).setVisibility(View.VISIBLE);
+    public void onRetrieveRunningChanged(Boolean running) {
+        final View progressLayout = findViewById(R.id.transaction_progress_layout);
+        if (running) {
+            ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
+            bTransactionListCancelDownload.setEnabled(true);
+            ColorStateList csl = Colors.getColorStateList();
+            progressBar.setIndeterminateTintList(csl);
+            progressBar.setProgressTintList(csl);
+            progressBar.setIndeterminate(true);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                progressBar.setProgress(0, false);
+            }
+            else {
+                progressBar.setProgress(0);
+            }
+            progressLayout.setVisibility(View.VISIBLE);
+        }
+        else {
+            progressLayout.setVisibility(View.GONE);
+        }
     }
     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
         ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
 
         if (progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED) {
+            Logger.debug("progress", "Done");
             findViewById(R.id.transaction_progress_layout).setVisibility(View.GONE);
 
             Data.transactionRetrievalDone();
@@ -607,22 +620,25 @@ public class MainActivity extends ProfileThemedActivity {
 
 
         bTransactionListCancelDownload.setEnabled(true);
-        ColorStateList csl = Colors.getColorStateList();
-        progressBar.setIndeterminateTintList(csl);
-        progressBar.setProgressTintList(csl);
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
-            progressBar.setProgress(0, false);
-        else
-            progressBar.setProgress(0);
+//        ColorStateList csl = Colors.getColorStateList();
+//        progressBar.setIndeterminateTintList(csl);
+//        progressBar.setProgressTintList(csl);
+//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+//            progressBar.setProgress(0, false);
+//        else
+//            progressBar.setProgress(0);
         findViewById(R.id.transaction_progress_layout).setVisibility(View.VISIBLE);
 
-        if (progress.isIndeterminate() || (progress.getTotal() == 0)) {
+        if (progress.isIndeterminate() || (progress.getTotal() <= 0)) {
             progressBar.setIndeterminate(true);
+            Logger.debug("progress", "indeterminate");
         }
         else {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                 progressBar.setMin(0);
             }
+            Logger.debug("progress",
+                    String.format(Locale.US, "%d/%d", progress.getProgress(), progress.getTotal()));
             progressBar.setMax(progress.getTotal());
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                 progressBar.setProgress(progress.getProgress(), true);
index fd13bc60ee70a825227ec47495df64baf0f9b1fa..e936f2f4ec4dcdc7153224b625481ad9faf35024 100644 (file)
@@ -118,7 +118,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
 
         refreshLayout.setOnRefreshListener(() -> {
             debug("ui", "refreshing transactions via swipe");
-            Data.scheduleTransactionListRetrieval(mainActivity);
+            mainActivity.scheduleDataRetrieval();
         });
 
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);