]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Wait to enable the delete icon until the previous domain has been deleted. Fixes...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.java
index a26976636b6f58b53b179d7b5221e6e2ba0c7e1b..59c5fb9ff6d4b30bcdabea988b905757c937c453 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;
 
@@ -139,8 +146,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         // Only display `deleteMenuItem` (initially) in two-paned mode.
         deleteMenuItem.setVisible(twoPanedMode);
 
-        // Populate the list of domains.  We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs the `deleteMenuItem` to be inflated.
-        populateDomainsListView();
+        // Populate the list of domains.  We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs the `deleteMenuItem` to be inflated.  `-1` highlights the first domain.
+        populateDomainsListView(-1);
 
         // Success!
         return true;
@@ -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();
                     }
 
@@ -170,8 +177,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
                     supportFragmentManager.executePendingTransactions();
 
-                    // Populate the list of domains.
-                    populateDomainsListView();
+                    // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
+                    populateDomainsListView(-1);
 
                     // Update `domainSettingsFragmentDisplayed`.
                     domainSettingsFragmentDisplayed = false;
@@ -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();
             }
 
@@ -354,8 +393,8 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
             supportFragmentManager.executePendingTransactions();
 
-            // Populate the list of domains.
-            populateDomainsListView();
+            // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
+            populateDomainsListView(-1);
 
             // Update `domainSettingsFragmentDisplayed`.
             domainSettingsFragmentDisplayed = false;
@@ -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();
@@ -380,18 +424,10 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         // Create the domain and store the database ID in `currentDomainDatabaseId`.
         currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
 
-        // Add `currentDomainDatabaseId` to `argumentsBundle`.
-        Bundle argumentsBundle = new Bundle();
-        argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
-
-        // Add `argumentsBundle` to `domainSettingsFragment`.
-        DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
-        domainSettingsFragment.setArguments(argumentsBundle);
-
         // Display the newly created domain.
-        if (twoPanedMode) {
-
-        } else {
+        if (twoPanedMode) {  // The device in in two-paned mode.
+            populateDomainsListView(currentDomainDatabaseId);
+        } else {  // The device is in single-paned mode.
             // Hide `add_domain_fab`.
             addDomainFAB.setVisibility(View.GONE);
 
@@ -401,6 +437,14 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             // Set `domainSettingsFragmentDisplayed`.
             DomainsActivity.domainSettingsFragmentDisplayed = true;
 
+            // Add `currentDomainDatabaseId` to `argumentsBundle`.
+            Bundle argumentsBundle = new Bundle();
+            argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
+
+            // Add `argumentsBundle` to `domainSettingsFragment`.
+            DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+            domainSettingsFragment.setArguments(argumentsBundle);
+
             // Display `domainSettingsFragment`.
             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
         }
@@ -445,7 +489,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 displayWebpageImagesInt);
     }
 
-    private void populateDomainsListView() {
+    private void populateDomainsListView(final int highlightedDomainDatabaseId) {
         // get a handle for the current `domains_listview`.
         domainsListView = (ListView) findViewById(R.id.domains_listview);
 
@@ -474,11 +518,28 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
 
         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
         if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) {  // Two-paned mode is enabled and there is at least one domain.
-            // Select the first domain.
-            domainsListView.setItemChecked(0, true);
+            // Initialize `highlightedDomainPosition`.
+            int highlightedDomainPosition = 0;
+
+            // Get the cursor position for the highlighted domain.
+            for (int i = 0; i < domainsCursor.getCount(); i++) {
+                // Move to position `i` in the cursor.
+                domainsCursor.moveToPosition(i);
+
+                // Get the database ID for this position.
+                int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
+
+                // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
+                if (highlightedDomainDatabaseId == currentDatabaseId) {
+                    highlightedDomainPosition = i;
+                }
+            }
+
+            // Select the highlighted domain.
+            domainsListView.setItemChecked(highlightedDomainPosition, true);
 
-            // Get the `databaseId` for the first domain.
-            domainsCursor.moveToPosition(0);
+            // Get the `databaseId` for the highlighted domain.
+            domainsCursor.moveToPosition(highlightedDomainPosition);
             currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
 
             // Store `databaseId` in `argumentsBundle`.