Skip to content

Commit

Permalink
fix smile change to img
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed May 4, 2020
1 parent 52919c8 commit 8d9b26c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
10 changes: 7 additions & 3 deletions app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.DialogInterface
import android.content.Intent
import android.provider.OpenableColumns
import android.text.Spanned
import android.text.style.ImageSpan
import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
Expand All @@ -30,7 +31,10 @@ import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.ui.view.BaseDialog
import soko.ekibun.bangumi.util.HtmlUtil
import soko.ekibun.bangumi.util.ResourceUtil
import soko.ekibun.bangumi.util.span.*
import soko.ekibun.bangumi.util.span.ClickableImageSpan
import soko.ekibun.bangumi.util.span.ClickableUrlSpan
import soko.ekibun.bangumi.util.span.CollapseUrlDrawable
import soko.ekibun.bangumi.util.span.UploadDrawable
import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -192,7 +196,7 @@ class ReplyDialog : BaseDialog(R.layout.dialog_reply) {
drawable.url = emojiList[position].second
view.item_input.editableText.insert(
view.item_input.selectionStart,
HtmlUtil.createImageSpan(UrlImageSpan(drawable, emojiList[position].first))
HtmlUtil.createImageSpan(ImageSpan(drawable, emojiList[position].first, ImageSpan.ALIGN_BASELINE))
)
drawable.container = WeakReference(view.item_input)
drawable.loadImage()
Expand Down Expand Up @@ -325,7 +329,7 @@ class ReplyDialog : BaseDialog(R.layout.dialog_reply) {
item_input.editableText.replace(start, end, "[img]$it[/img]")
}
}
val imageSpan = UrlImageSpan(drawable, "")
val imageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
span = ClickableImageSpan(imageSpan, collapseImageGetter.onClick)
item_input.editableText.insert(
item_input.selectionStart,
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ object HtmlUtil {
fun attachToTextView(span: Spanned, textView: TextView) {
// 结束之前的请求
(textView.text as? Spanned)?.let { text ->
text.getSpans(0, text.length, UrlImageSpan::class.java).filterNot {
text.getSpans(0, text.length, ImageSpan::class.java).filterNot {
span.getSpanFlags(it) == 0
}.forEach {
(it.drawable as? UrlDrawable)?.cancel()
}
}
// 更新引用
val weakRef = WeakReference(textView)
span.getSpans(0, span.length, UrlImageSpan::class.java).forEach { imageSpan ->
span.getSpans(0, span.length, ImageSpan::class.java).forEach { imageSpan ->
(imageSpan.drawable as? UrlDrawable)?.let {
it.container = weakRef
it.loadImage()
Expand Down Expand Up @@ -126,13 +126,14 @@ object HtmlUtil {
val sources = Bangumi.parseUrl(src)
val alt = node.attr("alt")
val isSmile = node.hasAttr("smileid")
val imageSpan = UrlImageSpan(
val imageSpan = ImageSpan(
imageGetter.getDrawable(sources),
if (isSmile) alt else src
if (isSmile) alt else src,
ImageSpan.ALIGN_BASELINE
)
createImageSpan(imageSpan).also {
if (!isSmile) {
imageGetter.drawables.add(imageSpan.url)
imageGetter.drawables.add(sources)
setSpan(
ClickableImageSpan(imageSpan, imageGetter.onClick),
it, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
Expand All @@ -150,7 +151,7 @@ object HtmlUtil {
return span
}

fun createImageSpan(imageSpan: UrlImageSpan): SpannableStringBuilder {
fun createImageSpan(imageSpan: ImageSpan): SpannableStringBuilder {
return SpannableStringBuilder("").also {
setSpan(imageSpan, it, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
Expand Down Expand Up @@ -205,10 +206,10 @@ object HtmlUtil {
) {
val drawables = ArrayList<String>()

open val onClick: (View, UrlImageSpan) -> Unit = { itemView, span ->
open val onClick: (View, ImageSpan) -> Unit = { itemView, span ->
PhotoPagerAdapter.showWindow(
itemView, drawables,
index = drawables.indexOf(span.url)
index = drawables.indexOf((span.drawable as UrlDrawable).url)
)
}

Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ abstract class SpanFormatter {
is ForegroundColorSpan -> return "${
String.format("[color=#%06X]", 0xFFFFFF and span.foregroundColor)}${inner()}[/color]"
is MaskSpan -> return "[mask]${inner()}[/mask]"
is UrlImageSpan -> return if (span.url.startsWith("(")) span.url
else "[img]${span.url}[/img]"
is ImageSpan -> return if (span.source?.startsWith("(") == true) span.source!!
else "[img]${span.source ?: (span.drawable as? UrlDrawable)?.url}[/img]"
}
return inner()
}
Expand All @@ -93,7 +93,10 @@ abstract class SpanFormatter {
.replace(Regex("""\[/(color|s|size)]"""), "</span>").let {
var ret = it
ReplyDialog.emojiList.forEach {
ret = ret.replace(it.first, "<img src=\"${it.second.replace(Bangumi.SERVER, "")}\"/>")
ret = ret.replace(
it.first,
"<img src=\"${it.second.replace(Bangumi.SERVER, "")}\" smileid alt=\"${it.first}\"/>"
)
}
ret
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package soko.ekibun.bangumi.util.span

import android.text.style.ClickableSpan
import android.text.style.ImageSpan
import android.util.Log
import android.view.View

/**
*
*/
class ClickableImageSpan(
var image: UrlImageSpan,
private val onClick: (View, UrlImageSpan) -> Unit
var image: ImageSpan,
private val onClick: (View, ImageSpan) -> Unit
) : ClickableSpan() {
override fun onClick(widget: View) {
Log.v("click", image.drawable.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.*
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.text.Spannable
import android.text.style.ImageSpan
import android.util.Size
import android.view.View
import android.widget.TextView
Expand Down Expand Up @@ -34,10 +35,9 @@ open class CollapseUrlDrawable(

container?.get()?.let {
val text = (it.text as? Spannable) ?: return@let
text.getSpans(0, text.length, UrlImageSpan::class.java)?.filter { span ->
text.getSpans(0, text.length, ImageSpan::class.java)?.filter { span ->
span.drawable == this
}?.forEach { span ->
span.url = url ?: ""
val start = text.getSpanStart(span)
val end = text.getSpanEnd(span)
val flags = text.getSpanFlags(span)
Expand Down Expand Up @@ -82,8 +82,11 @@ open class CollapseUrlDrawable(
class CollapseImageGetter(container: TextView) : HtmlUtil.ImageGetter(wrapWidth = {
Math.min(container.width.toFloat(), Math.max(container.textSize, it))
}) {
override val onClick: (View, UrlImageSpan) -> Unit = { itemView, span ->
Toast.makeText(itemView.context, span.url, Toast.LENGTH_LONG).show()
override val onClick: (View, ImageSpan) -> Unit = { itemView, span ->
Toast.makeText(
itemView.context,
span.source ?: (span.drawable as? UrlDrawable)?.url, Toast.LENGTH_LONG
).show()
}

override fun createDrawable(): UrlDrawable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.drawable.Animatable
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.Drawable
import android.text.Spannable
import android.text.style.ImageSpan
import android.widget.TextView
import java.lang.ref.WeakReference

Expand All @@ -25,7 +26,7 @@ open class TextViewDrawable : AnimationDrawable() {

container?.get()?.let {
val text = (it.text as? Spannable) ?: return@let
text.getSpans(0, text.length, UrlImageSpan::class.java)?.filter { span ->
text.getSpans(0, text.length, ImageSpan::class.java)?.filter { span ->
span.drawable == this
}?.forEach { span ->
val start = text.getSpanStart(span)
Expand Down

This file was deleted.

0 comments on commit 8d9b26c

Please sign in to comment.