InputDialog: move to own class

This commit is contained in:
dece 2022-02-04 15:28:41 +01:00
parent 9a91c72d1e
commit ea6f54cd73
3 changed files with 45 additions and 23 deletions

View file

@ -0,0 +1,23 @@
package dev.lowrespalmtree.comet
import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import dev.lowrespalmtree.comet.databinding.DialogInputBinding
/** Generic text input dialog. Used for code 10 and a few other simple text input. */
class InputDialog(
private val context: Context,
private val prompt: String
) {
fun show(onOk: (text: String) -> Unit, onDismiss: () -> Unit) {
val binding = DialogInputBinding.inflate(LayoutInflater.from(context))
AlertDialog.Builder(context)
.setMessage(prompt)
.setView(binding.root)
.setPositiveButton(android.R.string.ok) { _, _ -> onOk(binding.textInput.text.toString()) }
.setOnDismissListener { onDismiss() }
.create()
.show()
}
}

View file

@ -196,29 +196,14 @@ class PageFragment : Fragment(), PageAdapter.Listener {
} }
private fun askForInput(prompt: String, uri: Uri) { private fun askForInput(prompt: String, uri: Uri) {
val editText = EditText(requireContext()) InputDialog(requireContext(), prompt.ifEmpty { "Input required" })
editText.inputType = InputType.TYPE_CLASS_TEXT .show(
val inputView = FrameLayout(requireContext()).apply { onOk = { text ->
addView(FrameLayout(requireContext()).apply { val newUri = uri.buildUpon().query(text).build()
addView(editText)
val params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
params.setMargins(resources.getDimensionPixelSize(R.dimen.text_margin))
layoutParams = params
})
}
AlertDialog.Builder(requireContext())
.setMessage(prompt.ifEmpty { "Input required" })
.setView(inputView)
.setPositiveButton(android.R.string.ok) { _, _ ->
val newUri = uri.buildUpon().query(editText.text.toString()).build()
openUrl(newUri.toString(), base = vm.currentUrl) openUrl(newUri.toString(), base = vm.currentUrl)
} },
.setOnDismissListener { updateState(PageViewModel.State.IDLE) } onDismiss = { updateState(PageViewModel.State.IDLE) }
.create() )
.show()
} }
private fun openUnknownScheme(uri: Uri) { private fun openUnknownScheme(uri: Uri) {

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="text" />
</LinearLayout>