From 60c444f557ab404619a765279f4089402140aef4 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 26 Aug 2020 20:03:05 +0000 Subject: [PATCH] rework transaction retrieval without MainActivity reference use a public LiveData to which interested parties can subscribe --- .../async/RetrieveTransactionsTask.java | 19 +----- .../net/ktnx/mobileledger/model/Data.java | 7 +-- .../AccountSummaryFragment.java | 2 +- .../ui/activity/MainActivity.java | 58 ++++++++++++------- .../TransactionListFragment.java | 2 +- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 043dcce4..288301f0 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -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 contextRef; // %3A is '=' private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\""); private Pattern reAccountValue = Pattern.compile( "\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?"); private MobileLedgerProfile profile; - public RetrieveTransactionsTask(WeakReference 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) 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 aaaf4991..c538f57a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -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); 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 174a4b2c..63c7455d 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 @@ -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(); 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 25fb12a2..91951306 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 @@ -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 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); 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 fd13bc60..e936f2f4 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 @@ -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); -- 2.39.2