From aa4c87766cd8ba1c16ff39d16e34881f0430d52c Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 22 Dec 2018 08:02:02 +0000 Subject: [PATCH] cancelable transaction refresh --- .../mobileledger/TransactionListActivity.java | 22 +++++++--- .../async/RetrieveTransactionsTask.java | 7 +++ .../res/layout/transaction_list_fragment.xml | 43 +++++++++++++------ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java index 1a74eaad..7dff1a11 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java @@ -29,6 +29,7 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; +import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; @@ -46,8 +47,10 @@ public class TransactionListActivity extends AppCompatActivity { private SwipeRefreshLayout swiper; private RecyclerView root; private ProgressBar progressBar; + private LinearLayout progressLayout; private TextView tvLastUpdate; private TransactionListAdapter modelAdapter; + private RetrieveTransactionsTask retrieveTransactionsTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -62,9 +65,12 @@ public class TransactionListActivity extends AppCompatActivity { if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout"); root = findViewById(R.id.transaction_root); if (root == null) throw new RuntimeException("Can't get hold on the transaction list view"); - progressBar = findViewById(R.id.transaction_progress_bar); + progressBar = findViewById(R.id.transaction_list_progress_bar); if (progressBar == null) throw new RuntimeException("Can't get hold on the transaction list progress bar"); + progressLayout = findViewById(R.id.transaction_progress_layout); + if (progressLayout == null) throw new RuntimeException( + "Can't get hold on the transaction list progress bar layout"); tvLastUpdate = findViewById(R.id.transactions_last_update); updateLastUpdateText(); model = ViewModelProviders.of(this).get(TransactionListViewModel.class); @@ -106,19 +112,20 @@ public class TransactionListActivity extends AppCompatActivity { overridePendingTransition(R.anim.dummy, R.anim.slide_out_right); } private void update_transactions() { - RetrieveTransactionsTask task = new RetrieveTransactionsTask(new WeakReference<>(this)); + retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this)); RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params( PreferenceManager.getDefaultSharedPreferences(this)); - task.execute(params); + retrieveTransactionsTask.execute(params); + findViewById(R.id.transaction_list_cancel_download).setEnabled(true); } public void onRetrieveStart() { progressBar.setIndeterminate(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false); else progressBar.setProgress(0); - progressBar.setVisibility(View.VISIBLE); + progressLayout.setVisibility(View.VISIBLE); } public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) { if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) || @@ -140,7 +147,7 @@ public class TransactionListActivity extends AppCompatActivity { } public void onRetrieveDone(boolean success) { - progressBar.setVisibility(View.INVISIBLE); + progressLayout.setVisibility(View.GONE); swiper.setRefreshing(false); updateLastUpdateText(); if (success) { @@ -167,4 +174,9 @@ public class TransactionListActivity extends AppCompatActivity { } } } + public void onStopTransactionRefreshClick(View view) { + Log.d("interactive", "Cancelling transactions refresh"); + if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false); + findViewById(R.id.transaction_list_cancel_download).setEnabled(false); + } } 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 8902d455..701ec506 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -82,6 +82,13 @@ public class RetrieveTransactionsTask extends if (context == null) return; context.onRetrieveDone(success); } + @Override + protected void onCancelled() { + super.onCancelled(); + TransactionListActivity context = getContext(); + if (context == null) return; + context.onRetrieveDone(false); + } @SuppressLint("DefaultLocale") @Override protected Void doInBackground(Params... params) { diff --git a/app/src/main/res/layout/transaction_list_fragment.xml b/app/src/main/res/layout/transaction_list_fragment.xml index 4d3ceb6f..35bd5c80 100644 --- a/app/src/main/res/layout/transaction_list_fragment.xml +++ b/app/src/main/res/layout/transaction_list_fragment.xml @@ -58,20 +58,37 @@ tools:ignore="HardcodedText" /> - + android:layout_height="match_parent" + android:gravity="center_vertical" + android:orientation="horizontal" + android:visibility="gone"> + + + + + -- 2.39.5