Compare commits

..

10 commits

Author SHA1 Message Date
dece 6eb9c142cc handle input responses 2022-01-10 00:36:15 +01:00
dece da3a9f4c75 UriUtils: fix issue with encoded colon in netloc 2022-01-09 23:08:52 +01:00
dece 809db8f324 Gemtext: fix link line parsing
Yes you can use tabs as separators…
2022-01-09 23:02:48 +01:00
dece afd0b42c83 PageViewModel: clearer message if no connection 2022-01-09 22:47:34 +01:00
dece bf28d30e4c MainActivity: use / as empty path in URLs
As per the spec, clients should rewrite empty paths of absolute URLs to
"/" instead. Indeed, why the server would return something other than a
redirection for "" to "/"?
2022-01-09 22:18:36 +01:00
dece f6fc5c2f69 PageViewModel: fix activity state on error 2022-01-09 22:06:09 +01:00
dece ee83d1c8fd MainActivity: fix toolbar stuck hidden 2022-01-09 22:02:12 +01:00
dece 2debc6d9f6 gradle: update stuff 2022-01-09 21:41:11 +01:00
dece 4f41d6fecd ContentAdapter: use intermediate rendering blocks
Rendering one view per line is looking for trouble when considering
preformatted blocks and possibly list/blockquotes: you want to group
actions and styles together. Specifically, make preformatted blocks
horizontally scrollable.

This commit adds an intermediate level for rendering which produces such
blocks.
2022-01-09 21:39:33 +01:00
dece 685d60a09b res: factorize text styles 2022-01-05 15:58:56 +01:00
25 changed files with 291 additions and 172 deletions

View file

@ -44,9 +44,9 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.2' implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.room:room-runtime:2.3.0" implementation "androidx.room:room-runtime:2.4.0"
implementation 'com.google.android.material:material:1.4.0' implementation 'com.google.android.material:material:1.4.0'
kapt "androidx.room:room-compiler:2.3.0" kapt "androidx.room:room-compiler:2.4.0"
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

View file

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.lowrespalmtree.comet"> package="dev.lowrespalmtree.comet">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<application <application

View file

