Compare commits
10 commits
781a4a66b0
...
6eb9c142cc
Author | SHA1 | Date | |
---|---|---|---|
dece | 6eb9c142cc | ||
dece | da3a9f4c75 | ||
dece | 809db8f324 | ||
dece | afd0b42c83 | ||
dece | bf28d30e4c | ||
dece | f6fc5c2f69 | ||
dece | ee83d1c8fd | ||
dece | 2debc6d9f6 | ||
dece | 4f41d6fecd | ||
dece | 685d60a09b |
|
@ -44,9 +44,9 @@ dependencies {
|
|||
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.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'
|
||||
kapt "androidx.room:room-compiler:2.3.0"
|
||||
kapt "androidx.room:room-compiler:2.4.0"
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="dev.lowrespalmtree.comet">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
|
|
|
@ -10,31 +10,106 @@ import android.view.ViewGroup
|
|||
import androidx.recyclerview.widget.RecyclerView
|
||||
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>() {
|
||||
|
||||
private var lastLineCount = 0
|
||||
private var lines = listOf<Line>()
|
||||
private var currentLine = 0
|
||||
private var blocks = mutableListOf<ContentBlock>()
|
||||
private var lastBlockCount = 0
|
||||
|
||||
interface ContentAdapterListen {
|
||||
fun onLinkClick(url: String)
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int =
|
||||
when (content[position]) {
|
||||
is EmptyLine -> TYPE_EMPTY
|
||||
is ParagraphLine -> TYPE_PARAGRAPH
|
||||
is LinkLine -> TYPE_LINK
|
||||
is PreFenceLine -> TYPE_PRE_FENCE
|
||||
is PreTextLine -> TYPE_PRE_TEXT
|
||||
is BlockquoteLine -> TYPE_BLOCKQUOTE
|
||||
is ListItemLine -> TYPE_LIST_ITEM
|
||||
is TitleLine -> when ((content[position] as TitleLine).level) {
|
||||
1 -> TYPE_TITLE_1
|
||||
2 -> TYPE_TITLE_2
|
||||
3 -> TYPE_TITLE_3
|
||||
else -> error("invalid title level")
|
||||
sealed class ContentBlock {
|
||||
object Empty : ContentBlock()
|
||||
class Paragraph(val text: String) : ContentBlock()
|
||||
class Title(val text: String, val level: Int) : ContentBlock()
|
||||
class Link(val url: String, val label: String) : ContentBlock()
|
||||
class Pre(val caption: String, var content: String, var closed: Boolean) : ContentBlock()
|
||||
class Blockquote(val text: String) : ContentBlock()
|
||||
class ListItem(val text: String) : ContentBlock()
|
||||
}
|
||||
|
||||
/** Replace the content rendered by the recycler. */
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun setLines(newLines: List<Line>) {
|
||||
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 {
|
||||
|
@ -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_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it))
|
||||
TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it))
|
||||
TYPE_PRE_FENCE -> ContentViewHolder.PreFence(GemtextEmptyBinding.inflate(it))
|
||||
TYPE_PRE_TEXT -> ContentViewHolder.PreText(GemtextPreformattedBinding.inflate(it))
|
||||
TYPE_PREFORMATTED -> ContentViewHolder.Pre(GemtextPreformattedBinding.inflate(it))
|
||||
TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it))
|
||||
TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it))
|
||||
else -> error("invalid view type")
|
||||
|
@ -57,80 +131,45 @@ class ContentAdapter(private var content: List<Line>, private val listener: Cont
|
|||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
|
||||
val line = content[position]
|
||||
when (holder) {
|
||||
is ContentViewHolder.Paragraph ->
|
||||
holder.binding.textView.text = (line as ParagraphLine).text
|
||||
is ContentViewHolder.Title1 -> holder.binding.textView.text = (line as TitleLine).text
|
||||
is ContentViewHolder.Title2 -> holder.binding.textView.text = (line as TitleLine).text
|
||||
is ContentViewHolder.Title3 -> holder.binding.textView.text = (line as TitleLine).text
|
||||
is ContentViewHolder.Link -> {
|
||||
val text = if ((line as LinkLine).label.isNotEmpty()) line.label else line.url
|
||||
val underlined = SpannableString(text)
|
||||
underlined.setSpan(UnderlineSpan(), 0, underlined.length, 0)
|
||||
holder.binding.textView.text = underlined
|
||||
holder.binding.root.setOnClickListener { listener.onLinkClick(line.url) }
|
||||
when (val block = blocks[position]) {
|
||||
is ContentBlock.Empty -> {}
|
||||
is ContentBlock.Paragraph ->
|
||||
(holder as ContentViewHolder.Paragraph).binding.textView.text = block.text
|
||||
is ContentBlock.Blockquote ->
|
||||
(holder as ContentViewHolder.Blockquote).binding.textView.text = block.text
|
||||
is ContentBlock.ListItem ->
|
||||
(holder as ContentViewHolder.ListItem).binding.textView.text = "\u25CF ${block.text}"
|
||||
is ContentBlock.Title -> {
|
||||
when (block.level) {
|
||||
1 -> (holder as ContentViewHolder.Title1).binding.textView.text = block.text
|
||||
2 -> (holder as ContentViewHolder.Title2).binding.textView.text = block.text
|
||||
3 -> (holder as ContentViewHolder.Title3).binding.textView.text = block.text
|
||||
}
|
||||
}
|
||||
is ContentViewHolder.PreText ->
|
||||
holder.binding.textView.text = (line as PreTextLine).text
|
||||
is ContentViewHolder.Blockquote ->
|
||||
holder.binding.textView.text = (line as BlockquoteLine).text
|
||||
is ContentViewHolder.ListItem ->
|
||||
holder.binding.textView.text = "\u25CF ${(line as ListItemLine).text}"
|
||||
else -> {}
|
||||
is ContentBlock.Link -> {
|
||||
val label = if (block.label.isNotBlank()) block.label else block.url
|
||||
val underlinedLabel = SpannableString(label)
|
||||
underlinedLabel.setSpan(UnderlineSpan(), 0, underlinedLabel.length, 0)
|
||||
(holder as ContentViewHolder.Link).binding.textView.text = underlinedLabel
|
||||
holder.binding.root.setOnClickListener { listener.onLinkClick(block.url) }
|
||||
}
|
||||
is ContentBlock.Pre ->
|
||||
(holder as ContentViewHolder.Pre).binding.textView.text = block.content
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = content.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)
|
||||
}
|
||||
override fun getItemCount(): Int = blocks.size
|
||||
|
||||
companion object {
|
||||
const val TAG = "ContentRecycler"
|
||||
const val TYPE_EMPTY = 0
|
||||
const val TYPE_PARAGRAPH = 1
|
||||
const val TYPE_TITLE_1 = 2
|
||||
const val TYPE_TITLE_2 = 3
|
||||
const val TYPE_TITLE_3 = 4
|
||||
const val TYPE_TITLE_1 = 1
|
||||
const val TYPE_TITLE_2 = 2
|
||||
const val TYPE_TITLE_3 = 3
|
||||
const val TYPE_PARAGRAPH = 4
|
||||
const val TYPE_LINK = 5
|
||||
const val TYPE_PRE_FENCE = 6
|
||||
const val TYPE_PRE_TEXT = 7
|
||||
const val TYPE_BLOCKQUOTE = 8
|
||||
const val TYPE_LIST_ITEM = 9
|
||||
const val TYPE_PREFORMATTED = 6
|
||||
const val TYPE_BLOCKQUOTE = 7
|
||||
const val TYPE_LIST_ITEM = 8
|
||||
}
|
||||
}
|
|
@ -21,6 +21,7 @@ class ListItemLine(val text: String) : Line
|
|||
|
||||
const val TAG = "Gemtext"
|
||||
|
||||
/** Pipe incoming gemtext data into parsed Lines. */
|
||||
fun parseData(
|
||||
inChannel: Channel<ByteArray>,
|
||||
charset: Charset,
|
||||
|
@ -51,19 +52,23 @@ fun parseData(
|
|||
return channel
|
||||
}
|
||||
|
||||
/** Parse a single line into a Line object. */
|
||||
private fun parseLine(line: CharBuffer, isPreformatted: Boolean): Line =
|
||||
when {
|
||||
line.isEmpty() -> EmptyLine()
|
||||
line.startsWith("###") -> TitleLine(3, getCharsFrom(line, 3))
|
||||
line.startsWith("##") -> TitleLine(2, getCharsFrom(line, 2))
|
||||
line.startsWith("#") -> TitleLine(1, getCharsFrom(line, 1))
|
||||
line.startsWith(">") -> BlockquoteLine(getCharsFrom(line, 1))
|
||||
line.startsWith("```") -> PreFenceLine(getCharsFrom(line, 3))
|
||||
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)
|
||||
.split(" ", limit = 2)
|
||||
.split(" ", "\t", limit = 2)
|
||||
.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()
|
|
@ -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
|
||||
|
@ -39,7 +46,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
setContentView(binding.root)
|
||||
|
||||
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
||||
adapter = ContentAdapter(listOf(), this)
|
||||
adapter = ContentAdapter(this)
|
||||
binding.contentRecycler.layoutManager = LinearLayoutManager(this)
|
||||
binding.contentRecycler.adapter = adapter
|
||||
|
||||
|
@ -75,6 +82,16 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
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) {
|
||||
if (redirections >= 5) {
|
||||
alert("Too many redirections.")
|
||||
|
@ -84,6 +101,8 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
var uri = Uri.parse(url)
|
||||
if (!uri.isAbsolute) {
|
||||
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) {
|
||||
|
@ -103,6 +122,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
binding.contentProgressBar.show()
|
||||
}
|
||||
PageViewModel.State.RECEIVING -> {
|
||||
binding.appBar.setExpanded(true, true)
|
||||
binding.contentSwipeLayout.isRefreshing = false
|
||||
}
|
||||
}
|
||||
|
@ -110,13 +130,34 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
|
||||
private fun updateLines(lines: List<Line>) {
|
||||
Log.d(TAG, "updateLines: ${lines.size} lines")
|
||||
adapter.setContent(lines)
|
||||
adapter.setLines(lines)
|
||||
}
|
||||
|
||||
private fun handleEvent(event: PageViewModel.Event) {
|
||||
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)
|
||||
|
@ -127,8 +168,11 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
is PageViewModel.FailureEvent -> {
|
||||
var message = event.details
|
||||
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)
|
||||
updateState(PageViewModel.State.IDLE)
|
||||
}
|
||||
}
|
||||
event.handled = true
|
||||
|
@ -147,7 +191,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
try {
|
||||
startActivity(Intent(ACTION_VIEW, uri))
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
alert("Can't open this URL.")
|
||||
alert("Can't find an app to open \"${uri.scheme}\" URLs.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt
Normal file
11
app/src/main/java/dev/lowrespalmtree/comet/NetUtils.kt
Normal 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)
|
||||
}
|
|
@ -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,12 +74,14 @@ 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 ->
|
||||
handleRequestSuccess(response, uri)
|
||||
handleSuccessResponse(response, uri)
|
||||
Response.Code.Category.REDIRECT ->
|
||||
handleRedirect(response, redirects = redirects + 1)
|
||||
handleRedirectResponse(response, redirects = redirects + 1)
|
||||
Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR ->
|
||||
handleError(response)
|
||||
handleErrorResponse(response)
|
||||
else ->
|
||||
signalError("Can't handle code ${response.code}.")
|
||||
}
|
||||
|
@ -87,10 +90,13 @@ class PageViewModel : ViewModel() {
|
|||
|
||||
private fun signalError(message: String) {
|
||||
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)
|
||||
|
||||
linesList.clear()
|
||||
|
@ -135,11 +141,11 @@ class PageViewModel : ViewModel() {
|
|||
state.postValue(State.IDLE)
|
||||
}
|
||||
|
||||
private fun handleRedirect(response: Response, redirects: Int) {
|
||||
private fun handleRedirectResponse(response: Response, redirects: Int) {
|
||||
event.postValue(RedirectEvent(response.meta, redirects))
|
||||
}
|
||||
|
||||
private fun handleError(response: Response) {
|
||||
private fun handleErrorResponse(response: Response) {
|
||||
val briefMessage = when (response.code) {
|
||||
Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure"
|
||||
Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable"
|
||||
|
|
|
@ -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
|
||||
|
@ -15,6 +14,7 @@ fun toGeminiUri(uri: Uri): Uri =
|
|||
Uri.Builder()
|
||||
.scheme("gemini")
|
||||
.authority(uri.path)
|
||||
.path("/")
|
||||
.query(uri.query)
|
||||
.fragment(uri.fragment)
|
||||
.build()
|
||||
|
@ -35,7 +35,7 @@ fun joinUrls(base: String, relative: String): Uri {
|
|||
)
|
||||
return Uri.Builder()
|
||||
.scheme(baseUri.scheme)
|
||||
.authority(baseUri.authority)
|
||||
.encodedAuthority(baseUri.authority)
|
||||
.path(newPath)
|
||||
.query(relUri.query)
|
||||
.fragment(relUri.fragment)
|
||||
|
|
4
app/src/main/res/drawable/devborders.xml
Normal file
4
app/src/main/res/drawable/devborders.xml
Normal 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>
|
BIN
app/src/main/res/font/hack.ttf
Normal file
BIN
app/src/main/res/font/hack.ttf
Normal file
Binary file not shown.
4
app/src/main/res/font/preformatted.xml
Normal file
4
app/src/main/res/font/preformatted.xml
Normal 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>
|
|
@ -11,12 +11,12 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="56dp">
|
||||
|
||||
<com.google.android.material.appbar.CollapsingToolbarLayout
|
||||
android:id="@+id/collapsing_toolbar"
|
||||
<Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:contentScrim="?attr/colorPrimary"
|
||||
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
|
||||
app:layout_scrollFlags="scroll|enterAlways"
|
||||
app:toolbarId="@+id/toolbar">
|
||||
|
||||
<EditText
|
||||
|
@ -31,7 +31,7 @@
|
|||
android:text=""
|
||||
tools:ignore="TextContrastCheck" />
|
||||
|
||||
</com.google.android.material.appbar.CollapsingToolbarLayout>
|
||||
</Toolbar>
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/text"
|
||||
android:textStyle="italic"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/CometText"
|
||||
android:textStyle="italic"
|
||||
android:paddingStart="32dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
||||
android:paddingEnd="16dp" />
|
|
@ -1,4 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="4dp" />
|
||||
android:layout_height="0dp"
|
||||
android:textSize="8sp"
|
||||
tools:ignore="SmallSp" />
|
|
@ -1,12 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/teal_700"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp"
|
||||
style="@style/CometText"
|
||||
android:textColor="@color/teal_700"
|
||||
android:textIsSelectable="false" />
|
|
@ -1,11 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/CometText"
|
||||
android:paddingStart="32dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
||||
android:paddingEnd="16dp" />
|
|
@ -1,11 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
||||
style="@style/CometText" />
|
|
@ -1,13 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView 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"
|
||||
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="2dp"
|
||||
android:background="@color/purple_200" />
|
||||
android:paddingEnd="16dp">
|
||||
<TextView
|
||||
android:id="@+id/text_view"
|
||||
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>
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="30sp"
|
||||
android:textColor="@color/text"
|
||||
android:textStyle="bold"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="4dp" />
|
||||
style="@style/CometText"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="16dp" />
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="24sp"
|
||||
android:textColor="@color/text"
|
||||
android:textStyle="bold"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="4dp" />
|
||||
style="@style/CometText"
|
||||
android:textSize="24sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="8dp" />
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/text_view"
|
||||
android:textSize="18sp"
|
||||
android:textColor="@color/text"
|
||||
android:textStyle="bold"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="4dp" />
|
||||
style="@style/CometText"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="8dp" />
|
|
@ -6,6 +6,12 @@
|
|||
<color name="teal_200">#FF03DAC5</color>
|
||||
<color name="teal_700">#FF018786</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>
|
12
app/src/main/res/values/styles.xml
Normal file
12
app/src/main/res/values/styles.xml
Normal 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>
|
|
@ -2,15 +2,17 @@
|
|||
<!-- Base application theme. -->
|
||||
<style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/purple_200</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_500</item>
|
||||
<item name="colorOnPrimary">@color/white</item>
|
||||
<item name="colorPrimary">@color/orange</item>
|
||||
<item name="colorPrimaryVariant">@color/orange_dark</item>
|
||||
<item name="colorOnPrimary">@color/bright</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<item name="colorSecondary">@color/teal</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_dark</item>
|
||||
<item name="colorOnSecondary">@color/text</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="android:windowBackground">@color/bright</item>
|
||||
<item name="android:progressBackgroundTint">?attr/colorSecondaryVariant</item>
|
||||
</style>
|
||||
</resources>
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
mavenCentral()
|
||||
}
|
||||
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"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
|
Reference in a new issue