2021-07-09 05:18:48 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.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
|
2021-06-24 07:17:12 +02:00
|
|
|
import androidx.core.content.res.ResourcesCompat
|
2019-10-11 05:41:43 +02:00
|
|
|
import network.loki.messenger.R
|
2020-01-15 02:20:10 +01:00
|
|
|
import nl.komponents.kovenant.combine.Tuple2
|
2021-05-24 02:27:31 +02:00
|
|
|
import org.session.libsession.messaging.contacts.Contact
|
2021-01-19 00:48:02 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
2021-07-09 05:18:48 +02:00
|
|
|
import org.thoughtcrime.securesms.util.UiModeUtilities
|
2019-10-11 05:41:43 +02:00
|
|
|
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 {
|
2021-06-24 07:17:12 +02:00
|
|
|
@Suppress("NAME_SHADOWING") var text = text
|
2021-10-04 09:51:19 +02:00
|
|
|
val threadDB = DatabaseComponent.get(context).threadDatabase()
|
2021-05-24 02:27:31 +02:00
|
|
|
val isOpenGroup = threadDB.getRecipientForThreadId(threadID)?.isOpenGroupRecipient ?: false
|
2019-10-11 05:41:43 +02:00
|
|
|
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
|
2021-01-19 00:48:02 +01:00
|
|
|
val userPublicKey = 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) {
|
2020-07-15 07:14:37 +02:00
|
|
|
val publicKey = text.subSequence(matcher.start() + 1, matcher.end()).toString() // +1 to get rid of the @
|
2021-05-24 02:27:31 +02:00
|
|
|
val userDisplayName: String? = if (publicKey.equals(userPublicKey, ignoreCase = true)) {
|
2019-10-11 05:41:43 +02:00
|
|
|
TextSecurePreferences.getProfileName(context)
|
|
|
|
} else {
|
2021-10-04 09:51:19 +02:00
|
|
|
val contact = DatabaseComponent.get(context).sessionContactDatabase().getContactWithSessionID(publicKey)
|
2021-06-24 07:17:12 +02:00
|
|
|
@Suppress("NAME_SHADOWING") val context = if (isOpenGroup) Contact.ContactContext.OPEN_GROUP else Contact.ContactContext.REGULAR
|
2021-05-24 02:27:31 +02:00
|
|
|
contact?.displayName(context)
|
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-07-15 07:14:37 +02:00
|
|
|
mentions.add(Tuple2(Range.create(matcher.start(), endIndex), publicKey))
|
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)
|
2021-06-24 07:17:12 +02:00
|
|
|
val isLightMode = UiModeUtilities.isDayUiMode(context)
|
2020-01-15 02:20:10 +01:00
|
|
|
for (mention in mentions) {
|
2021-06-24 07:17:12 +02:00
|
|
|
val colorID = if (isOutgoingMessage) {
|
|
|
|
if (isLightMode) R.color.white else R.color.black
|
|
|
|
} else {
|
|
|
|
R.color.accent
|
|
|
|
}
|
|
|
|
val color = ResourcesCompat.getColor(context.resources, colorID, context.theme)
|
|
|
|
result.setSpan(ForegroundColorSpan(color), mention.first.lower, mention.first.upper, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2020-01-15 02:20:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|