diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt index 207528d..2bdffb4 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt @@ -18,7 +18,7 @@ class MimeType( if (";" in string) { val elements = string.split(";") typeString = elements[0] - params = mutableMapOf() + params = mutableMapOf() elements.subList(1, elements.size) .map { it.trim().lowercase() } .map { p -> if (p.count { it == '=' } != 1) return@from null else p } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt index 4884d90..cda8c30 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt @@ -8,7 +8,6 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.preference.PreferenceManager -import dev.lowrespalmtree.comet.utils.joinUrls import dev.lowrespalmtree.comet.utils.resolveLinkUri import kotlinx.coroutines.* import kotlinx.coroutines.channels.onSuccess @@ -147,6 +146,20 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState @ExperimentalCoroutinesApi private suspend fun handleSuccessResponse(response: Response, uri: Uri) { + val mimeType = MimeType.from(response.meta) ?: MimeType.DEFAULT // Spec. section 3.3 last ยง + when (mimeType.main) { + "text" -> { + if (mimeType.sub == "gemini") + handleSuccessGemtextResponse(response, uri) + else + handleSuccessGenericTextResponse(response, uri) + } + else -> signalError("No idea how to process a \"${mimeType.short}\" file.") + } + } + + @ExperimentalCoroutinesApi + private suspend fun handleSuccessGemtextResponse(response: Response, uri: Uri) { state.postValue(State.RECEIVING) val uriString = uri.toString() @@ -199,6 +212,10 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState state.postValue(State.IDLE) } + @ExperimentalCoroutinesApi + private suspend fun handleSuccessGenericTextResponse(response: Response, uri: Uri) = + handleSuccessGemtextResponse(response, uri) // TODO render plain text as... something else? + private fun handleRedirectResponse(response: Response, redirects: Int) { event.postValue(RedirectEvent(response.meta, redirects)) }