Un-hack focus gain in folder creation dialog (#672)

The current way to let the folder name EditText in the folder creation
dialog gain focus is... slightly hacky.

This commit proposes a more conceptual solution, which, if it works
reliably, could be much easier to maintain.
This commit is contained in:
Fabian Henneke 2020-03-27 09:53:49 +01:00 committed by GitHub
parent b21c032f31
commit 0153d3215e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,11 +6,8 @@ package com.zeapo.pwdstore.ui.dialogs
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.view.inputmethod.InputMethodManager
import android.os.SystemClock
import android.view.MotionEvent
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
@ -20,16 +17,6 @@ import java.io.File
class FolderCreationDialogFragment : DialogFragment() { class FolderCreationDialogFragment : DialogFragment() {
override fun onResume() {
// TODO: We really, really should not need this amount of hackery. WTF is going on?
super.onResume()
val editText = dialog?.findViewById<TextInputEditText>(R.id.folder_name_text)
Handler().postDelayed(300) {
editText?.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0))
editText?.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0))
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext()) val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext())
alertDialogBuilder.setTitle(R.string.title_create_folder) alertDialogBuilder.setTitle(R.string.title_create_folder)
@ -40,7 +27,20 @@ class FolderCreationDialogFragment : DialogFragment() {
alertDialogBuilder.setNegativeButton(getString(android.R.string.cancel)) { _, _ -> alertDialogBuilder.setNegativeButton(getString(android.R.string.cancel)) { _, _ ->
dismiss() dismiss()
} }
return alertDialogBuilder.create() val dialog = alertDialogBuilder.create()
dialog.setOnShowListener {
// https://stackoverflow.com/a/13056259/297261
dialog.findViewById<TextInputEditText>(R.id.folder_name_text)!!.apply {
setOnFocusChangeListener { v, _ ->
v.post {
val imm = activity!!.getSystemService(InputMethodManager::class.java)
imm?.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT)
}
}
requestFocus()
}
}
return dialog
} }
private fun createDirectory(currentDir: String) { private fun createDirectory(currentDir: String) {