+ override fun deleteAllDomainSettings() {
+ // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
+ closeOnBack = false
+
+ // Store a copy of the currently selected domain database ID because it could change while the snackbar is displayed.
+ val currentlySelectedDatabaseId = currentDomainDatabaseId
+
+ // Store the domains list view scroll position.
+ val domainsListViewFirstVisiblePosition = domainsListView!!.firstVisiblePosition
+
+ // Update the fragments and menu items.
+ if (twoPanedMode) { // Two-paned mode.
+ // Store the deleted domain position, which is needed if undo is selected in the snackbar.
+ deletedDomainPosition = domainsListView!!.checkedItemPosition
+
+ // Get a handle for the domain settings fragment.
+ val domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)!!
+
+ // Get a handle for the domain settings fragment view.
+ val domainSettingsFragmentView = domainSettingsFragment.requireView()
+
+ // Hide the domain settings fragment.
+ domainSettingsFragmentView.visibility = View.INVISIBLE
+
+ // Disable the delete menu item.
+ deleteMenuItemEnabled = false
+ }
+
+ // Disable the delete all menu item.
+ deleteAllMenuItemEnabled = false
+
+ // Invalidate the options menu.
+ invalidateMenu()
+
+ // Create an empty cursor
+ val emptyCursor = domainsDatabaseHelper.getCursorForId(-1)
+
+ // Populate the empty cursor adapter.
+ val emptyCursorAdapter = populateDomainsCursorAdapter(emptyCursor)
+
+ // Update the handle for the current domains list view.
+ domainsListView = findViewById(R.id.domains_listview)
+
+ // Update the list view.
+ domainsListView!!.adapter = emptyCursorAdapter
+
+ // Get a handle for the activity (used in an inner class below).
+ val activity: Activity = this
+
+ // Display a snackbar.
+ undoDeleteSnackbar = Snackbar.make(domainsListView!!, R.string.all_domains_deleted, Snackbar.LENGTH_LONG)
+ .setAction(R.string.undo) {} // Do nothing because everything will be handled by `onDismissed()` below.
+ .addCallback(object : Snackbar.Callback() {
+ override fun onDismissed(snackbar: Snackbar, event: Int) {
+ //Run commands based on the event.
+ if (event == DISMISS_EVENT_ACTION) { // The user pushed the `Undo` button.
+ // Get a cursor with the current contents of the domains database.
+ val undoDeleteDomainsCursor = domainsDatabaseHelper.domainNameCursorOrderedByDomain
+
+ // Populate the undo delete domains cursor adapter.
+ val undoDeleteDomainsCursorAdapter = populateDomainsCursorAdapter(undoDeleteDomainsCursor)
+
+ // Update the domains list view.
+ domainsListView!!.adapter = undoDeleteDomainsCursorAdapter
+
+ // Redisplay the domain settings in two-paned mode.
+ if (twoPanedMode) {
+ // Create an arguments bundle.
+ val argumentsBundle = Bundle()
+
+ // Store the domain settings in the arguments bundle.
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentlySelectedDatabaseId)
+ argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY)
+
+ // Instantiate a new domain settings fragment.
+ val domainSettingsFragment = DomainSettingsFragment()
+
+ // Add the arguments bundle to the domain settings fragment.
+ domainSettingsFragment.arguments = argumentsBundle
+
+ // Select the previously selected domain in the list view.
+ domainsListView!!.setItemChecked(deletedDomainPosition, true)
+
+ // Display the domain settings fragment.
+ supportFragmentManager.commitNow {
+ replace(R.id.domain_settings_fragment_container, domainSettingsFragment, DOMAIN_SETTINGS_FRAGMENT_TAG)
+ }
+
+ // Enable the delete menu item.
+ deleteMenuItemEnabled = true
+ }
+
+ // Restore the domains list view scroll position.
+ domainsListView!!.setSelection(domainsListViewFirstVisiblePosition)
+
+ // Enable the delete all menu item.
+ deleteAllMenuItemEnabled = true
+
+ // Update the options menu.
+ invalidateMenu()
+ } else { // The snackbar was dismissed without the undo button being pushed.
+ // Delete all the domains.
+ val rowsDeleted = domainsDatabaseHelper.deleteAllDomains()
+
+ // Reset the dismissing snackbar option.
+ // The rows deleted should always be greater than 0, but in all cases they should be greater than -1.
+ // This has the effect of tricking the compiler into waiting until after the delete finishes to reenable the delete menu item,
+ // because the compiler (probably) can't tell that the response will never be less than -1, so it doesn't compile out the delay.
+ if (rowsDeleted > -1) {
+ // Reset the dismissing snackbar tracker.
+ dismissingSnackbar = false
+ }
+
+ // Close the activity if back was pressed.
+ if (closeActivityAfterDismissingSnackbar)
+ NavUtils.navigateUpFromSameTask(activity)
+ }
+ }
+ })
+
+ // Show the Snackbar.
+ undoDeleteSnackbar!!.show()
+ }
+