Wait to enable the delete icon until the previous domain has been deleted. Fixes...
authorSoren Stoutner <soren@stoutner.com>
Tue, 18 Jul 2017 06:41:04 +0000 (23:41 -0700)
committerSoren Stoutner <soren@stoutner.com>
Tue, 18 Jul 2017 06:41:04 +0000 (23:41 -0700)
app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java

index 323cd83..59c5fb9 100644 (file)
@@ -22,6 +22,7 @@ package com.stoutner.privacybrowser.activities;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.os.Handler;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.FragmentManager;
@@ -60,6 +61,12 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
     public static MenuItem deleteMenuItem;
 
+    // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onOptionsItemSelected()`.
+    public static Snackbar undoDeleteSnackbar;
+
+    // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onOptionsItemSelected()`.
+    public static boolean dismissingSnackbar;
+
     // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
     private Context context;
 
@@ -154,8 +161,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         switch (menuItemID) {
             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
                 if (twoPanedMode) {  // The device is in two-paned mode.
-                    // If there is at least one domain, save the current domain settings.
-                    if (domainsListView.getCount() > 0) {
+                    // Save the current domain settings if the domain settings fragment is displayed.
+                    if (findViewById(R.id.domain_settings_scrollview) != null) {
                         saveDomainSettings();
                     }
 
@@ -245,7 +252,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
 
                 // Display a `Snackbar`.
-                Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
+                undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
                         .setAction(R.string.undo, new View.OnClickListener() {
                             @Override
                             public void onClick(View v) {
@@ -322,11 +329,43 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                                     default:
                                         // Delete the selected domain.
                                         domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
+
+                                        // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
+                                        if (DomainsActivity.dismissingSnackbar) {
+                                            // Create a `Runnable` to enable the delete menu item.
+                                            Runnable enableDeleteMenuItemRunnable = new Runnable() {
+                                                @Override
+                                                public void run() {
+                                                    // Enable `deleteMenuItem` according to the display mode.
+                                                    if (twoPanedMode) {  // Two-paned mode.
+                                                        // Enable `deleteMenuItem`.
+                                                        deleteMenuItem.setEnabled(true);
+
+                                                        // Set the delete icon according to the theme.
+                                                        if (MainWebViewActivity.darkTheme) {
+                                                            deleteMenuItem.setIcon(R.drawable.delete_dark);
+                                                        } else {
+                                                            deleteMenuItem.setIcon(R.drawable.delete_light);
+                                                        }
+                                                    } else {  // Single-paned mode.
+                                                        // Show `deleteMenuItem`.
+                                                        deleteMenuItem.setVisible(true);
+                                                    }
+
+                                                    // Reset `dismissingSnackbar`.
+                                                    dismissingSnackbar = false;
+                                                }
+                                            };
+
+                                            // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+                                            Handler handler = new Handler();
+                                            handler.postDelayed(enableDeleteMenuItemRunnable, 100);
+                                        }
                                         break;
                                 }
                             }
-                        })
-                        .show();
+                        });
+                undoDeleteSnackbar.show();
                 break;
         }
 
@@ -338,8 +377,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
     @Override
     public void onBackPressed() {
         if (twoPanedMode) {  // The device is in two-paned mode.
-            // If there is at least one domain, save the current domain settings.
-            if (domainsListView.getCount() > 0) {
+            // Save the current domain settings if the domain settings fragment is displayed.
+            if (findViewById(R.id.domain_settings_scrollview) != null) {
                 saveDomainSettings();
             }
 
@@ -373,6 +412,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
 
     @Override
     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
+        // Dismiss `undoDeleteSnackbar` if it is currently displayed.
+        if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
+            undoDeleteSnackbar.dismiss();
+        }
+
         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
         String domainNameString = domainNameEditText.getText().toString();
index 02f2dfa..36bc9b7 100644 (file)
@@ -55,7 +55,14 @@ public class DomainsListFragment extends Fragment {
         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                // Save the current domain settings if operating in two-paned mode a domain is currently selected.
+                // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted).
+                if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) {
+                    DomainsActivity.dismissingSnackbar = true;
+
+                    DomainsActivity.undoDeleteSnackbar.dismiss();
+                }
+
+                // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
                 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
                     View domainSettingsFragmentView = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container).getView();
                     assert domainSettingsFragmentView != null;
@@ -111,26 +118,31 @@ public class DomainsListFragment extends Fragment {
 
                 // Display the domain settings fragment.
                 if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
-                    // Enable the options `MenuItems`.
-                    DomainsActivity.deleteMenuItem.setEnabled(true);
-
-                    // Set the delete icon according to the theme.
-                    if (MainWebViewActivity.darkTheme) {
-                        DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
-                    } else {
-                        DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
+                    // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
+                    if (!DomainsActivity.dismissingSnackbar) {
+                        // Enable `deleteMenuItem`.
+                        DomainsActivity.deleteMenuItem.setEnabled(true);
+
+                        // Set the delete icon according to the theme.
+                        if (MainWebViewActivity.darkTheme) {
+                            DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
+                        } else {
+                            DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
+                        }
                     }
 
                     // Display `domainSettingsFragment`.
                     supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
                 } else { // The device in in single-paned mode
+                    // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
+                    if (!DomainsActivity.dismissingSnackbar) {
+                        DomainsActivity.deleteMenuItem.setVisible(true);
+                    }
+
                     // Hide `add_domain_fab`.
                     FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab);
                     addDomainFAB.setVisibility(View.GONE);
 
-                    // Show and enable `deleteMenuItem`.
-                    DomainsActivity.deleteMenuItem.setVisible(true);
-
                     // Set `domainSettingsFragmentDisplayed`.
                     DomainsActivity.domainSettingsFragmentDisplayed = true;