diff --git a/app/src/main/java/dev/lowrespalmtree/comet/InputDialog.kt b/app/src/main/java/dev/lowrespalmtree/comet/InputDialog.kt new file mode 100644 index 0000000..3bdbba9 --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/InputDialog.kt @@ -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() + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageFragment.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageFragment.kt index 30863f4..d9590b4 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageFragment.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageFragment.kt @@ -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) { diff --git a/app/src/main/res/layout/dialog_input.xml b/app/src/main/res/layout/dialog_input.xml new file mode 100644 index 0000000..af768ff --- /dev/null +++ b/app/src/main/res/layout/dialog_input.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file