mirror of https://git.sr.ht/~oppen/ariane
inline image improvements
This commit is contained in:
parent
fe16f605a0
commit
c5b4bd07b7
|
@ -1,5 +1,6 @@
|
|||
package oppen
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.CountDownTimer
|
||||
import android.view.View
|
||||
|
@ -27,6 +28,14 @@ fun String.toURI(): URI {
|
|||
return URI.create(this)
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
fun String.endsWithImage(): Boolean{
|
||||
return this.toLowerCase().endsWith(".png") ||
|
||||
this.toLowerCase().endsWith(".jpg") ||
|
||||
this.toLowerCase().endsWith(".jpeg") ||
|
||||
this.toLowerCase().endsWith(".gif")
|
||||
}
|
||||
|
||||
fun delay(ms: Long, action: () -> Unit){
|
||||
object : CountDownTimer(ms, ms/2) {
|
||||
override fun onTick(millisUntilFinished: Long) {}
|
||||
|
|
|
@ -8,7 +8,5 @@ class Ariane: Application() {
|
|||
const val DEFAULT_HOME_CAPSULE = "gemini://gemini.circumlunar.space/~oppen/index.gmi"
|
||||
const val GEMINI_USER_SEARCH_BASE = "gemini://gus.guru/search?"
|
||||
const val GEMINI_BACKLINK_BASE = "gemini://gus.guru/backlinks?"
|
||||
|
||||
const val FEATURE_INLINE_IMAGES = true
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import kotlinx.android.synthetic.main.gemtext_code_block.view.*
|
|||
import kotlinx.android.synthetic.main.gemtext_link.view.*
|
||||
import kotlinx.android.synthetic.main.gemtext_text.view.gemtext_text_textview
|
||||
import oppen.ariane.R
|
||||
import oppen.endsWithImage
|
||||
import oppen.visible
|
||||
import java.net.URI
|
||||
|
||||
|
@ -23,9 +24,10 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
private val typeH2 = 2
|
||||
private val typeH3 = 3
|
||||
private val typeListItem = 4
|
||||
private val typeLink = 5
|
||||
private val typeCodeBlock = 6
|
||||
private val typeQuote = 7
|
||||
private val typeImageLink = 5
|
||||
private val typeLink = 6
|
||||
private val typeCodeBlock = 7
|
||||
private val typeQuote = 8
|
||||
|
||||
sealed class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
|
||||
class Text(itemView: View): ViewHolder(itemView)
|
||||
|
@ -33,6 +35,7 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
class H2(itemView: View): ViewHolder(itemView)
|
||||
class H3(itemView: View): ViewHolder(itemView)
|
||||
class ListItem(itemView: View): ViewHolder(itemView)
|
||||
class ImageLinkLink(itemView: View): ViewHolder(itemView)
|
||||
class Link(itemView: View): ViewHolder(itemView)
|
||||
class Code(itemView: View): ViewHolder(itemView)
|
||||
class Quote(itemView: View): ViewHolder(itemView)
|
||||
|
@ -52,6 +55,7 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
typeH2 -> ViewHolder.H2(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_h2, parent, false))
|
||||
typeH3 -> ViewHolder.H3(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_h3, parent, false))
|
||||
typeListItem -> ViewHolder.ListItem(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_text, parent, false))
|
||||
typeImageLink -> ViewHolder.Link(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_image_link, parent, false))
|
||||
typeLink -> ViewHolder.Link(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_link, parent, false))
|
||||
typeCodeBlock-> ViewHolder.Code(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_code_block, parent, false))
|
||||
typeQuote -> ViewHolder.Quote(LayoutInflater.from(parent.context).inflate(R.layout.gemtext_quote, parent, false))
|
||||
|
@ -67,6 +71,7 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
line.startsWith("##") -> typeH2
|
||||
line.startsWith("#") -> typeH1
|
||||
line.startsWith("*") -> typeListItem
|
||||
line.startsWith("=>") && line.endsWithImage() -> typeImageLink
|
||||
line.startsWith("=>") -> typeLink
|
||||
line.startsWith(">") -> typeQuote
|
||||
else -> typeText
|
||||
|
@ -113,13 +118,36 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
holder.itemView.gemtext_text_link.paint.isUnderlineText = true
|
||||
holder.itemView.gemtext_text_link.setOnClickListener {
|
||||
val uri = getUri(lines[holder.adapterPosition])
|
||||
println("User click link: $uri")
|
||||
println("User clicked link: $uri")
|
||||
onLink(this, uri, false, holder.adapterPosition, null)
|
||||
|
||||
}
|
||||
holder.itemView.gemtext_text_link.setOnLongClickListener {view ->
|
||||
val uri = getUri(lines[holder.adapterPosition])
|
||||
println("User click link: $uri")
|
||||
println("User long-clicked link: $uri")
|
||||
onLink(this, uri, true, holder.adapterPosition, view)
|
||||
true
|
||||
}
|
||||
}
|
||||
is ViewHolder.ImageLinkLink -> {
|
||||
//todo - extract, largely duplicate of above
|
||||
val linkParts = line.substring(2).trim().split("\\s+".toRegex(), 2)
|
||||
var linkName = linkParts[0]
|
||||
if(linkParts.size > 1) {
|
||||
linkName = linkParts[1]
|
||||
}
|
||||
val displayText = linkName
|
||||
holder.itemView.gemtext_text_link.text = displayText
|
||||
holder.itemView.gemtext_text_link.paint.isUnderlineText = true
|
||||
holder.itemView.gemtext_text_link.setOnClickListener {
|
||||
val uri = getUri(lines[holder.adapterPosition])
|
||||
println("User clicked link: $uri")
|
||||
onLink(this, uri, false, holder.adapterPosition, null)
|
||||
|
||||
}
|
||||
holder.itemView.gemtext_text_link.setOnLongClickListener {view ->
|
||||
val uri = getUri(lines[holder.adapterPosition])
|
||||
println("User long-clicked link: $uri")
|
||||
onLink(this, uri, true, holder.adapterPosition, view)
|
||||
true
|
||||
}
|
||||
|
@ -127,6 +155,8 @@ class GemtextAdapter(val onLink: (adapter: GemtextAdapter, link: URI, longTap: B
|
|||
if(inlineImages.containsKey(position)){
|
||||
holder.itemView.gemtext_inline_image.visible(true)
|
||||
holder.itemView.gemtext_inline_image.setImageURI(inlineImages[position])
|
||||
}else{
|
||||
holder.itemView.gemtext_inline_image.visible(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,32 +3,28 @@ package oppen.ariane.ui.modals_menus
|
|||
import android.view.MenuInflater
|
||||
import android.view.View
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
import oppen.ariane.Ariane
|
||||
import oppen.ariane.R
|
||||
import oppen.endsWithImage
|
||||
import java.net.URI
|
||||
|
||||
|
||||
object LinkPopup {
|
||||
|
||||
fun show(view: View?, uri: URI, onMenuOption: (menuId: Int) -> Unit){
|
||||
if(view != null) {
|
||||
|
||||
val popup = PopupMenu(view.context, view)
|
||||
val inflater: MenuInflater = popup.menuInflater
|
||||
|
||||
val path = uri.toString().toLowerCase()
|
||||
if(Ariane.FEATURE_INLINE_IMAGES &&
|
||||
(path.endsWith(".png") ||
|
||||
path.endsWith(".jpg") ||
|
||||
path.endsWith(".jpeg"))){
|
||||
inflater.inflate(R.menu.image_link_menu, popup.menu)
|
||||
}else{
|
||||
inflater.inflate(R.menu.link_menu, popup.menu)
|
||||
when {
|
||||
uri.toString().endsWithImage() -> inflater.inflate(R.menu.image_link_menu, popup.menu)
|
||||
else -> inflater.inflate(R.menu.link_menu, popup.menu)
|
||||
}
|
||||
|
||||
popup.setOnMenuItemClickListener { menuItem ->
|
||||
onMenuOption(menuItem.itemId)
|
||||
true
|
||||
}
|
||||
|
||||
popup.show()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/gemtext_text_link"
|
||||
android:textColor="@color/stroke"
|
||||
android:textSize="@dimen/default_text_size"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:layout_marginLeft="@dimen/screen_margin"
|
||||
android:layout_marginRight="@dimen/screen_margin"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/gemtext_inline_image"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/screen_margin"
|
||||
android:layout_marginRight="@dimen/screen_margin"
|
||||
android:layout_marginTop="@dimen/default_margin"
|
||||
android:visibility="gone"
|
||||
android:adjustViewBounds="true"
|
||||
android:layout_below="@+id/gemtext_text_link"/>
|
||||
</RelativeLayout>
|
Loading…
Reference in New Issue