Compare commits
No commits in common. "6eb9c142cc99285315ad27cc735bffb9ab6a0892" and "781a4a66b096dcb1b8f9186deb0efec0c4310205" have entirely different histories.
6eb9c142cc
...
781a4a66b0
|
@ -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.4.0"
|
||||
implementation "androidx.room:room-runtime:2.3.0"
|
||||
implementation 'com.google.android.material:material:1.4.0'
|
||||
kapt "androidx.room:room-compiler:2.4.0"
|
||||
kapt "androidx.room:room-compiler:2.3.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,7 +2,6 @@
|
|||
<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,106 +10,31 @@ import android.view.ViewGroup
|
|||
import androidx.recyclerview.widget.RecyclerView
|
||||
import dev.lowrespalmtree.comet.databinding.*
|
||||
|
||||
/**
|
||||
* 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) :
|
||||
class ContentAdapter(private var content: List<Line>, private val listener: ContentAdapterListen) :
|
||||
RecyclerView.Adapter<ContentAdapter.ContentViewHolder>() {
|
||||
|
||||
private var lines = listOf<Line>()
|
||||
private var currentLine = 0
|
||||
private var blocks = mutableListOf<ContentBlock>()
|
||||
private var lastBlockCount = 0
|
||||
private var lastLineCount = 0
|
||||
|
||||
interface ContentAdapterListen {
|
||||
fun onLinkClick(url: String)
|
||||
}
|
||||
|
||||
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++
|
||||
}
|
||||
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
|
||||
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")
|
||||
}
|
||||
else -> error("unknown line type")
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {
|
||||
|
@ -121,7 +46,8 @@ class ContentAdapter(private val listener: ContentAdapterListen) :
|
|||
TYPE_TITLE_2 -> ContentViewHolder.Title2(GemtextTitle2Binding.inflate(it))
|
||||
TYPE_TITLE_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it))
|
||||
TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it))
|
||||
TYPE_PREFORMATTED -> ContentViewHolder.Pre(GemtextPreformattedBinding.inflate(it))
|
||||
TYPE_PRE_FENCE -> ContentViewHolder.PreFence(GemtextEmptyBinding.inflate(it))
|
||||
TYPE_PRE_TEXT -> ContentViewHolder.PreText(GemtextPreformattedBinding.inflate(it))
|
||||
TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it))
|
||||
TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it))
|
||||
else -> error("invalid view type")
|
||||
|
@ -131,45 +57,80 @@ class ContentAdapter(private val listener: ContentAdapterListen) :
|
|||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
|
||||
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
|
||||
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) }
|
||||
}
|
||||
}
|
||||
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
|
||||
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 -> {}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = blocks.size
|
||||
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)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = "ContentRecycler"
|
||||
const val TYPE_EMPTY = 0
|
||||
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_PARAGRAPH = 1
|
||||
const val TYPE_TITLE_1 = 2
|
||||
const val TYPE_TITLE_2 = 3
|
||||
const val TYPE_TITLE_3 = 4
|
||||
const val TYPE_LINK = 5
|
||||
const val TYPE_PREFORMATTED = 6
|
||||
const val TYPE_BLOCKQUOTE = 7
|
||||
const val TYPE_LIST_ITEM = 8
|
||||
const val TYPE_PRE_FENCE = 6
|
||||
const val TYPE_PRE_TEXT = 7
|
||||
const val TYPE_BLOCKQUOTE = 8
|
||||
const val TYPE_LIST_ITEM = 9
|
||||
}
|
||||
}
|
|
@ -21,7 +21,6 @@ class ListItemLine(val text: String) : Line
|
|||
|
||||
const val TAG = "Gemtext"
|
||||
|
||||
/** Pipe incoming gemtext data into parsed Lines. */
|
||||
fun parseData(
|
||||
inChannel: Channel<ByteArray>,
|
||||
charset: Charset,
|
||||
|
@ -52,23 +51,19 @@ 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(" ", "\t", limit = 2)
|
||||
.split(" ", limit = 2)
|
||||
.run { LinkLine(get(0), if (size == 2) get(1).trimStart() else "") }
|
||||
isPreformatted -> PreTextLine(line.toString())
|
||||
line.isEmpty() -> EmptyLine()
|
||||
else -> ParagraphLine(line.toString())
|
||||
else -> if (isPreformatted) PreTextLine(line.toString()) 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,23 +6,16 @@ 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
|
||||
|
@ -46,7 +39,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
setContentView(binding.root)
|
||||
|
||||
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
||||
adapter = ContentAdapter(this)
|
||||
adapter = ContentAdapter(listOf(), this)
|
||||
binding.contentRecycler.layoutManager = LinearLayoutManager(this)
|
||||
binding.contentRecycler.adapter = adapter
|
||||
|
||||
|
@ -82,16 +75,6 @@ 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.")
|
||||
|
@ -101,8 +84,6 @@ 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) {
|
||||
|
@ -122,7 +103,6 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
binding.contentProgressBar.show()
|
||||
}
|
||||
PageViewModel.State.RECEIVING -> {
|
||||
binding.appBar.setExpanded(true, true)
|
||||
binding.contentSwipeLayout.isRefreshing = false
|
||||
}
|
||||
}
|
||||
|
@ -130,34 +110,13 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
|
||||
private fun updateLines(lines: List<Line>) {
|
||||
Log.d(TAG, "updateLines: ${lines.size} lines")
|
||||
adapter.setLines(lines)
|
||||
adapter.setContent(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)
|
||||
|
@ -168,11 +127,8 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
is PageViewModel.FailureEvent -> {
|
||||
var message = event.details
|
||||
if (!event.serverDetails.isNullOrEmpty())
|
||||
message += "\n\nServer details: ${event.serverDetails}"
|
||||
if (!isConnectedToNetwork(this))
|
||||
message += "\n\nInternet may be inaccessible…"
|
||||
message += "\n\n" + "Server details: ${event.serverDetails}"
|
||||
alert(message, title = event.short)
|
||||
updateState(PageViewModel.State.IDLE)
|
||||
}
|
||||
}
|
||||
event.handled = true
|
||||
|
@ -191,7 +147,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
|||
try {
|
||||
startActivity(Intent(ACTION_VIEW, uri))
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
alert("Can't find an app to open \"${uri.scheme}\" URLs.")
|
||||
alert("Can't open this URL.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
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,7 +25,6 @@ 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(
|
||||
|
@ -74,14 +73,12 @@ 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)
|
||||
handleRequestSuccess(response, uri)
|
||||
Response.Code.Category.REDIRECT ->
|
||||
handleRedirectResponse(response, redirects = redirects + 1)
|
||||
handleRedirect(response, redirects = redirects + 1)
|
||||
Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR ->
|
||||
handleErrorResponse(response)
|
||||
handleError(response)
|
||||
else ->
|
||||
signalError("Can't handle code ${response.code}.")
|
||||
}
|
||||
|
@ -90,13 +87,10 @@ class PageViewModel : ViewModel() {
|
|||
|
||||
private fun signalError(message: String) {
|
||||
event.postValue(FailureEvent("Error", message))
|
||||
state.postValue(State.IDLE)
|
||||
}
|
||||
|
||||
private fun handleInputResponse(response: Response, uri: Uri) {
|
||||
event.postValue(InputEvent(uri, response.meta))
|
||||
}
|
||||
|
||||
private suspend fun handleSuccessResponse(response: Response, uri: Uri) {
|
||||
private suspend fun handleRequestSuccess(response: Response, uri: Uri) {
|
||||
state.postValue(State.RECEIVING)
|
||||
|
||||
linesList.clear()
|
||||
|
@ -141,11 +135,11 @@ class PageViewModel : ViewModel() {
|
|||
state.postValue(State.IDLE)
|
||||
}
|
||||
|
||||
private fun handleRedirectResponse(response: Response, redirects: Int) {
|
||||
private fun handleRedirect(response: Response, redirects: Int) {
|
||||
event.postValue(RedirectEvent(response.meta, redirects))
|
||||
}
|
||||
|
||||
private fun handleErrorResponse(response: Response) {
|
||||
private fun handleError(response: Response) {
|
||||
val briefMessage = when (response.code) {
|
||||
Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure"
|
||||
Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dev.lowrespalmtree.comet
|
||||
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Transform a relative URI to an absolute Gemini URI
|
||||
|
@ -14,7 +15,6 @@ 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)
|
||||
.encodedAuthority(baseUri.authority)
|
||||
.authority(baseUri.authority)
|
||||
.path(newPath)
|
||||
.query(relUri.query)
|
||||
.fragment(relUri.fragment)
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
<?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.
|
@ -1,4 +0,0 @@
|
|||
<?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">
|
||||
|
||||
<Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
<com.google.android.material.appbar.CollapsingToolbarLayout
|
||||
android:id="@+id/collapsing_toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:contentScrim="?attr/colorPrimary"
|
||||
app:layout_scrollFlags="scroll|enterAlways"
|
||||
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
|
||||
app:toolbarId="@+id/toolbar">
|
||||
|
||||
<EditText
|
||||
|
@ -31,7 +31,7 @@
|
|||
android:text=""
|
||||
tools:ignore="TextContrastCheck" />
|
||||
|
||||
</Toolbar>
|
||||
</com.google.android.material.appbar.CollapsingToolbarLayout>
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
<?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:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -1,7 +1,4 @@
|
|||
<?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="0dp"
|
||||
android:textSize="8sp"
|
||||
tools:ignore="SmallSp" />
|
||||
android:layout_height="4dp" />
|
|
@ -1,8 +1,12 @@
|
|||
<?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"
|
||||
style="@style/CometText"
|
||||
android:textColor="@color/teal_700"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp"
|
||||
android:textIsSelectable="false" />
|
|
@ -1,8 +1,11 @@
|
|||
<?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:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -1,6 +1,11 @@
|
|||
<?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="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -1,19 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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="16dp">
|
||||
<TextView
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:paddingTop="4dp"
|
||||
android:paddingBottom="4dp" />
|
||||
</HorizontalScrollView>
|
||||
android:paddingTop="2dp"
|
||||
android:paddingBottom="2dp"
|
||||
android:background="@color/purple_200" />
|
|
@ -1,9 +1,12 @@
|
|||
<?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"
|
||||
style="@style/CometText"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="16dp" />
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -1,9 +1,12 @@
|
|||
<?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"
|
||||
style="@style/CometText"
|
||||
android:textSize="24sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="8dp" />
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -1,9 +1,12 @@
|
|||
<?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"
|
||||
style="@style/CometText"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:paddingTop="8dp" />
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="4dp" />
|
|
@ -6,12 +6,6 @@
|
|||
<color name="teal_200">#FF03DAC5</color>
|
||||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</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>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="text">#222222</color>
|
||||
</resources>
|
|
@ -1,12 +0,0 @@
|
|||
<?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,17 +2,15 @@
|
|||
<!-- Base application theme. -->
|
||||
<style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/orange</item>
|
||||
<item name="colorPrimaryVariant">@color/orange_dark</item>
|
||||
<item name="colorOnPrimary">@color/bright</item>
|
||||
<item name="colorPrimary">@color/purple_200</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_500</item>
|
||||
<item name="colorOnPrimary">@color/white</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_dark</item>
|
||||
<item name="colorOnSecondary">@color/text</item>
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||
<item name="colorOnSecondary">@color/black</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.4"
|
||||
classpath "com.android.tools.build:gradle:7.0.3"
|
||||
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