From 6eb9c142cc99285315ad27cc735bffb9ab6a0892 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 10 Jan 2022 00:36:15 +0100 Subject: [PATCH] handle input responses --- .../dev/lowrespalmtree/comet/MainActivity.kt | 28 +++++++++++++++++++ .../dev/lowrespalmtree/comet/PageViewModel.kt | 7 +++++ .../java/dev/lowrespalmtree/comet/UriUtils.kt | 1 - 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt index 41fda2b..9cfdce9 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt @@ -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) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt index 24ef1cb..7f2c2d6 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt @@ -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) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt b/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt index e636d4d..842fea8 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt @@ -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