Browse Source

ProfileDeleteFragment: Require confirmation via inputting profile name

Peter Cai 1 year ago
parent
commit
19c63113a1

+ 13 - 1
app-common/src/main/java/im/angry/openeuicc/ui/ProfileDeleteFragment.kt

@@ -2,7 +2,9 @@ package im.angry.openeuicc.ui
 
 
 import android.app.Dialog
 import android.app.Dialog
 import android.os.Bundle
 import android.os.Bundle
+import android.text.Editable
 import android.util.Log
 import android.util.Log
+import android.widget.EditText
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AlertDialog
 import androidx.fragment.app.DialogFragment
 import androidx.fragment.app.DialogFragment
 import androidx.lifecycle.lifecycleScope
 import androidx.lifecycle.lifecycleScope
@@ -26,11 +28,21 @@ class ProfileDeleteFragment : DialogFragment(), EuiccChannelFragmentMarker {
         }
         }
     }
     }
 
 
+    private val editText by lazy {
+        EditText(requireContext()).apply {
+            hint = Editable.Factory.getInstance().newEditable(
+                getString(R.string.profile_delete_confirm_input, requireArguments().getString("name")!!)
+            )
+        }
+    }
+    private val inputMatchesName: Boolean
+        get() = editText.text.toString() == requireArguments().getString("name")!!
     private var deleting = false
     private var deleting = false
 
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
         return AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme).apply {
         return AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme).apply {
             setMessage(getString(R.string.profile_delete_confirm, requireArguments().getString("name")))
             setMessage(getString(R.string.profile_delete_confirm, requireArguments().getString("name")))
+            setView(editText)
             setPositiveButton(android.R.string.ok, null) // Set listener to null to prevent auto closing
             setPositiveButton(android.R.string.ok, null) // Set listener to null to prevent auto closing
             setNegativeButton(android.R.string.cancel, null)
             setNegativeButton(android.R.string.cancel, null)
         }.create()
         }.create()
@@ -40,7 +52,7 @@ class ProfileDeleteFragment : DialogFragment(), EuiccChannelFragmentMarker {
         super.onResume()
         super.onResume()
         val alertDialog = dialog!! as AlertDialog
         val alertDialog = dialog!! as AlertDialog
         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
-            if (!deleting) delete()
+            if (!deleting && inputMatchesName) delete()
         }
         }
         alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
         alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
             if (!deleting) dismiss()
             if (!deleting) dismiss()

+ 1 - 0
app-common/src/main/res/values/strings.xml

@@ -35,6 +35,7 @@
     <string name="profile_rename_new_name">New nickname</string>
     <string name="profile_rename_new_name">New nickname</string>
 
 
     <string name="profile_delete_confirm">Are you sure you want to delete the profile %s? This operation is irreversible.</string>
     <string name="profile_delete_confirm">Are you sure you want to delete the profile %s? This operation is irreversible.</string>
+    <string name="profile_delete_confirm_input">Type \'%s\' here to confirm deletion</string>
 
 
     <string name="profile_notifications">Profile Notifications</string>
     <string name="profile_notifications">Profile Notifications</string>
     <string name="profile_notifications_show">Manage Notifications</string>
     <string name="profile_notifications_show">Manage Notifications</string>