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
a09dc8fd
Commit
a09dc8fd
authored
Feb 26, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix mentions' styles
parent
06565722
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
33 deletions
+68
-33
MessageParser.kt
...src/main/java/chat/rocket/android/helper/MessageParser.kt
+64
-29
message_composer.xml
app/src/main/res/layout/message_composer.xml
+1
-0
colors.xml
app/src/main/res/values/colors.xml
+0
-2
dimens.xml
app/src/main/res/values/dimens.xml
+3
-2
No files found.
app/src/main/java/chat/rocket/android/helper/MessageParser.kt
View file @
a09dc8fd
...
@@ -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
)
}
}
}
}
...
...
app/src/main/res/layout/message_composer.xml
View file @
a09dc8fd
...
@@ -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"
/>
...
...
app/src/main/res/values/colors.xml
View file @
a09dc8fd
...
@@ -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>
app/src/main/res/values/dimens.xml
View file @
a09dc8fd
...
@@ -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
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