Mentions matching

This commit is contained in:
2021-02-21 01:38:08 +03:00
parent 34e5258241
commit 662f1333c2
8 changed files with 50 additions and 8 deletions
@@ -392,7 +392,7 @@ class MessagesAdapter(
if (message.isOutbox()) R.attr.messageOutTextColor else R.attr.messageInTextColor
)
)
text.text = VKUtil.matchMentions(message.text)
text.text = message.text
}
fun prepareDate(message: VKMessage, date: TextView) {
@@ -2,6 +2,7 @@ package com.meloda.fast.api.model
import android.util.ArrayMap
import androidx.room.*
import com.meloda.fast.api.util.VKUtil
import com.meloda.fast.database.dao.converters.ArrayListToByteArrayConverter
import com.meloda.fast.database.dao.converters.ForwardedConverter
import org.json.JSONObject
@@ -125,7 +126,9 @@ open class VKMessage() : VKModel() {
fromId = o.optInt("from_id", -1)
editTime = o.optInt("edit_time", -1)
isOut = o.optInt("out") == 1
text = o.optString("text")
text = VKUtil.prepareMessageText(o.optString("text"))
randomId = o.optInt("random_id", -1)
conversationMessageId = o.optInt("conversation_message_id", -1)
isImportant = o.optBoolean("important")
@@ -80,8 +80,46 @@ object VKUtil {
return values
}
fun matchMentions(text: String): String {
return text
fun prepareMessageText(message: String): String {
if (message.isEmpty()) return message
var newText = message
val mentions = hashMapOf<String, String>()
var startFrom = 0
while (true) {
val leftBracketIndex = newText.indexOf('[', startFrom)
val verticalLineIndex = newText.indexOf('|', startFrom)
val rightBracketIndex = newText.indexOf(']', startFrom)
if (leftBracketIndex == -1 ||
verticalLineIndex == -1 ||
rightBracketIndex == -1
) {
break
}
val id = newText.substring(leftBracketIndex + 1, verticalLineIndex)
if (!id.matches(Regex("^id(\\d+)\$")) || rightBracketIndex - verticalLineIndex < 2) {
break
}
val text = newText.substring(verticalLineIndex + 1, rightBracketIndex)
val str = "[$id|$text]"
mentions[str] = text
startFrom = rightBracketIndex + 1
}
mentions.forEach {
newText = newText.replace(it.key, it.value)
}
return newText
}
// fun removeTime(date: Date): Long {
@@ -74,7 +74,7 @@ class FragmentConversations : BaseFragment(), ConversationsView {
private fun prepareToolbar() {
initToolbar(R.id.toolbar)
toolbar.title = getString(R.string.navigation_conversations)
toolbar.title = getString(R.string.navigation_chats)
setProfileAvatar()
TaskManager.addOnEventListener(object : TaskManager.OnEventListener {