@ -10,31 +10,106 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import dev.lowrespalmtree.comet.databinding.* import dev.lowrespalmtree.comet.databinding.*
class ContentAdapter(private var content: List<Line>, private val listener: ContentAdapterListen) : /**
* Adapter for a rendered Gemtext page.
*
* Considering each content line as a different block/View to render works until you want to render
* preformatted lines as a whole block. This adapter approaches this issue by mapping each line to
* a block, where most of the time it will be a 1-to-1 relation, but for blocks and possibly quotes
* it could be a n-to-1 relation: many lines belong to the same block. This of course changes a bit
* the way we have to render things.
*/
class ContentAdapter(private val listener: ContentAdapterListen) :
RecyclerView.Adapter<ContentAdapter.ContentViewHolder>() { RecyclerView.Adapter<ContentAdapter.ContentViewHolder>() {
private var lastLineCount = 0 private var lines = listOf<Line>()
private var currentLine = 0
private var blocks = mutableListOf<ContentBlock>()
private var lastBlockCount = 0
interface ContentAdapterListen { interface ContentAdapterListen {
fun onLinkClick(url: String) fun onLinkClick(url: String)
} }
override fun getItemViewType(position: Int): Int = sealed class ContentBlock {
when (content[position]) { object Empty : ContentBlock()
is EmptyLine -> TYPE_EMPTY class Paragraph(val text: String) : ContentBlock()
is ParagraphLine -> TYPE_PARAGRAPH class Title(val text: String, val level: Int) : ContentBlock()
is LinkLine -> TYPE_LINK class Link(val url: String, val label: String) : ContentBlock()
is PreFenceLine -> TYPE_PRE_FENCE class Pre(val caption: String, var content: String, var closed: Boolean) : ContentBlock()
is PreTextLine -> TYPE_PRE_TEXT class Blockquote(val text: String) : ContentBlock()
is BlockquoteLine -> TYPE_BLOCKQUOTE class ListItem(val text: String) : ContentBlock()
is ListItemLine -> TYPE_LIST_ITEM }
is TitleLine -> when ((content[position] as TitleLine).level) {
1 -> TYPE_TITLE_1 /** Replace the content rendered by the recycler. */
2 -> TYPE_TITLE_2 @SuppressLint("NotifyDataSetChanged")
3 -> TYPE_TITLE_3 fun setLines(newLines: List<Line>) {
else -> error("invalid title level") lines = newLines.toList() // Shallow copy to avoid concurrent update issues.
if (lines.isEmpty()) {
Log.d(TAG, "setLines: empty content")
currentLine = 0
blocks = mutableListOf()
lastBlockCount = 0
notifyDataSetChanged()
} else {
while (currentLine < lines.size) {
when (val line = lines[currentLine]) {
is EmptyLine -> blocks.add(ContentBlock.Empty)
is ParagraphLine -> blocks.add(ContentBlock.Paragraph(line.text))
is LinkLine -> blocks.add(ContentBlock.Link(line.url, line.label))
is BlockquoteLine -> blocks.add(ContentBlock.Blockquote(line.text))
is ListItemLine -> blocks.add(ContentBlock.ListItem(line.text))
is TitleLine -> blocks.add(ContentBlock.Title(line.text, line.level))
is PreFenceLine -> {
var justClosedBlock = false
if (blocks.isNotEmpty()) {
val lastBlock = blocks.last()
if (lastBlock is ContentBlock.Pre && !lastBlock.closed) {
lastBlock.closed = true
justClosedBlock = true
}
}
if (!justClosedBlock)
blocks.add(ContentBlock.Pre(line.caption, "", false))
}
is PreTextLine -> {
val lastBlock = blocks.last()
if (lastBlock is ContentBlock.Pre && !lastBlock.closed)
lastBlock.content += line.text + "\n"
else
Log.e(TAG, "setLines: unexpected preformatted line")
}
}
currentLine++
} }
else -> error("unknown line type") val numAdded = blocks.size - lastBlockCount
Log.d(TAG, "setContent: added $numAdded items")
notifyItemRangeInserted(lastBlockCount, numAdded)
}
lastBlockCount = blocks.size
}
sealed class ContentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
class Empty(binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
class Paragraph(val binding: GemtextParagraphBinding) : ContentViewHolder(binding.root)
class Title1(val binding: GemtextTitle1Binding) : ContentViewHolder(binding.root)
class Title2(val binding: GemtextTitle2Binding) : ContentViewHolder(binding.root)
class Title3(val binding: GemtextTitle3Binding) : ContentViewHolder(binding.root)
class Link(val binding: GemtextLinkBinding) : ContentViewHolder(binding.root)
class Pre(val binding: GemtextPreformattedBinding) : ContentViewHolder(binding.root)
class Blockquote(val binding: GemtextBlockquoteBinding) : ContentViewHolder(binding.root)
class ListItem(val binding: GemtextListItemBinding) : ContentViewHolder(binding.root)
}
override fun getItemViewType(position: Int): Int =
when (val block = blocks[position]) {
is ContentBlock.Empty -> TYPE_EMPTY
is ContentBlock.Paragraph -> TYPE_PARAGRAPH
is ContentBlock.Link -> TYPE_LINK
is ContentBlock.Blockquote -> TYPE_BLOCKQUOTE
is ContentBlock.ListItem -> TYPE_LIST_ITEM
is ContentBlock.Pre -> TYPE_PREFORMATTED
is ContentBlock.Title -> block.level // ruse
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {
@ -46,8 +121,7 @@ class ContentAdapter(private var content: List<Line>, private val listener: Cont
TYPE_TITLE_2 -> ContentViewHolder.Title2(GemtextTitle2Binding.inflate(it)) TYPE_TITLE_2 -> ContentViewHolder.Title2(GemtextTitle2Binding.inflate(it))
TYPE_TITLE_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it)) TYPE_TITLE_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it))
TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it)) TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it))
TYPE_PRE_FENCE -> ContentViewHolder.PreFence(GemtextEmptyBinding.inflate(it)) TYPE_PREFORMATTED -> ContentViewHolder.Pre(GemtextPreformattedBinding.inflate(it))
TYPE_PRE_TEXT -> ContentViewHolder.PreText(GemtextPreformattedBinding.inflate(it))
TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it)) TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it))
TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it)) TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it))
else -> error("invalid view type") else -> error("invalid view type")
@ -57,80 +131,45 @@ class ContentAdapter(private var content: List<Line>, private val listener: Cont
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ContentViewHolder, position: Int) { override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
val line = content[position] when (val block = blocks[position]) {
when (holder) { is ContentBlock.Empty -> {}
is ContentViewHolder.Paragraph -> is ContentBlock.Paragraph ->
holder.binding.textView.text = (line as ParagraphLine).text (holder as ContentViewHolder.Paragraph).binding.textView.text = block.text
is ContentViewHolder.Title1 -> holder.binding.textView.text = (line as TitleLine).text is ContentBlock.Blockquote ->
is ContentViewHolder.Title2 -> holder.binding.textView.text = (line as TitleLine).text (holder as ContentViewHolder.Blockquote).binding.textView.text = block.text
is ContentViewHolder.Title3 -> holder.binding.textView.text = (line as TitleLine).text is ContentBlock.ListItem ->
is ContentViewHolder.Link -> { (holder as ContentViewHolder.ListItem).binding.textView.text = "\u25CF ${block.text}"
val text = if ((line as LinkLine).label.isNotEmpty()) line.label else line.url is ContentBlock.Title -> {
val underlined = SpannableString(text) when (block.level) {
underlined.setSpan(UnderlineSpan(), 0, underlined.length, 0) 1 -> (holder as ContentViewHolder.Title1).binding.textView.text = block.text
holder.binding.textView.text = underlined 2 -> (holder as ContentViewHolder.Title2).binding.textView.text = block.text
holder.binding.root.setOnClickListener { listener.onLinkClick(line.url) } 3 -> (holder as ContentViewHolder.Title3).binding.textView.text = block.text
}
} }
is ContentViewHolder.PreText -> is ContentBlock.Link -> {
holder.binding.textView.text = (line as PreTextLine).text val label = if (block.label.isNotBlank()) block.label else block.url
is ContentViewHolder.Blockquote -> val underlinedLabel = SpannableString(label)
holder.binding.textView.text = (line as BlockquoteLine).text underlinedLabel.setSpan(UnderlineSpan(), 0, underlinedLabel.length, 0)
is ContentViewHolder.ListItem -> (holder as ContentViewHolder.Link).binding.textView.text = underlinedLabel
holder.binding.textView.text = "\u25CF ${(line as ListItemLine).text}" holder.binding.root.setOnClickListener { listener.onLinkClick(block.url) }
else -> {} }
is ContentBlock.Pre ->
(holder as ContentViewHolder.Pre).binding.textView.text = block.content
} }
} }
override fun getItemCount(): Int = content.size override fun getItemCount(): Int = blocks.size
/**
* Replace the content rendered by the recycler.
*
* The new content list may or may not be the same object as the previous one, we don't
* assume anything. The assumptions this function do however are:
* - If the new content is empty, we are about to load new content, so clear the recycler.
* - If it's longer than before, we received new streamed content, so *append* data.
* - If it's shorter or the same size than before, we do not notify anything and let the caller
* manage the changes itself.
*/
@SuppressLint("NotifyDataSetChanged")
fun setContent(newContent: List<Line>) {
content = newContent.toList() // Shallow copy to avoid concurrent update issues.
if (content.isEmpty()) {
Log.d(TAG, "setContent: empty content")
notifyDataSetChanged()
} else if (content.size > lastLineCount) {
val numAdded = content.size - lastLineCount
Log.d(TAG, "setContent: added $numAdded items")
notifyItemRangeInserted(lastLineCount, numAdded)
}
lastLineCount = content.size
}
sealed class ContentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
class Empty(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
class Paragraph(val binding: GemtextParagraphBinding) : ContentViewHolder(binding.root)
class Title1(val binding: GemtextTitle1Binding) : ContentViewHolder(binding.root)
class Title2(val binding: GemtextTitle2Binding) : ContentViewHolder(binding.root)
class Title3(val binding: GemtextTitle3Binding) : ContentViewHolder(binding.root)
class Link(val binding: GemtextLinkBinding) : ContentViewHolder(binding.root)
class PreFence(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
class PreText(val binding: GemtextPreformattedBinding) : ContentViewHolder(binding.root)
class Blockquote(val binding: GemtextBlockquoteBinding) : ContentViewHolder(binding.root)
class ListItem(val binding: GemtextListItemBinding) : ContentViewHolder(binding.root)
}
companion object { companion object {
const val TAG = "ContentRecycler" const val TAG = "ContentRecycler"
const val TYPE_EMPTY = 0 const val TYPE_EMPTY = 0
const val TYPE_PARAGRAPH = 1 const val TYPE_TITLE_1 = 1
const val TYPE_TITLE_1 = 2 const val TYPE_TITLE_2 = 2
const val TYPE_TITLE_2 = 3 const val TYPE_TITLE_3 = 3
const val TYPE_TITLE_3 = 4 const val TYPE_PARAGRAPH = 4
const val TYPE_LINK = 5 const val TYPE_LINK = 5
const val TYPE_PRE_FENCE = 6 const val TYPE_PREFORMATTED = 6
const val TYPE_PRE_TEXT = 7 const val TYPE_BLOCKQUOTE = 7
const val TYPE_BLOCKQUOTE = 8 const val TYPE_LIST_ITEM = 8
const val TYPE_LIST_ITEM = 9
} }
} }

