Commit a09dc8fd authored by Leonardo Aramaki's avatar Leonardo Aramaki

Fix mentions' styles

parent 06565722
...@@ -2,18 +2,14 @@ package chat.rocket.android.helper ...@@ -2,18 +2,14 @@ package chat.rocket.android.helper
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.*
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import android.support.v4.content.res.ResourcesCompat import android.support.v4.content.res.ResourcesCompat
import android.text.Layout import android.text.Layout
import android.text.Spannable import android.text.Spannable
import android.text.Spanned import android.text.Spanned
import android.text.TextPaint
import android.text.style.* import android.text.style.*
import android.view.View
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.chatroom.viewmodel.MessageViewModel import chat.rocket.android.chatroom.viewmodel.MessageViewModel
import org.commonmark.node.BlockQuote import org.commonmark.node.BlockQuote
...@@ -31,6 +27,7 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -31,6 +27,7 @@ class MessageParser @Inject constructor(val context: Application, private val co
Pattern.MULTILINE or Pattern.CASE_INSENSITIVE) Pattern.MULTILINE or Pattern.CASE_INSENSITIVE)
private val regexLink = Pattern.compile("(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&/=]*)", private val regexLink = Pattern.compile("(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&/=]*)",
Pattern.MULTILINE or Pattern.CASE_INSENSITIVE) Pattern.MULTILINE or Pattern.CASE_INSENSITIVE)
private val selfReferList = listOf("@all", "@here")
/** /**
* Render a markdown text message to Spannable. * Render a markdown text message to Spannable.
...@@ -73,17 +70,42 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -73,17 +70,42 @@ class MessageParser @Inject constructor(val context: Application, private val co
} }
private fun applySpans(text: CharSequence, currentUser: String?) { private fun applySpans(text: CharSequence, currentUser: String?) {
if (text !is Spannable) return
applyMentionSpans(text, currentUser)
}
private fun applyMentionSpans(text: CharSequence, currentUser: String?) {
val matcher = regexUsername.matcher(text) val matcher = regexUsername.matcher(text)
val result = text as Spannable val result = text as Spannable
while (matcher.find()) { while (matcher.find()) {
val user = matcher.group(2) val user = matcher.group(2)
val start = matcher.start(2) val start = matcher.start(2)
//TODO: should check if username actually exists prior to applying. //TODO: should check if username actually exists prior to applying.
val linkColor = ResourcesCompat.getColor(context.resources, R.color.white, null) with(context) {
val linkBackgroundColor = ResourcesCompat.getColor(context.resources, R.color.colorAccent, null) val referSelf = when (user) {
val referSelf = currentUser != null && "@$currentUser" == user in selfReferList -> true
val usernameSpan = UsernameClickableSpan(linkBackgroundColor, linkColor, referSelf) "@$currentUser" -> true
result.setSpan(usernameSpan, start, start + user.length, 0) else -> false
}
val mentionTextColor: Int
val mentionBgColor: Int
if (referSelf) {
mentionTextColor = ResourcesCompat.getColor(resources, R.color.white, theme)
mentionBgColor = ResourcesCompat.getColor(context.resources,
R.color.colorAccent, theme)
} else {
mentionTextColor = ResourcesCompat.getColor(resources, R.color.colorAccent,
theme)
mentionBgColor = ResourcesCompat.getColor(resources,
android.R.color.transparent, theme)
}
val padding = resources.getDimensionPixelSize(R.dimen.padding_mention).toFloat()
val radius = resources.getDimensionPixelSize(R.dimen.radius_mention).toFloat()
val usernameSpan = MentionSpan(mentionBgColor, mentionTextColor, radius, padding,
referSelf)
result.setSpan(usernameSpan, start, start + user.length, 0)
}
} }
} }
...@@ -117,7 +139,7 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -117,7 +139,7 @@ class MessageParser @Inject constructor(val context: Application, private val co
// set time spans // set time spans
builder.setSpan(AbsoluteSizeSpan(res.getDimensionPixelSize(R.dimen.message_time_text_size)), builder.setSpan(AbsoluteSizeSpan(res.getDimensionPixelSize(R.dimen.message_time_text_size)),
timeOffsetStart, builder.length()) timeOffsetStart, builder.length())
builder.setSpan(ForegroundColorSpan(res.getColor(R.color.darkGray)), builder.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.darkGray)),
timeOffsetStart, builder.length()) timeOffsetStart, builder.length())
} }
} }
...@@ -134,7 +156,7 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -134,7 +156,7 @@ class MessageParser @Inject constructor(val context: Application, private val co
// pass to super to apply markdown // pass to super to apply markdown
super.visit(blockQuote) super.visit(blockQuote)
val padding = context.resources.getDimensionPixelSize(R.dimen.quote_padding) val padding = context.resources.getDimensionPixelSize(R.dimen.padding_quote)
builder.setSpan(QuoteMarginSpan(context.getDrawable(R.drawable.quote), padding), length, builder.setSpan(QuoteMarginSpan(context.getDrawable(R.drawable.quote), padding), length,
builder.length()) builder.length())
} }
...@@ -176,24 +198,37 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -176,24 +198,37 @@ class MessageParser @Inject constructor(val context: Application, private val co
} }
} }
class UsernameClickableSpan(private val linkBackgroundColor: Int, class MentionSpan(private val backgroundColor: Int,
private val linkTextColor: Int, private val textColor: Int,
private val referSelf: Boolean) : ClickableSpan() { private val radius: Float,
padding: Float,
override fun onClick(widget: View) { referSelf: Boolean) : ReplacementSpan() {
//TODO: Implement action when clicking on username, like showing user profile. private val padding: Float = if (referSelf) padding else 0F
override fun getSize(paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fm: Paint.FontMetricsInt?): Int {
return (padding + paint.measureText(text.subSequence(start, end).toString()) + padding).toInt()
} }
override fun updateDrawState(ds: TextPaint) { override fun draw(canvas: Canvas,
if (referSelf) { text: CharSequence,
ds.color = Color.WHITE start: Int,
ds.typeface = Typeface.DEFAULT_BOLD end: Int,
ds.bgColor = linkTextColor x: Float,
} else { top: Int,
ds.color = linkTextColor y: Int,
ds.bgColor = linkBackgroundColor bottom: Int,
} paint: Paint) {
ds.isUnderlineText = false val length = paint.measureText(text.subSequence(start, end).toString())
val rect = RectF(x, top.toFloat(), x + length + padding * 2,
bottom.toFloat())
paint.setColor(backgroundColor)
canvas.drawRoundRect(rect, radius, radius, paint)
paint.setColor(textColor)
canvas.drawText(text, start, end, x + padding, y.toFloat(), paint)
} }
} }
......
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:hint="@string/msg_message" android:hint="@string/msg_message"
android:inputType="textCapSentences|textMultiLine" android:inputType="textCapSentences|textMultiLine"
android:lineSpacingExtra="4dp"
android:maxLines="4" android:maxLines="4"
android:minHeight="24dp" android:minHeight="24dp"
android:scrollbars="vertical" /> android:scrollbars="vertical" />
......
...@@ -31,8 +31,6 @@ ...@@ -31,8 +31,6 @@
<color name="actionMenuColor">#FF727272</color> <color name="actionMenuColor">#FF727272</color>
<color name="whitesmoke">#FFf1f1f1</color> <color name="whitesmoke">#FFf1f1f1</color>
<color name="linkTextColor">#FF074481</color>
<color name="linkBackgroundColor">#30074481</color>
<color name="colorEmojiIcon">#FF767676</color> <color name="colorEmojiIcon">#FF767676</color>
</resources> </resources>
...@@ -16,10 +16,11 @@ ...@@ -16,10 +16,11 @@
<!-- Emoji --> <!-- Emoji -->
<dimen name="picker_padding_bottom">16dp</dimen> <dimen name="picker_padding_bottom">16dp</dimen>
<dimen name="supposed_keyboard_height">252dp</dimen> <dimen name="supposed_keyboard_height">252dp</dimen>
<!-- Message --> <!-- Message -->
<dimen name="quote_padding">8dp</dimen> <dimen name="padding_quote">8dp</dimen>
<dimen name="padding_mention">4dp</dimen>
<dimen name="radius_mention">6dp</dimen>
</resources> </resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment