From 8d9b26c877b243cbcb4e41266539226b474750e7 Mon Sep 17 00:00:00 2001 From: ekibun Date: Mon, 4 May 2020 23:34:12 +0800 Subject: [PATCH] fix smile change to img --- .../soko/ekibun/bangumi/ui/topic/ReplyDialog.kt | 10 +++++++--- .../java/soko/ekibun/bangumi/util/HtmlUtil.kt | 17 +++++++++-------- .../soko/ekibun/bangumi/util/SpanFormatter.kt | 9 ++++++--- .../bangumi/util/span/ClickableImageSpan.kt | 5 +++-- .../bangumi/util/span/CollapseUrlDrawable.kt | 11 +++++++---- .../bangumi/util/span/TextViewDrawable.kt | 3 ++- .../ekibun/bangumi/util/span/UrlImageSpan.kt | 9 --------- 7 files changed, 34 insertions(+), 30 deletions(-) delete mode 100644 app/src/main/java/soko/ekibun/bangumi/util/span/UrlImageSpan.kt diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt b/app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt index e31c3a8..599a5f4 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/topic/ReplyDialog.kt @@ -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 @@ -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 /** @@ -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() @@ -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, diff --git a/app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt b/app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt index e7f5ba3..1ab4940 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/HtmlUtil.kt @@ -70,7 +70,7 @@ 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() @@ -78,7 +78,7 @@ object HtmlUtil { } // 更新引用 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() @@ -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 @@ -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) } @@ -205,10 +206,10 @@ object HtmlUtil { ) { val drawables = ArrayList() - 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) ) } diff --git a/app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt b/app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt index 3aa9036..d43fb73 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/SpanFormatter.kt @@ -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() } @@ -93,7 +93,10 @@ abstract class SpanFormatter { .replace(Regex("""\[/(color|s|size)]"""), "").let { var ret = it ReplyDialog.emojiList.forEach { - ret = ret.replace(it.first, "") + ret = ret.replace( + it.first, + "\"${it.first}\"/" + ) } ret } diff --git a/app/src/main/java/soko/ekibun/bangumi/util/span/ClickableImageSpan.kt b/app/src/main/java/soko/ekibun/bangumi/util/span/ClickableImageSpan.kt index db90c8b..3e5bc5c 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/span/ClickableImageSpan.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/span/ClickableImageSpan.kt @@ -1,6 +1,7 @@ package soko.ekibun.bangumi.util.span import android.text.style.ClickableSpan +import android.text.style.ImageSpan import android.util.Log import android.view.View @@ -8,8 +9,8 @@ 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()) diff --git a/app/src/main/java/soko/ekibun/bangumi/util/span/CollapseUrlDrawable.kt b/app/src/main/java/soko/ekibun/bangumi/util/span/CollapseUrlDrawable.kt index 27930ed..8716d85 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/span/CollapseUrlDrawable.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/span/CollapseUrlDrawable.kt @@ -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 @@ -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) @@ -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 { diff --git a/app/src/main/java/soko/ekibun/bangumi/util/span/TextViewDrawable.kt b/app/src/main/java/soko/ekibun/bangumi/util/span/TextViewDrawable.kt index b439024..a7f738e 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/span/TextViewDrawable.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/span/TextViewDrawable.kt @@ -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 @@ -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) diff --git a/app/src/main/java/soko/ekibun/bangumi/util/span/UrlImageSpan.kt b/app/src/main/java/soko/ekibun/bangumi/util/span/UrlImageSpan.kt deleted file mode 100644 index 5ce2302..0000000 --- a/app/src/main/java/soko/ekibun/bangumi/util/span/UrlImageSpan.kt +++ /dev/null @@ -1,9 +0,0 @@ -package soko.ekibun.bangumi.util.span - -import android.text.style.ImageSpan - -class UrlImageSpan(drawable: UrlDrawable, var url: String = "") : ImageSpan(drawable, "", ALIGN_BASELINE) { - - @Deprecated("use url instead", ReplaceWith("url")) - override fun getSource(): String? = url -} \ No newline at end of file