From d819fa9bd1f2d0137e83ae0804928b62cba3ea40 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Sat, 11 Feb 2017 16:04:11 +0800 Subject: [PATCH] improved DM item - hides text when there's only media --- .../org/mariotaku/twidere/model/SpanItem.java | 11 +++++ .../text/SafeSpannableStringBuilder.java | 28 ----------- .../twidere/text/TwidereHighLightStyle.java | 45 ------------------ .../twidere/util/TwidereStringUtils.java | 17 +------ .../adapter/MessagesConversationAdapter.kt | 11 +++++ .../extension/model/SpanItemExtensions.kt | 43 +++++++++++++++++ .../twidere/fragment/StatusFragment.kt | 39 +++++++-------- .../twidere/fragment/UserFragment.kt | 8 ++-- .../model/util/ParcelableStatusUtils.kt | 8 +--- .../twidere/text/MarkForDeleteSpan.kt} | 11 ++--- .../text/SafeSpannableStringBuilder.kt | 26 ++++++++++ .../twidere/text/TwidereHighLightStyle.kt | 38 +++++++++++++++ .../mariotaku/twidere/text/ZeroWidthSpan.kt | 38 +++++++++++++++ .../twidere/view/holder/StatusViewHolder.kt | 20 ++++---- .../view/holder/message/MessageViewHolder.kt | 47 ++++++++++++++++++- 15 files changed, 254 insertions(+), 136 deletions(-) delete mode 100644 twidere/src/main/java/org/mariotaku/twidere/text/SafeSpannableStringBuilder.java delete mode 100644 twidere/src/main/java/org/mariotaku/twidere/text/TwidereHighLightStyle.java create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/SpanItemExtensions.kt rename twidere/src/main/{java/org/mariotaku/twidere/text/MarkForDeleteSpan.java => kotlin/org/mariotaku/twidere/text/MarkForDeleteSpan.kt} (78%) create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/text/SafeSpannableStringBuilder.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/text/TwidereHighLightStyle.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/text/ZeroWidthSpan.kt diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/SpanItem.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/SpanItem.java index 5b8a85cc7..a6c25fb69 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/SpanItem.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/SpanItem.java @@ -23,6 +23,7 @@ package org.mariotaku.twidere.model; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.IntDef; import android.text.Spanned; import android.text.style.URLSpan; @@ -67,6 +68,10 @@ public class SpanItem implements Parcelable { @ParcelableNoThanks public int orig_end = -1; + @ParcelableNoThanks + @SpanType + public int type = SpanType.LINK; + @Override public String toString() { return "SpanItem{" + @@ -95,4 +100,10 @@ public class SpanItem implements Parcelable { spanItem.end = spanned.getSpanEnd(span); return spanItem; } + + @IntDef({SpanType.HIDE, SpanType.LINK}) + public @interface SpanType { + int HIDE = -1; + int LINK = 0; + } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/text/SafeSpannableStringBuilder.java b/twidere/src/main/java/org/mariotaku/twidere/text/SafeSpannableStringBuilder.java deleted file mode 100644 index ec17d4e06..000000000 --- a/twidere/src/main/java/org/mariotaku/twidere/text/SafeSpannableStringBuilder.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.mariotaku.twidere.text; - -import android.text.SpannableStringBuilder; - -import org.mariotaku.twidere.util.CheckUtils; -import org.mariotaku.twidere.util.TwidereStringUtils; - -/** - * Created by Ningyuan on 2015/5/1. - */ -public class SafeSpannableStringBuilder extends SpannableStringBuilder { - - public SafeSpannableStringBuilder(CharSequence source) { - super(source); - TwidereStringUtils.fixSHY(this); - } - - @Override - public void setSpan(Object what, int start, int end, int flags) { - if (!CheckUtils.checkRange(this, start, end)) { - // Silently ignore - return; - } - super.setSpan(what, start, end, flags); - } - - -} diff --git a/twidere/src/main/java/org/mariotaku/twidere/text/TwidereHighLightStyle.java b/twidere/src/main/java/org/mariotaku/twidere/text/TwidereHighLightStyle.java deleted file mode 100644 index 11751fe8d..000000000 --- a/twidere/src/main/java/org/mariotaku/twidere/text/TwidereHighLightStyle.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2014 Mariotaku Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.mariotaku.twidere.text; - -import android.text.TextPaint; -import android.text.style.CharacterStyle; - -import static org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT; -import static org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE; - -public class TwidereHighLightStyle extends CharacterStyle { - - private final int option; - - public TwidereHighLightStyle(final int option) { - this.option = option; - } - - @Override - public void updateDrawState(final TextPaint ds) { - if ((option & VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE) != 0) { - ds.setUnderlineText(true); - } - if ((option & VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT) != 0) { - ds.setColor(ds.linkColor); - } - } -} diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereStringUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereStringUtils.java index f327e49b2..c8a000ed9 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereStringUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereStringUtils.java @@ -19,12 +19,11 @@ package org.mariotaku.twidere.util; -import android.graphics.Canvas; -import android.graphics.Paint; import android.support.annotation.NonNull; import android.text.Spannable; import android.text.Spanned; -import android.text.style.ReplacementSpan; + +import org.mariotaku.twidere.text.ZeroWidthSpan; /** * Created by mariotaku on 14/12/23. @@ -61,16 +60,4 @@ public class TwidereStringUtils { } } - private static class ZeroWidthSpan extends ReplacementSpan { - - @Override - public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { - return 0; - } - - @Override - public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) { - - } - } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt index 977353627..d86cd9abe 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt @@ -24,11 +24,16 @@ import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.ViewGroup import org.apache.commons.lang3.time.DateUtils +import org.mariotaku.kpreferences.get import org.mariotaku.twidere.adapter.iface.IItemCountsAdapter +import org.mariotaku.twidere.annotation.PreviewStyle +import org.mariotaku.twidere.constant.linkHighlightOptionKey +import org.mariotaku.twidere.constant.mediaPreviewStyleKey import org.mariotaku.twidere.extension.model.timestamp import org.mariotaku.twidere.model.ItemCounts import org.mariotaku.twidere.model.ParcelableMessage import org.mariotaku.twidere.model.ParcelableMessage.MessageType +import org.mariotaku.twidere.util.TwidereLinkify import org.mariotaku.twidere.view.holder.message.AbsMessageViewHolder import org.mariotaku.twidere.view.holder.message.MessageViewHolder import org.mariotaku.twidere.view.holder.message.StickerMessageViewHolder @@ -38,6 +43,12 @@ class MessagesConversationAdapter(context: Context) : LoadMoreSupportAdapter? = null set(value) { field = value diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/SpanItemExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/SpanItemExtensions.kt new file mode 100644 index 000000000..beb1d1e78 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/SpanItemExtensions.kt @@ -0,0 +1,43 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.extension.model + +import android.text.Spannable +import android.text.Spanned +import android.text.style.URLSpan +import org.mariotaku.twidere.model.SpanItem +import org.mariotaku.twidere.text.ZeroWidthSpan + +val SpanItem.length: Int get() = end - start + +fun Array.applyTo(spannable: Spannable) { + forEach { span -> + when (span.type) { + SpanItem.SpanType.HIDE -> { + spannable.setSpan(ZeroWidthSpan(), span.start, span.end, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + } + else -> { + spannable.setSpan(URLSpan(span.link), span.start, span.end, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + } + } + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt index bf266b33e..5651386ce 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt @@ -91,6 +91,7 @@ import org.mariotaku.twidere.annotation.Referral import org.mariotaku.twidere.constant.* import org.mariotaku.twidere.constant.KeyboardShortcutConstants.* import org.mariotaku.twidere.extension.applyTheme +import org.mariotaku.twidere.extension.model.applyTo import org.mariotaku.twidere.extension.model.getAccountType import org.mariotaku.twidere.extension.model.media_type import org.mariotaku.twidere.loader.ConversationLoader @@ -861,7 +862,7 @@ class StatusFragment : BaseFragment(), LoaderCallbacks(fragment.context), IStatusesAdapter> { private val inflater: LayoutInflater - override val mediaLoadingHandler: MediaLoadingHandler + override val mediaLoadingHandler = MediaLoadingHandler(R.id.media_preview_progress) override val twidereLinkify: TwidereLinkify override var statusClickListener: StatusClickListener? = null @@ -1498,15 +1500,15 @@ class StatusFragment : BaseFragment(), LoaderCallbacks>(context, preferences) listener.setAdapter(this) twidereLinkify = TwidereLinkify(listener) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt index 9d424ee3a..e11592b68 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt @@ -105,6 +105,7 @@ import org.mariotaku.twidere.constant.lightFontKey import org.mariotaku.twidere.constant.newDocumentApiKey import org.mariotaku.twidere.constant.profileImageStyleKey import org.mariotaku.twidere.extension.applyTheme +import org.mariotaku.twidere.extension.model.applyTo import org.mariotaku.twidere.fragment.AbsStatusesFragment.StatusesFragmentDelegate import org.mariotaku.twidere.fragment.UserTimelineFragment.UserTimelineFragmentDelegate import org.mariotaku.twidere.fragment.iface.IBaseFragment.SystemWindowsInsetsCallback @@ -471,9 +472,10 @@ class UserFragment : BaseFragment(), OnClickListener, OnLinkClickListener, profileNameContainer.screenName.text = "@${user.screen_name}" val linkify = TwidereLinkify(this) if (user.description_unescaped != null) { - val text = SpannableStringBuilder.valueOf(user.description_unescaped) - ParcelableStatusUtils.applySpans(text, user.description_spans) - linkify.applyAllLinks(text, user.account_key, false, false) + val text = SpannableStringBuilder.valueOf(user.description_unescaped).apply { + user.description_spans?.applyTo(this) + linkify.applyAllLinks(this, user.account_key, false, false) + } descriptionContainer.description.text = text } else { descriptionContainer.description.text = user.description_plain diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt index 26ffe3584..a8b8a1e7a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt @@ -4,6 +4,7 @@ import android.text.Spannable import android.text.Spanned import android.text.style.URLSpan import org.mariotaku.microblog.library.twitter.model.Status +import org.mariotaku.twidere.extension.model.applyTo import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.ParcelableStatus.FilterFlags import org.mariotaku.twidere.util.HtmlSpanBuilder @@ -254,13 +255,6 @@ object ParcelableStatusUtils { return status.inReplyToScreenName } - fun applySpans(text: Spannable, spans: Array?) { - spans?.forEach { span -> - text.setSpan(URLSpan(span.link), span.start, span.end, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - } - } - fun updateExtraInformation(status: ParcelableStatus, details: AccountDetails) { status.account_color = details.color } diff --git a/twidere/src/main/java/org/mariotaku/twidere/text/MarkForDeleteSpan.java b/twidere/src/main/kotlin/org/mariotaku/twidere/text/MarkForDeleteSpan.kt similarity index 78% rename from twidere/src/main/java/org/mariotaku/twidere/text/MarkForDeleteSpan.java rename to twidere/src/main/kotlin/org/mariotaku/twidere/text/MarkForDeleteSpan.kt index f73c1a7b2..bb6659986 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/text/MarkForDeleteSpan.java +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/text/MarkForDeleteSpan.kt @@ -17,17 +17,16 @@ * along with this program. If not, see . */ -package org.mariotaku.twidere.text; +package org.mariotaku.twidere.text -import android.text.TextPaint; -import android.text.style.CharacterStyle; +import android.text.TextPaint +import android.text.style.CharacterStyle /** * Created by mariotaku on 15/5/11. */ -public class MarkForDeleteSpan extends CharacterStyle { - @Override - public void updateDrawState(TextPaint tp) { +class MarkForDeleteSpan : CharacterStyle() { + override fun updateDrawState(tp: TextPaint) { } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/text/SafeSpannableStringBuilder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/text/SafeSpannableStringBuilder.kt new file mode 100644 index 000000000..681b1ed1e --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/text/SafeSpannableStringBuilder.kt @@ -0,0 +1,26 @@ +package org.mariotaku.twidere.text + +import android.text.SpannableStringBuilder + +import org.mariotaku.twidere.util.CheckUtils +import org.mariotaku.twidere.util.TwidereStringUtils + +/** + * Created by Ningyuan on 2015/5/1. + */ +class SafeSpannableStringBuilder(source: CharSequence) : SpannableStringBuilder(source) { + + init { + TwidereStringUtils.fixSHY(this) + } + + override fun setSpan(what: Any, start: Int, end: Int, flags: Int) { + if (!CheckUtils.checkRange(this, start, end)) { + // Silently ignore + return + } + super.setSpan(what, start, end, flags) + } + + +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/text/TwidereHighLightStyle.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/text/TwidereHighLightStyle.kt new file mode 100644 index 000000000..5d20b8fad --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/text/TwidereHighLightStyle.kt @@ -0,0 +1,38 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.text + +import android.text.TextPaint +import android.text.style.CharacterStyle + +import org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT +import org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE + +class TwidereHighLightStyle(private val option: Int) : CharacterStyle() { + + override fun updateDrawState(ds: TextPaint) { + if (option and VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE != 0) { + ds.isUnderlineText = true + } + if (option and VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT != 0) { + ds.color = ds.linkColor + } + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/text/ZeroWidthSpan.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/text/ZeroWidthSpan.kt new file mode 100644 index 000000000..86815ca19 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/text/ZeroWidthSpan.kt @@ -0,0 +1,38 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.text + +import android.graphics.Canvas +import android.graphics.Paint +import android.text.style.ReplacementSpan + +/** + * Created by mariotaku on 2017/2/11. + */ +class ZeroWidthSpan : ReplacementSpan() { + + override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int { + return 0 + } + + override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) { + + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt index 24acaef3d..6cce72372 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt @@ -3,7 +3,10 @@ package org.mariotaku.twidere.view.holder import android.support.v4.content.ContextCompat import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView.ViewHolder -import android.text.* +import android.text.SpannableString +import android.text.SpannableStringBuilder +import android.text.Spanned +import android.text.TextUtils import android.text.style.ForegroundColorSpan import android.view.View import android.view.View.OnClickListener @@ -16,13 +19,13 @@ import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.USER_TYPE_FANFOU_COM import org.mariotaku.twidere.adapter.iface.IStatusesAdapter import org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE +import org.mariotaku.twidere.extension.model.applyTo import org.mariotaku.twidere.graphic.like.LikeAnimationDrawable import org.mariotaku.twidere.model.ParcelableLocation import org.mariotaku.twidere.model.ParcelableMedia import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.util.ParcelableLocationUtils -import org.mariotaku.twidere.model.util.ParcelableStatusUtils import org.mariotaku.twidere.task.CreateFavoriteTask import org.mariotaku.twidere.task.DestroyFavoriteTask import org.mariotaku.twidere.task.RetweetStatusTask @@ -208,7 +211,7 @@ class StatusViewHolder(private val adapter: IStatusesAdapter<*>, itemView: View) val quotedText: CharSequence if (adapter.linkHighlightingStyle != VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) { quotedText = SpannableStringBuilder.valueOf(status.quoted_text_unescaped) - ParcelableStatusUtils.applySpans(quotedText as Spannable, status.quoted_spans) + status.quoted_spans?.applyTo(quotedText) linkify.applyAllLinks(quotedText, status.account_key, layoutPosition.toLong(), status.is_possibly_sensitive, adapter.linkHighlightingStyle, skipLinksInText) @@ -344,11 +347,12 @@ class StatusViewHolder(private val adapter: IStatusesAdapter<*>, itemView: View) val text: CharSequence if (adapter.linkHighlightingStyle != VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) { - text = SpannableStringBuilder.valueOf(status.text_unescaped) - ParcelableStatusUtils.applySpans(text as Spannable, status.spans) - linkify.applyAllLinks(text, status.account_key, layoutPosition.toLong(), - status.is_possibly_sensitive, adapter.linkHighlightingStyle, - skipLinksInText) + text = SpannableStringBuilder.valueOf(status.text_unescaped).apply { + status.spans?.applyTo(this) + linkify.applyAllLinks(this, status.account_key, layoutPosition.toLong(), + status.is_possibly_sensitive, adapter.linkHighlightingStyle, + skipLinksInText) + } } else { text = status.text_unescaped } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageViewHolder.kt index 8a5f9c6b8..889f3ba32 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageViewHolder.kt @@ -20,17 +20,21 @@ package org.mariotaku.twidere.view.holder.message import android.support.v4.view.GravityCompat +import android.text.SpannableStringBuilder import android.text.format.DateUtils import android.view.View import android.widget.FrameLayout import android.widget.LinearLayout import kotlinx.android.synthetic.main.list_item_message_conversation_text.view.* +import org.mariotaku.ktextension.empty import org.mariotaku.ktextension.isNullOrEmpty import org.mariotaku.messagebubbleview.library.MessageBubbleView import org.mariotaku.twidere.R import org.mariotaku.twidere.adapter.MessagesConversationAdapter +import org.mariotaku.twidere.extension.model.applyTo import org.mariotaku.twidere.extension.model.timestamp import org.mariotaku.twidere.model.ParcelableMessage +import org.mariotaku.twidere.model.SpanItem /** * Created by mariotaku on 2017/2/9. @@ -49,12 +53,12 @@ class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : text.textSize = textSize time.textSize = textSize * 0.8f date.textSize = textSize * 0.9f + mediaPreview.style = adapter.mediaPreviewStyle } override fun display(message: ParcelableMessage, showDate: Boolean) { super.display(message, showDate) setOutgoingStatus(messageContent, message.is_outgoing) - text.text = message.text_unescaped if (showDate) { date.visibility = View.VISIBLE date.text = DateUtils.getRelativeTimeSpanString(message.timestamp, System.currentTimeMillis(), @@ -62,8 +66,49 @@ class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : } else { date.visibility = View.GONE } + + + // Loop through text and spans to found non-space char count + val hideText = run { + + fun String.nonSpaceCount(range: IntRange): Int { + if (range.isEmpty()) return 0 + return range.count { !Character.isSpaceChar(this[it]) } + } + + val text = message.text_unescaped + + var nonSpaceCount = 0 + var curPos = 0 + message.spans?.forEach { span -> + if (message.media?.firstOrNull { media -> span.link == media.url } != null) { + // Skip if span is hidden + span.type = SpanItem.SpanType.HIDE + } else { + nonSpaceCount += text.nonSpaceCount(curPos until span.end) + } + curPos = span.end + } + nonSpaceCount += text.nonSpaceCount(curPos..text.lastIndex) + return@run nonSpaceCount == 0 + } + + + text.text = SpannableStringBuilder.valueOf(message.text_unescaped).apply { + message.spans?.applyTo(this) + adapter.linkify.applyAllLinks(this, message.account_key, layoutPosition.toLong(), + false, adapter.linkHighlightingStyle, true) + } + + text.visibility = if (hideText || text.empty) { + View.GONE + } else { + View.VISIBLE + } + time.text = DateUtils.formatDateTime(adapter.context, message.timestamp, DateUtils.FORMAT_SHOW_TIME) + if (message.media.isNullOrEmpty()) { mediaPreview.visibility = View.GONE } else {