From 8de7d02ce90569e36be421d89c44d3ef3f7378f6 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 17 Jul 2017 23:41:04 -0700 Subject: [PATCH] Wait to enable the delete icon until the previous domain has been deleted. Fixes https://redmine.stoutner.com/issues/173. --- .../activities/DomainsActivity.java | 58 ++++++++++++++++--- .../fragments/DomainsListFragment.java | 36 ++++++++---- 2 files changed, 75 insertions(+), 19 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 323cd833..59c5fb9f 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java @@ -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(); diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java index 02f2dfa8..36bc9b74 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java @@ -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; -- 2.45.2