@ -4,18 +4,24 @@ import android.content.Context
import android.graphics.Typeface
import android.graphics.Typeface
import android.text.Spannable
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableString
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.text.style.StyleSpan
import android.util.Range
import android.util.Range
import androidx.appcompat.widget.ThemeUtils
import androidx.core.content.res.ResourcesCompat
import androidx.core.content.res.ResourcesCompat
import network.loki.messenger.R
import network.loki.messenger.R
import nl.komponents.kovenant.combine.Tuple2
import nl.komponents.kovenant.combine.Tuple2
import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.utilities.SodiumUtilities
import org.session.libsession.messaging.utilities.SodiumUtilities
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.ThemeUtil
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.util.UiModeUtilities
import org.thoughtcrime.securesms.util.UiModeUtilities
import org.thoughtcrime.securesms.util.getAccentColor
import org.thoughtcrime.securesms.util.getAccentColor
import org.thoughtcrime.securesms.util.getColorResourceIdFromAttr
import org.thoughtcrime.securesms.util.getMessageTextColourAttr
import java.util.regex.Pattern
import java.util.regex.Pattern
object MentionUtilities {
object MentionUtilities {
@ -58,15 +64,37 @@ object MentionUtilities {
}
}
}
}
val result = SpannableString ( text )
val result = SpannableString ( text )
val isLightMode = UiModeUtilities . isDayUiMode ( context )
val color = if ( isOutgoingMessage ) {
var mentionTextColour : Int ? = null
ResourcesCompat . getColor ( context . resources , if ( isLightMode ) R . color . white else R . color . black , context . theme )
// In dark themes..
} else {
if ( ThemeUtil . isDarkTheme ( context ) ) {
context . getAccentColor ( )
// ..we use the standard outgoing message colour for outgoing messages..
if ( isOutgoingMessage ) {
val mentionTextColourAttributeId = getMessageTextColourAttr ( true )
val mentionTextColourResourceId = getColorResourceIdFromAttr ( context , mentionTextColourAttributeId )
mentionTextColour = ResourcesCompat . getColor ( context . resources , mentionTextColourResourceId , context . theme )
}
else // ..but we use the accent colour for incoming messages (i.e., someone mentioning us)..
{
mentionTextColour = context . getAccentColor ( )
}
}
else // ..while in light themes we always just use the incoming or outgoing message text colour for mentions.
{
val mentionTextColourAttributeId = getMessageTextColourAttr ( isOutgoingMessage )
val mentionTextColourResourceId = getColorResourceIdFromAttr ( context , mentionTextColourAttributeId )
mentionTextColour = ResourcesCompat . getColor ( context . resources , mentionTextColourResourceId , context . theme )
}
}
for ( mention in mentions ) {
for ( mention in mentions ) {
result . setSpan ( ForegroundColorSpan ( color ) , mention . first . lower , mention . first . upper , Spannable . SPAN _EXCLUSIVE _EXCLUSIVE )
result . setSpan ( ForegroundColorSpan ( mentionTextColou r) , 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 )
result . setSpan ( StyleSpan ( Typeface . BOLD ) , mention . first . lower , mention . first . upper , Spannable . SPAN _EXCLUSIVE _EXCLUSIVE )
// If we're using a light theme then we change the background colour of the mention to be the accent colour
if ( ThemeUtil . isLightTheme ( context ) ) {
val backgroundColour = context . getAccentColor ( ) ;
result . setSpan ( BackgroundColorSpan ( backgroundColour ) , mention . first . lower , mention . first . upper , Spannable . SPAN _EXCLUSIVE _EXCLUSIVE )
}
}
}
return result
return result
}
}