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;
// `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;
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();
}
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) {
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;
}
@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();
}
@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();
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;
// 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;