From df38abb63dacb2ec493d7906dbc4bc995152a467 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 30 Jul 2018 15:04:47 -0700 Subject: [PATCH] Refactor Domains delete code. https://redmine.stoutner.com/issues/316 --- .../activities/DomainsActivity.java | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java index a56e7184..cca88565 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java @@ -19,6 +19,7 @@ package com.stoutner.privacybrowser.activities; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; @@ -70,6 +71,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`. public static boolean dismissingSnackbar; + // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`. + private boolean closeActivityAfterDismissingSnackbar; + // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`. private Context context; @@ -284,17 +288,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Dismiss the undo delete `SnackBar` if it is shown. if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) { - undoDeleteSnackbar.dismiss(); - - // Create a `Runnable` to return to the main activity. - Runnable navigateHomeRunnable = () -> { - // Go home. - NavUtils.navigateUpFromSameTask(this); - }; + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database. - Handler handler = new Handler(); - handler.postDelayed(navigateHomeRunnable, 300); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Go home. NavUtils.navigateUpFromSameTask(this); @@ -325,17 +323,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo } else { // The device is in single-paned mode and `DomainsListFragment` is displayed. // Dismiss the undo delete `SnackBar` if it is shown. if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) { - undoDeleteSnackbar.dismiss(); + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // Create a `Runnable` to return to the main activity. - Runnable navigateHomeRunnable = () -> { - // Go home. - NavUtils.navigateUpFromSameTask(this); - }; - - // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database. - Handler handler = new Handler(); - handler.postDelayed(navigateHomeRunnable, 300); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Go home. NavUtils.navigateUpFromSameTask(this); @@ -400,6 +392,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Update the `ListView`. domainsListView.setAdapter(domainsPendingDeleteCursorAdapter); + // Get a handle for the activity. + Activity activity = this; + // Display a `Snackbar`. undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG) .setAction(R.string.undo, (View v) -> { @@ -473,7 +468,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Delete the selected domain. domainsDatabaseHelper.deleteDomain(databaseIdToDelete); - // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed. + // Enable the delete menu item if the system was waiting for a snackbar to be dismissed. if (dismissingSnackbar) { // Create a `Runnable` to enable the delete menu item. Runnable enableDeleteMenuItemRunnable = () -> { @@ -497,10 +492,17 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo dismissingSnackbar = false; }; - // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database. + // Enable the delete menu icon after 100 milliseconds to make sure that the previous domain has been deleted from the database. Handler handler = new Handler(); handler.postDelayed(enableDeleteMenuItemRunnable, 100); } + + // Close the activity if back was pressed. + if (closeActivityAfterDismissingSnackbar) { + // Go home. + NavUtils.navigateUpFromSameTask(activity); + } + break; } } @@ -542,14 +544,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Dismiss the undo delete SnackBar if it is shown. if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) { - undoDeleteSnackbar.dismiss(); - - // Create a runnable to return to the main activity. - Runnable navigateHomeRunnable = super::onBackPressed; + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database. - Handler handler = new Handler(); - handler.postDelayed(navigateHomeRunnable, 300); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Pass `onBackPressed()` to the system. super.onBackPressed(); @@ -580,14 +579,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo } else { // The device is in single-paned mode and the domain list fragment is displayed. // Dismiss the undo delete SnackBar if it is shown. if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) { - undoDeleteSnackbar.dismiss(); + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // Create a runnable to return to the main activity. - Runnable navigateHomeRunnable = super::onBackPressed; - - // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database. - Handler handler = new Handler(); - handler.postDelayed(navigateHomeRunnable, 300); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Pass `onBackPressed()` to the system. super.onBackPressed(); -- 2.45.2