handle input responses

This commit is contained in:
dece 2022-01-10 00:36:15 +01:00
parent da3a9f4c75
commit 6eb9c142cc
3 changed files with 35 additions and 1 deletions

View file

@ -6,16 +6,23 @@ import android.content.Intent
import android.content.Intent.ACTION_VIEW
import android.net.Uri
import android.os.Bundle
import android.text.InputType
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.FrameLayout.LayoutParams.MATCH_PARENT
import android.widget.FrameLayout.LayoutParams.WRAP_CONTENT
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setMargins
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi
@ExperimentalCoroutinesApi
class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
private lateinit var binding: ActivityMainBinding
@ -130,6 +137,27 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
Log.d(TAG, "handleEvent: $event")
if (!event.handled) {
when (event) {
is PageViewModel.InputEvent -> {
val editText = EditText(this).apply { inputType = InputType.TYPE_CLASS_TEXT }
val inputView = FrameLayout(this).apply {
addView(FrameLayout(this@MainActivity).apply {
addView(editText)
val params = FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
params.setMargins(resources.getDimensionPixelSize(R.dimen.text_margin))
layoutParams = params
})
}
AlertDialog.Builder(this)
.setMessage(if (event.prompt.isNotEmpty()) event.prompt else "Input required")
.setView(inputView)
.setPositiveButton(android.R.string.ok) { _, _ ->
val newUri = event.uri.buildUpon().query(editText.text.toString()).build()
openUrl(newUri.toString(), base = currentUrl)
}
.setOnDismissListener { updateState(PageViewModel.State.IDLE) }
.create()
.show()
}
is PageViewModel.SuccessEvent -> {
currentUrl = event.uri
visitedUrls.add(event.uri)

View file

@ -25,6 +25,7 @@ class PageViewModel : ViewModel() {
}
abstract class Event(var handled: Boolean = false)
data class InputEvent(val uri: Uri, val prompt: String) : Event()
data class SuccessEvent(val uri: String) : Event()
data class RedirectEvent(val uri: String, val redirects: Int) : Event()
data class FailureEvent(
@ -73,6 +74,8 @@ class PageViewModel : ViewModel() {
Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"")
when (response.code.getCategory()) {
Response.Code.Category.INPUT ->
handleInputResponse(response, uri)
Response.Code.Category.SUCCESS ->
handleSuccessResponse(response, uri)
Response.Code.Category.REDIRECT ->
@ -89,6 +92,10 @@ class PageViewModel : ViewModel() {
event.postValue(FailureEvent("Error", message))
}
private fun handleInputResponse(response: Response, uri: Uri) {
event.postValue(InputEvent(uri, response.meta))
}
private suspend fun handleSuccessResponse(response: Response, uri: Uri) {
state.postValue(State.RECEIVING)

View file

@ -1,7 +1,6 @@
package dev.lowrespalmtree.comet
import android.net.Uri
import android.util.Log
/**
* Transform a relative URI to an absolute Gemini URI