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;
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) {
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();
Data.backgroundTaskFinished();
}
}
- private MainActivity getContext() {
- return contextRef.get();
- }
private void throwIfCancelled() {
if (isCancelled())
throw new OperationCanceledException(null);
protected void setTotal(int total) {
this.total = total;
state = ProgressState.RUNNING;
+ indeterminate = total == -1;
}
private void ensureState(ProgressState wanted) {
if (state != wanted)
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;
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;
}
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;
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);
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,
"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;
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();
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);