InputDialog: move to own class

main
dece 2 years ago
parent 9a91c72d1e
commit ea6f54cd73

@ -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()
}
}

@ -196,29 +196,14 @@ class PageFragment : Fragment(), PageAdapter.Listener {
}
private fun askForInput(prompt: String, uri: Uri) {
val editText = EditText(requireContext())
editText.inputType = InputType.TYPE_CLASS_TEXT
val inputView = FrameLayout(requireContext()).apply {
addView(FrameLayout(requireContext()).apply {
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)
}
.setOnDismissListener { updateState(PageViewModel.State.IDLE) }
.create()
.show()
InputDialog(requireContext(), prompt.ifEmpty { "Input required" })
.show(
onOk = { text ->
val newUri = uri.buildUpon().query(text).build()
openUrl(newUri.toString(), base = vm.currentUrl)
},
onDismiss = { updateState(PageViewModel.State.IDLE) }
)
}
private fun openUnknownScheme(uri: Uri) {

@ -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>
Loading…
Cancel
Save