Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
691a6c76
Commit
691a6c76
authored
Feb 02, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply spans to usernames
parent
ecee2a7f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
1 deletion
+32
-1
MessageParser.kt
...src/main/java/chat/rocket/android/helper/MessageParser.kt
+32
-1
No files found.
app/src/main/java/chat/rocket/android/helper/MessageParser.kt
View file @
691a6c76
...
...
@@ -8,8 +8,11 @@ import android.graphics.Paint
import
android.graphics.Typeface
import
android.graphics.drawable.Drawable
import
android.text.Layout
import
android.text.Spannable
import
android.text.Spanned
import
android.text.TextPaint
import
android.text.style.*
import
android.view.View
import
chat.rocket.android.R
import
chat.rocket.android.chatroom.viewmodel.MessageViewModel
import
chat.rocket.core.model.Url
...
...
@@ -18,12 +21,15 @@ import ru.noties.markwon.Markwon
import
ru.noties.markwon.SpannableBuilder
import
ru.noties.markwon.SpannableConfiguration
import
ru.noties.markwon.renderer.SpannableMarkdownVisitor
import
java.util.regex.Pattern
import
javax.inject.Inject
class
MessageParser
@Inject
constructor
(
val
context
:
Application
,
private
val
configuration
:
SpannableConfiguration
)
{
private
val
parser
=
Markwon
.
createParser
()
private
val
userPattern
=
Pattern
.
compile
(
"(@[\\w.]+)"
,
Pattern
.
MULTILINE
or
Pattern
.
CASE_INSENSITIVE
)
/**
* Render a markdown text message to Spannable.
...
...
@@ -53,7 +59,20 @@ class MessageParser @Inject constructor(val context: Application, private val co
quoteNode
.
accept
(
QuoteMessageBodyVisitor
(
context
,
configuration
,
builder
))
}
return
builder
.
text
()
val
result
=
builder
.
text
()
applySpans
(
result
)
return
result
}
private
fun
applySpans
(
text
:
CharSequence
)
{
val
matcher
=
userPattern
.
matcher
(
text
)
val
result
=
text
as
Spannable
while
(
matcher
.
find
())
{
val
user
=
matcher
.
group
(
1
)
val
start
=
matcher
.
start
()
//TODO: should check if username actually exists prior to applying.
result
.
setSpan
(
UsernameClickableSpan
(),
start
,
start
+
user
.
length
,
0
)
}
}
/**
...
...
@@ -143,4 +162,16 @@ class MessageParser @Inject constructor(val context: Application, private val co
}
}
}
class
UsernameClickableSpan
:
ClickableSpan
()
{
override
fun
onClick
(
widget
:
View
)
{
//TODO: Implement action when clicking on username, like showing user profile.
}
override
fun
updateDrawState
(
ds
:
TextPaint
)
{
ds
.
color
=
ds
.
linkColor
ds
.
isUnderlineText
=
false
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment