Show text with only few emojis in bigger

This commit is contained in:
Valere 2019-06-06 11:54:08 +02:00
parent e22b555b58
commit 834a865dfa
3 changed files with 90 additions and 5 deletions

View file

@ -28,4 +28,12 @@ object DimensionUtils {
context.resources.displayMetrics
).toInt()
}
fun spToPx(sp: Int, context: Context): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp.toFloat(),
context.resources.displayMetrics
).toInt()
}
}

View file

@ -0,0 +1,69 @@
package im.vector.riotredesign.core.utils
import java.util.regex.Pattern
private val emojisPattern = Pattern.compile("((?:[\uD83C\uDF00-\uD83D\uDDFF]" +
"|[\uD83E\uDD00-\uD83E\uDDFF]" +
"|[\uD83D\uDE00-\uD83D\uDE4F]" +
"|[\uD83D\uDE80-\uD83D\uDEFF]" +
"|[\u2600-\u26FF]\uFE0F?" +
"|[\u2700-\u27BF]\uFE0F?" +
"|\u24C2\uFE0F?" +
"|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}" +
"|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?" +
"|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3" +
"|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?" +
"|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?" +
"|[\u2934\u2935]\uFE0F?" +
"|[\u3030\u303D]\uFE0F?" +
"|[\u3297\u3299]\uFE0F?" +
"|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?" +
"|[\u203C\u2049]\uFE0F?" +
"|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?" +
"|[\u00A9\u00AE]\uFE0F?" +
"|[\u2122\u2139]\uFE0F?" +
"|\uD83C\uDC04\uFE0F?" +
"|\uD83C\uDCCF\uFE0F?" +
"|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?))")
/**
* Test if a string contains emojis.
* It seems that the regex [emoji_regex]+ does not work.
* Some characters like ?, # or digit are accepted.
*
* @param str the body to test
* @return true if the body contains only emojis
*/
fun containsOnlyEmojis(str: String?): Boolean {
var res = false
if (str != null && str.isNotEmpty()) {
val matcher = emojisPattern.matcher(str)
var start = -1
var end = -1
while (matcher.find()) {
val nextStart = matcher.start()
// first emoji position
if (start < 0) {
if (nextStart > 0) {
return false
}
} else {
// must not have a character between
if (nextStart != end) {
return false
}
}
start = nextStart
end = matcher.end()
}
res = -1 != start && end == str.length
}
return res
}

View file

@ -16,10 +16,7 @@
package im.vector.riotredesign.features.home.room.detail.timeline.item
import android.text.Spannable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.text.PrecomputedTextCompat
import androidx.core.text.toSpannable
@ -28,6 +25,8 @@ import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.matrix.android.api.permalinks.MatrixLinkify
import im.vector.riotredesign.R
import im.vector.riotredesign.core.utils.DimensionUtils
import im.vector.riotredesign.core.utils.containsOnlyEmojis
import im.vector.riotredesign.features.html.PillImageSpan
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@ -47,12 +46,21 @@ abstract class MessageTextItem : AbsMessageItem<MessageTextItem.Holder>() {
override fun bind(holder: Holder) {
super.bind(holder)
MatrixLinkify.addLinkMovementMethod(holder.messageView)
val textFuture = PrecomputedTextCompat.getTextFuture(message ?: "",
val msg = message ?: ""
if (msg.length <= 4 && containsOnlyEmojis(msg.toString())) {
holder.messageView.textSize = 44F
} else {
holder.messageView.textSize = 14F
}
val textFuture = PrecomputedTextCompat.getTextFuture(msg,
TextViewCompat.getTextMetricsParams(holder.messageView),
null)
holder.messageView.setTextFuture(textFuture)
holder.messageView.renderSendState()
holder.messageView.setOnClickListener (clickListener)
holder.messageView.setOnClickListener(clickListener)
holder.messageView.setOnLongClickListener(longClickListener)
findPillsAndProcess { it.bind(holder.messageView) }
}