+ 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) -> {
+ // Do nothing because everything will be handled by `onDismissed()` below.
+ })
+ .addCallback(new Snackbar.Callback() {
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ // Run commands based on the event.
+ if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { // The user pushed the `Undo` button.
+ // Store the database ID in arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+
+ // Add the arguments bundle to the domain settings fragment.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display the correct fragments.
+ if (twoPanedMode) { // The device in in two-paned mode.
+ // Get a `Cursor` with the current contents of the domains database.
+ Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
+
+ // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
+ CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout. `false` does not attach it to the root.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Set the domain name.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the `ListView`.
+ domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
+ // Select the previously deleted domain in `domainsListView`.
+ domainsListView.setItemChecked(deletedDomainPosition, true);
+
+ // Display `domainSettingsFragment`.
+ fragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
+
+ // Enable the options `MenuItems`.
+ deleteMenuItem.setEnabled(true);
+ deleteMenuItem.setIcon(R.drawable.delete_light);
+ } else { // The device in in one-paned mode.
+ // Display `domainSettingsFragment`.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable `deleteMenuItem`.
+ deleteMenuItem.setVisible(true);
+
+ // Display `domainSettingsFragment`.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+ }
+ } else { // The snackbar was dismissed without the undo button being pushed.
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
+
+ // 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 = () -> {
+ // Enable `deleteMenuItem` according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Get a handle for the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+
+ // Get the theme preferences.
+ boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+
+ // Set the delete icon according to the theme.
+ if (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;
+ };
+
+ // 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);
+ }
+ }
+ }
+ });
+
+ // Show the Snackbar.
+ undoDeleteSnackbar.show();