]> git.ktnx.net Git - mobile-ledger.git/commitdiff
cancelable transaction refresh
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 08:02:02 +0000 (08:02 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 08:02:02 +0000 (08:02 +0000)
app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java
app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/res/layout/transaction_list_fragment.xml

index 1a74eaad5031da20664c7aa72fb0a7b980a44532..7dff1a11c67b00131a3f28c37aeace000c604c7a 100644 (file)
@@ -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);
+    }
 }
index 8902d455a91bb02f591408a0dd5358118e8d0153..701ec506896d19b53be9576e84ed0ae45b588f9e 100644 (file)
@@ -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) {
index 4d3ceb6f439f23006ec48294da2735c3d92457a1..35bd5c801c9952960cafdf194d5421f66f2ad5f5 100644 (file)
                 tools:ignore="HardcodedText" />
         </LinearLayout>
 
-        <ProgressBar
-            android:id="@+id/transaction_progress_bar"
-            style="?android:attr/progressBarStyleHorizontal"
+        <LinearLayout
+            android:id="@+id/transaction_progress_layout"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="-8dp"
-            android:layout_marginBottom="-7dp"
-            android:indeterminate="true"
-            android:padding="0dp"
-            android:progressTint="@color/colorPrimary"
-            android:visibility="invisible"
-            app:layout_constraintEnd_toEndOf="parent"
-            app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintTop_toBottomOf="@+id/last_update_row" />
+            android:layout_height="match_parent"
+            android:gravity="center_vertical"
+            android:orientation="horizontal"
+            android:visibility="gone">
+
+            <ProgressBar
+                android:id="@+id/transaction_list_progress_bar"
+                style="?android:attr/progressBarStyleHorizontal"
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="-8dp"
+                android:layout_marginBottom="-7dp"
+                android:layout_weight="1"
+                android:indeterminate="true"
+                android:padding="0dp"
+                android:progressTint="@color/colorPrimary"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/last_update_row" />
+
+            <TextView
+                android:id="@+id/transaction_list_cancel_download"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:background="@drawable/ic_clear_black_24dp"
+                android:clickable="true"
+                android:onClick="onStopTransactionRefreshClick" />
+        </LinearLayout>
 
     </LinearLayout>