View file

@ -21,6 +21,7 @@ class ListItemLine(val text: String) : Line
const val TAG = "Gemtext" const val TAG = "Gemtext"
/** Pipe incoming gemtext data into parsed Lines. */
fun parseData( fun parseData(
inChannel: Channel<ByteArray>, inChannel: Channel<ByteArray>,
charset: Charset, charset: Charset,
@ -51,19 +52,23 @@ fun parseData(
return channel return channel
} }
/** Parse a single line into a Line object. */
private fun parseLine(line: CharBuffer, isPreformatted: Boolean): Line = private fun parseLine(line: CharBuffer, isPreformatted: Boolean): Line =
when { when {
line.isEmpty() -> EmptyLine()
line.startsWith("###") -> TitleLine(3, getCharsFrom(line, 3)) line.startsWith("###") -> TitleLine(3, getCharsFrom(line, 3))
line.startsWith("##") -> TitleLine(2, getCharsFrom(line, 2)) line.startsWith("##") -> TitleLine(2, getCharsFrom(line, 2))
line.startsWith("#") -> TitleLine(1, getCharsFrom(line, 1)) line.startsWith("#") -> TitleLine(1, getCharsFrom(line, 1))
line.startsWith(">") -> BlockquoteLine(getCharsFrom(line, 1))
line.startsWith("```") -> PreFenceLine(getCharsFrom(line, 3)) line.startsWith("```") -> PreFenceLine(getCharsFrom(line, 3))
line.startsWith("* ") -> ListItemLine(getCharsFrom(line, 2)) line.startsWith("* ") -> ListItemLine(getCharsFrom(line, 2))
line.startsWith(">") -> getCharsFrom(line, 1) // eh empty lines in quotes…
.run { if (isBlank()) EmptyLine() else BlockquoteLine(this) }
line.startsWith("=>") -> getCharsFrom(line, 2) line.startsWith("=>") -> getCharsFrom(line, 2)
.split(" ", limit = 2) .split(" ", "\t", limit = 2)
.run { LinkLine(get(0), if (size == 2) get(1).trimStart() else "") } .run { LinkLine(get(0), if (size == 2) get(1).trimStart() else "") }
else -> if (isPreformatted) PreTextLine(line.toString()) else ParagraphLine(line.toString()) isPreformatted -> PreTextLine(line.toString())
line.isEmpty() -> EmptyLine()
else -> ParagraphLine(line.toString())
} }
/** Get trimmed string from the char buffer starting from this position. */
private fun getCharsFrom(line: CharBuffer, index: Int) = line.substring(index).trim() private fun getCharsFrom(line: CharBuffer, index: Int) = line.substring(index).trim()

View file

@ -6,16 +6,23 @@ import android.content.Intent
import android.content.Intent.ACTION_VIEW import android.content.Intent.ACTION_VIEW
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.InputType
import android.util.Log import android.util.Log
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager 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.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setMargins
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen { class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
private lateinit var binding: ActivityMainBinding private lateinit var binding: ActivityMainBinding
@ -39,7 +46,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
setContentView(binding.root) setContentView(binding.root)
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java] pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
adapter = ContentAdapter(listOf(), this) adapter = ContentAdapter(this)
binding.contentRecycler.layoutManager = LinearLayoutManager(this) binding.contentRecycler.layoutManager = LinearLayoutManager(this)
binding.contentRecycler.adapter = adapter binding.contentRecycler.adapter = adapter
@ -75,6 +82,16 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null) openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null)
} }
/**
* Open an URL.
*
* This function can be called after the user entered an URL in the app bar, clicked on a link,
* whatever. To make the user's life a bit easier, this function also makes a few guesses:
* - If the URL is not absolute, make it so from a base URL (e.g. the current URL) or assume
* the user only typed a hostname without scheme and use a utility function to make it
* absolute.
* - If it's an absolute Gemini URL with an empty path, use "/" instead as per the spec.
*/
private fun openUrl(url: String, base: String? = null, redirections: Int = 0) { private fun openUrl(url: String, base: String? = null, redirections: Int = 0) {
if (redirections >= 5) { if (redirections >= 5) {
alert("Too many redirections.") alert("Too many redirections.")
@ -84,6 +101,8 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
var uri = Uri.parse(url) var uri = Uri.parse(url)
if (!uri.isAbsolute) { if (!uri.isAbsolute) {
uri = if (!base.isNullOrEmpty()) joinUrls(base, url) else toGeminiUri(uri) uri = if (!base.isNullOrEmpty()) joinUrls(base, url) else toGeminiUri(uri)
} else if (uri.scheme == "gemini" && uri.path.isNullOrEmpty()) {
uri = uri.buildUpon().path("/").build()
} }
when (uri.scheme) { when (uri.scheme) {
@ -103,6 +122,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
binding.contentProgressBar.show() binding.contentProgressBar.show()
} }
PageViewModel.State.RECEIVING -> { PageViewModel.State.RECEIVING -> {
binding.appBar.setExpanded(true, true)
binding.contentSwipeLayout.isRefreshing = false binding.contentSwipeLayout.isRefreshing = false
} }
} }
@ -110,13 +130,34 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
private fun updateLines(lines: List<Line>) { private fun updateLines(lines: List<Line>) {
Log.d(TAG, "updateLines: ${lines.size} lines") Log.d(TAG, "updateLines: ${lines.size} lines")
adapter.setContent(lines) adapter.setLines(lines)
} }
private fun handleEvent(event: PageViewModel.Event) { private fun handleEvent(event: PageViewModel.Event) {
Log.d(TAG, "handleEvent: $event") Log.d(TAG, "handleEvent: $event")
if (!event.handled) { if (!event.handled) {
when (event) { 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 -> { is PageViewModel.SuccessEvent -> {
currentUrl = event.uri currentUrl = event.uri
visitedUrls.add(event.uri) visitedUrls.add(event.uri)
@ -127,8 +168,11 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
is PageViewModel.FailureEvent -> { is PageViewModel.FailureEvent -> {
var message = event.details var message = event.details
if (!event.serverDetails.isNullOrEmpty()) if (!event.serverDetails.isNullOrEmpty())
message += "\n\n" + "Server details: ${event.serverDetails}" message += "\n\nServer details: ${event.serverDetails}"
if (!isConnectedToNetwork(this))
message += "\n\nInternet may be inaccessible…"
alert(message, title = event.short) alert(message, title = event.short)
updateState(PageViewModel.State.IDLE)
} }
} }
event.handled = true event.handled = true
@ -147,7 +191,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
try { try {
startActivity(Intent(ACTION_VIEW, uri)) startActivity(Intent(ACTION_VIEW, uri))
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
alert("Can't open this URL.") alert("Can't find an app to open \"${uri.scheme}\" URLs.")
} }
} }

View file

@ -0,0 +1,11 @@
package dev.lowrespalmtree.comet
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
fun isConnectedToNetwork(context: Context): Boolean {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val caps = connManager.getNetworkCapabilities(connManager.activeNetwork) ?: return false
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}

View file

@ -25,6 +25,7 @@ class PageViewModel : ViewModel() {
} }
abstract class Event(var handled: Boolean = false) 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 SuccessEvent(val uri: String) : Event()
data class RedirectEvent(val uri: String, val redirects: Int) : Event() data class RedirectEvent(val uri: String, val redirects: Int) : Event()
data class FailureEvent( data class FailureEvent(
@ -73,12 +74,14 @@ class PageViewModel : ViewModel() {
Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"") Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"")
when (response.code.getCategory()) { when (response.code.getCategory()) {
Response.Code.Category.INPUT ->
handleInputResponse(response, uri)
Response.Code.Category.SUCCESS -> Response.Code.Category.SUCCESS ->
handleRequestSuccess(response, uri) handleSuccessResponse(response, uri)
Response.Code.Category.REDIRECT -> Response.Code.Category.REDIRECT ->
handleRedirect(response, redirects = redirects + 1) handleRedirectResponse(response, redirects = redirects + 1)
Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR -> Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR ->
handleError(response) handleErrorResponse(response)
else -> else ->
signalError("Can't handle code ${response.code}.") signalError("Can't handle code ${response.code}.")
} }
@ -87,10 +90,13 @@ class PageViewModel : ViewModel() {
private fun signalError(message: String) { private fun signalError(message: String) {
event.postValue(FailureEvent("Error", message)) event.postValue(FailureEvent("Error", message))
state.postValue(State.IDLE)
} }
private suspend fun handleRequestSuccess(response: Response, uri: Uri) { 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) state.postValue(State.RECEIVING)
linesList.clear() linesList.clear()
@ -135,11 +141,11 @@ class PageViewModel : ViewModel() {
state.postValue(State.IDLE) state.postValue(State.IDLE)
} }
private fun handleRedirect(response: Response, redirects: Int) { private fun handleRedirectResponse(response: Response, redirects: Int) {
event.postValue(RedirectEvent(response.meta, redirects)) event.postValue(RedirectEvent(response.meta, redirects))
} }
private fun handleError(response: Response) { private fun handleErrorResponse(response: Response) {
val briefMessage = when (response.code) { val briefMessage = when (response.code) {
Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure" Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure"
Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable" Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable"

View file

@ -1,7 +1,6 @@
package dev.lowrespalmtree.comet package dev.lowrespalmtree.comet
import android.net.Uri import android.net.Uri
import android.util.Log
/** /**
* Transform a relative URI to an absolute Gemini URI * Transform a relative URI to an absolute Gemini URI
@ -15,6 +14,7 @@ fun toGeminiUri(uri: Uri): Uri =
Uri.Builder() Uri.Builder()
.scheme("gemini") .scheme("gemini")
.authority(uri.path) .authority(uri.path)
.path("/")
.query(uri.query) .query(uri.query)
.fragment(uri.fragment) .fragment(uri.fragment)
.build() .build()
@ -35,7 +35,7 @@ fun joinUrls(base: String, relative: String): Uri {
) )
return Uri.Builder() return Uri.Builder()
.scheme(baseUri.scheme) .scheme(baseUri.scheme)
.authority(baseUri.authority) .encodedAuthority(baseUri.authority)
.path(newPath) .path(newPath)
.query(relUri.query) .query(relUri.query)
.fragment(relUri.fragment) .fragment(relUri.fragment)

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<stroke android:width="1dip" android:color="@color/black"/>
</shape>

Binary file not shown.

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/hack"/>
</font-family>

View file

@ -11,12 +11,12 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="56dp"> android:layout_height="56dp">
<com.google.android.material.appbar.CollapsingToolbarLayout <Toolbar
android:id="@+id/collapsing_toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary" app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" app:layout_scrollFlags="scroll|enterAlways"
app:toolbarId="@+id/toolbar"> app:toolbarId="@+id/toolbar">
<EditText <EditText
@ -31,7 +31,7 @@
android:text="" android:text=""
tools:ignore="TextContrastCheck" /> tools:ignore="TextContrastCheck" />
</com.google.android.material.appbar.CollapsingToolbarLayout> </Toolbar>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>

View file

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="16sp"
android:textColor="@color/text"
android:textStyle="italic"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
style="@style/CometText"
android:textStyle="italic"
android:paddingStart="32dp" android:paddingStart="32dp"
android:paddingEnd="16dp" android:paddingEnd="16dp" />
android:paddingTop="2dp"
android:paddingBottom="4dp" />

View file

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="4dp" /> android:layout_height="0dp"
android:textSize="8sp"
tools:ignore="SmallSp" />

View file

@ -1,12 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="16sp"
android:textColor="@color/teal_700"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp" style="@style/CometText"
android:paddingEnd="16dp" android:textColor="@color/teal_700"
android:paddingTop="2dp"
android:paddingBottom="4dp"
android:textIsSelectable="false" /> android:textIsSelectable="false" />

View file

@ -1,11 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="16sp"
android:textColor="@color/text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
style="@style/CometText"
android:paddingStart="32dp" android:paddingStart="32dp"
android:paddingEnd="16dp" android:paddingEnd="16dp" />
android:paddingTop="2dp"
android:paddingBottom="4dp" />

View file

@ -1,11 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="16sp"
android:textColor="@color/text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp" style="@style/CometText" />
android:paddingEnd="16dp"
android:paddingTop="2dp"
android:paddingBottom="4dp" />

View file

@ -1,13 +1,19 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:textSize="14sp"
android:textColor="@color/text"
android:typeface="monospace"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="8dp" android:paddingEnd="16dp">
android:paddingTop="2dp" <TextView
android:paddingBottom="2dp" android:id="@+id/text_view"
android:background="@color/purple_200" /> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/text"
android:fontFamily="@font/preformatted"
android:typeface="monospace"
android:paddingStart="16dp"
android:paddingEnd="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</HorizontalScrollView>

View file

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="30sp"
android:textColor="@color/text"
android:textStyle="bold"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp" style="@style/CometText"
android:paddingEnd="16dp" android:textSize="30sp"
android:paddingTop="16dp" android:textStyle="bold"
android:paddingBottom="4dp" /> android:paddingTop="16dp" />

View file

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="24sp"
android:textColor="@color/text"
android:textStyle="bold"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp" style="@style/CometText"
android:paddingEnd="16dp" android:textSize="24sp"
android:paddingTop="8dp" android:textStyle="bold"
android:paddingBottom="4dp" /> android:paddingTop="8dp" />

View file

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view" android:id="@+id/text_view"
android:textSize="18sp"
android:textColor="@color/text"
android:textStyle="bold"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp" style="@style/CometText"
android:paddingEnd="16dp" android:textSize="18sp"
android:paddingTop="8dp" android:textStyle="bold"
android:paddingBottom="4dp" /> android:paddingTop="8dp" />

View file

@ -6,6 +6,12 @@
<color name="teal_200">#FF03DAC5</color> <color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="text">#222222</color> <color name="bright">#F3F4ED</color>
<color name="text_light">#536162</color>
<color name="text">#424642</color>
<color name="orange">#C06014</color>
<color name="orange_dark">#683C19</color>
<color name="teal">#14C0B2</color>
<color name="teal_dark">#147169</color>
</resources> </resources>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CometText">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/text</item>
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">16dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">4dp</item>
<item name="android:textIsSelectable">true</item>
</style>
</resources>

View file

@ -2,15 +2,17 @@
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. --> <!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item> <item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryVariant">@color/purple_500</item> <item name="colorPrimaryVariant">@color/orange_dark</item>
<item name="colorOnPrimary">@color/white</item> <item name="colorOnPrimary">@color/bright</item>
<!-- Secondary brand color. --> <!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal</item>
<item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorSecondaryVariant">@color/teal_dark</item>
<item name="colorOnSecondary">@color/black</item> <item name="colorOnSecondary">@color/text</item>
<!-- Status bar color. --> <!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="android:windowBackground">@color/bright</item>
<item name="android:progressBackgroundTint">?attr/colorSecondaryVariant</item>
</style> </style>
</resources> </resources>

View file

@ -5,7 +5,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath "com.android.tools.build:gradle:7.0.3" classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong