]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDatabaseViewDialog.kt
Save and restore the app state. https://redmine.stoutner.com/issues/461
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / EditBookmarkDatabaseViewDialog.kt
index 57f764be844710cfcf724a9500c808864e51d469..a3c164521bdf22f98b72b80eab8b92f1a467c406 100644 (file)
@@ -19,7 +19,6 @@
 package com.stoutner.privacybrowser.dialogs
 
 import android.annotation.SuppressLint
-import android.app.AlertDialog
 import android.app.Dialog
 import android.content.Context
 import android.content.DialogInterface
@@ -37,6 +36,7 @@ import android.view.WindowManager
 import android.widget.*
 import android.widget.AdapterView.OnItemSelectedListener
 
+import androidx.appcompat.app.AlertDialog
 import androidx.core.content.ContextCompat
 import androidx.fragment.app.DialogFragment
 import androidx.preference.PreferenceManager
@@ -127,19 +127,8 @@ class EditBookmarkDatabaseViewDialog: DialogFragment() {
         // Move the cursor to the first position.
         bookmarkCursor.moveToFirst()
 
-        // Get a handle for the shared preferences.
-        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
-
-        // Get the screenshot and theme preferences.
-        val allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false)
-        val darkTheme = sharedPreferences.getBoolean("dark_theme", false)
-
         // Use an alert dialog builder to create the dialog and set the style according to the theme.
-        val dialogBuilder = if (darkTheme) {
-            AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialogDark)
-        } else {
-            AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialogLight)
-        }
+        val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
 
         // Set the title.
         dialogBuilder.setTitle(R.string.edit_bookmark)
@@ -159,6 +148,12 @@ class EditBookmarkDatabaseViewDialog: DialogFragment() {
         // Create an alert dialog from the alert dialog builder.
         val alertDialog = dialogBuilder.create()
 
+        // Get a handle for the shared preferences.
+        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+
+        // Get the screenshot preference.
+        val allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false)
+
         // Disable screenshots if not allowed.
         if (!allowScreenshots) {
             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
@@ -168,15 +163,15 @@ class EditBookmarkDatabaseViewDialog: DialogFragment() {
         alertDialog.show()
 
         // Get handles for the layout items.
-        val databaseIdTextView = alertDialog.findViewById<TextView>(R.id.edit_bookmark_database_id_textview)
-        val iconRadioGroup = alertDialog.findViewById<RadioGroup>(R.id.edit_bookmark_icon_radiogroup)
-        val currentIconImageView = alertDialog.findViewById<ImageView>(R.id.edit_bookmark_current_icon)
-        val newFavoriteIconImageView = alertDialog.findViewById<ImageView>(R.id.edit_bookmark_webpage_favorite_icon)
-        newIconRadioButton = alertDialog.findViewById(R.id.edit_bookmark_webpage_favorite_icon_radiobutton)
-        nameEditText = alertDialog.findViewById(R.id.edit_bookmark_name_edittext)
-        urlEditText = alertDialog.findViewById(R.id.edit_bookmark_url_edittext)
-        folderSpinner = alertDialog.findViewById(R.id.edit_bookmark_folder_spinner)
-        displayOrderEditText = alertDialog.findViewById(R.id.edit_bookmark_display_order_edittext)
+        val databaseIdTextView = alertDialog.findViewById<TextView>(R.id.edit_bookmark_database_id_textview)!!
+        val iconRadioGroup = alertDialog.findViewById<RadioGroup>(R.id.edit_bookmark_icon_radiogroup)!!
+        val currentIconImageView = alertDialog.findViewById<ImageView>(R.id.edit_bookmark_current_icon)!!
+        val newFavoriteIconImageView = alertDialog.findViewById<ImageView>(R.id.edit_bookmark_webpage_favorite_icon)!!
+        newIconRadioButton = alertDialog.findViewById(R.id.edit_bookmark_webpage_favorite_icon_radiobutton)!!
+        nameEditText = alertDialog.findViewById(R.id.edit_bookmark_name_edittext)!!
+        urlEditText = alertDialog.findViewById(R.id.edit_bookmark_url_edittext)!!
+        folderSpinner = alertDialog.findViewById(R.id.edit_bookmark_folder_spinner)!!
+        displayOrderEditText = alertDialog.findViewById(R.id.edit_bookmark_display_order_edittext)!!
         editButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
 
         // Store the current bookmark values.
@@ -210,7 +205,7 @@ class EditBookmarkDatabaseViewDialog: DialogFragment() {
         val matrixCursor = MatrixCursor(matrixCursorColumnNamesArray)
 
         // Add `Home Folder` as the first entry in the matrix folder.
-        matrixCursor.addRow(arrayOf<Any>(BookmarksDatabaseViewActivity.HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)))
+        matrixCursor.addRow(arrayOf(BookmarksDatabaseViewActivity.HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)))
 
         // Get a cursor with the list of all the folders.
         val foldersCursor = bookmarksDatabaseHelper.allFolders
@@ -326,15 +321,18 @@ class EditBookmarkDatabaseViewDialog: DialogFragment() {
             }
         })
 
-        // Update the edit button if the folder changes.
-        folderSpinner.onItemSelectedListener = object: OnItemSelectedListener {
-            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
-                // Update the edit button.
-                updateEditButton(currentBookmarkName, currentUrl, currentFolderDatabaseId, currentDisplayOrder)
-            }
+        // Wait to set the on item selected listener until the spinner has been inflated.  Otherwise the dialog will crash on restart.
+        folderSpinner.post {
+            // Update the edit button if the folder changes.
+            folderSpinner.onItemSelectedListener = object : OnItemSelectedListener {
+                override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
+                    // Update the edit button.
+                    updateEditButton(currentBookmarkName, currentUrl, currentFolderDatabaseId, currentDisplayOrder)
+                }
 
-            override fun onNothingSelected(parent: AdapterView<*>?) {
-                // Do nothing.
+                override fun onNothingSelected(parent: AdapterView<*>?) {
+                    // Do nothing.
+                }
             }
         }