+ // Get a cursor that does not show the domain to be deleted.
+ Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
+
+ // Setup the domains pending delete cursor adapter.
+ CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Get the domain name string.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+
+ // Get a handle for the domain name text view.
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+
+ // Display the domain name.
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the handle for the current domains list view.
+ domainsListView = findViewById(R.id.domains_listview);
+
+ // Update the list view.
+ domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
+
+ // 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.
+ // Create an arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+
+ // Store the domains settings in the arguments bundle.
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+ argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
+
+ // Instantiate a new domain settings fragment.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+
+ // Add the arguments bundle to the domain settings fragment.
+ 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 the domains cursor adapter.
+ CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ /// Get the domain name string.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+
+ // Get a handle for the domain name text view.
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+
+ // Display the domain name.
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the domains list view.
+ domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
+
+ // Select the previously deleted domain in the list view.
+ domainsListView.setItemChecked(deletedDomainPosition, true);
+
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
+
+ // Enable the options delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Get the current theme status.
+ int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
+ // Set the delete menu item icon according to the theme.
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ deleteMenuItem.setIcon(R.drawable.delete_night);
+ } else {
+ deleteMenuItem.setIcon(R.drawable.delete_day);
+ }
+ } else { // The device in in one-paned mode.
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable the delete menu item.
+ deleteMenuItem.setVisible(true);
+
+ // Display the domain settings fragment.
+ 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 the delete menu item according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Get the current theme status.
+ int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
+ // Set the delete menu item icon according to the theme.
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ deleteMenuItem.setIcon(R.drawable.delete_night);
+ } else {
+ deleteMenuItem.setIcon(R.drawable.delete_day);
+ }
+ } else { // Single-paned mode.
+ // Show the delete menu item.
+ deleteMenuItem.setVisible(true);
+ }
+
+ // Reset the dismissing snackbar tracker.
+ 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();
+ }