session-android/src/org/thoughtcrime/securesms/loki/utilities/MentionUtilities.kt

61 lines
3 KiB
Kotlin
Raw Normal View History

2020-05-11 08:19:26 +02:00
package org.thoughtcrime.securesms.loki.utilities
2019-10-11 05:41:43 +02:00
import android.content.Context
2020-01-15 02:20:10 +01:00
import android.graphics.Typeface
2019-10-11 05:41:43 +02:00
import android.text.Spannable
import android.text.SpannableString
2020-01-15 02:20:10 +01:00
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
2019-10-11 05:41:43 +02:00
import android.util.Range
import network.loki.messenger.R
2020-01-15 02:20:10 +01:00
import nl.komponents.kovenant.combine.Tuple2
2019-10-11 05:41:43 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.util.TextSecurePreferences
import java.util.regex.Pattern
object MentionUtilities {
@JvmStatic
2019-10-15 04:39:17 +02:00
fun highlightMentions(text: CharSequence, threadID: Long, context: Context): String {
2020-01-15 02:20:10 +01:00
return highlightMentions(text, false, threadID, context).toString() // isOutgoingMessage is irrelevant
2019-10-11 05:41:43 +02:00
}
@JvmStatic
2019-10-15 04:39:17 +02:00
fun highlightMentions(text: CharSequence, isOutgoingMessage: Boolean, threadID: Long, context: Context): SpannableString {
2019-10-11 05:41:43 +02:00
var text = text
val pattern = Pattern.compile("@[0-9a-fA-F]*")
var matcher = pattern.matcher(text)
2020-01-15 02:20:10 +01:00
val mentions = mutableListOf<Tuple2<Range<Int>, String>>()
2019-10-11 05:41:43 +02:00
var startIndex = 0
2019-10-15 04:39:17 +02:00
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
2020-01-15 02:20:10 +01:00
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
2019-10-15 04:39:17 +02:00
if (matcher.find(startIndex)) {
2019-10-11 05:41:43 +02:00
while (true) {
2019-10-11 07:37:28 +02:00
val hexEncodedPublicKey = text.subSequence(matcher.start() + 1, matcher.end()).toString() // +1 to get rid of the @
2020-01-15 02:20:10 +01:00
val userDisplayName: String? = if (hexEncodedPublicKey.toLowerCase() == userHexEncodedPublicKey.toLowerCase()) {
2019-10-11 05:41:43 +02:00
TextSecurePreferences.getProfileName(context)
2019-10-15 04:39:17 +02:00
} else if (publicChat != null) {
DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.id, hexEncodedPublicKey)
2019-10-11 05:41:43 +02:00
} else {
DatabaseFactory.getLokiUserDatabase(context).getDisplayName(hexEncodedPublicKey)
2019-10-11 05:41:43 +02:00
}
if (userDisplayName != null) {
text = text.subSequence(0, matcher.start()).toString() + "@" + userDisplayName + text.subSequence(matcher.end(), text.length)
val endIndex = matcher.start() + 1 + userDisplayName.length
startIndex = endIndex
2020-01-15 02:20:10 +01:00
mentions.add(Tuple2(Range.create(matcher.start(), endIndex), hexEncodedPublicKey))
2019-10-11 05:41:43 +02:00
} else {
startIndex = matcher.end()
}
matcher = pattern.matcher(text)
if (!matcher.find(startIndex)) { break }
}
}
val result = SpannableString(text)
2020-01-15 02:20:10 +01:00
for (mention in mentions) {
result.setSpan(ForegroundColorSpan(context.resources.getColorWithID(R.color.accent, context.theme)), mention.first.lower, mention.first.upper, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
result.setSpan(StyleSpan(Typeface.BOLD), mention.first.lower, mention.first.upper, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
2019-10-11 05:41:43 +02:00
}
return result
}
}