ソースを参照

chore: rename dialog commits on enter key

beautusg 7 年 前
コミット
7d1d2cd029

+ 20 - 4
app/src/main/java/com/sduduzog/slimlauncher/dialogs/RenameAppDialog.kt

@@ -4,6 +4,7 @@ import android.app.Dialog
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.widget.EditText
+import android.widget.Toast
 import androidx.appcompat.app.AlertDialog
 import androidx.fragment.app.DialogFragment
 import com.sduduzog.slimlauncher.R
@@ -17,19 +18,34 @@ class RenameAppDialog : DialogFragment() {
     private lateinit var model: MainViewModel
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
-        val view = LayoutInflater.from(context).inflate(R.layout.settings_rename_app, customise_apps_fragment)
-        val editText = view.findViewById<EditText>(R.id.rename_editText)
+        val view = LayoutInflater.from(context).inflate(R.layout.rename_app_dialog_edit_text, customise_apps_fragment, false)
+        val editText: EditText = view.findViewById(R.id.rename_editText)
         editText.text.append(app.appName)
         val builder = AlertDialog.Builder(context!!)
         builder.setTitle("Rename ${app.appName}")
         builder.setView(view)
         builder.setPositiveButton("DONE") { _, _ ->
-            app.appName = editText.text.toString()
-            model.update(app)
+            val name = editText.text.toString()
+            updateApp(name)
+        }
+        editText.setOnEditorActionListener { v, _, _ ->
+            val name = v.text.toString()
+            updateApp(name)
+            this@RenameAppDialog.dismiss()
+            true
         }
         return builder.create()
     }
 
+    private fun updateApp(newName: String) {
+        if (newName.isNotEmpty()) {
+            app.appName = newName
+            model.update(app)
+        } else {
+            Toast.makeText(context, "App name shouldn't be empty", Toast.LENGTH_LONG).show()
+        }
+    }
+
     companion object {
         fun rename(app: HomeApp, model: MainViewModel): RenameAppDialog {
             return RenameAppDialog().apply {

+ 2 - 1
app/src/main/res/layout/add_app_fragment.xml

@@ -23,7 +23,8 @@
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent"
-        tools:ignore="Autofill,LabelFor" />
+        tools:ignore="Autofill,LabelFor"
+        android:hint="Search apps" />
 
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/add_app_fragment_list"

+ 2 - 1
app/src/main/res/layout/settings_rename_app.xml → app/src/main/res/layout/rename_app_dialog_edit_text.xml

@@ -14,6 +14,7 @@
         android:layout_marginEnd="20dp"
         android:layout_marginBottom="0dp"
         android:ems="10"
-        android:inputType="textPersonName"
+        android:imeOptions="actionDone"
+        android:inputType="text|textCapWords"
         tools:ignore="Autofill,LabelFor" />
 </LinearLayout>