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
e358a7c5
Commit
e358a7c5
authored
Jan 22, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add MessageViewModel implementation and a mapper for it
parent
fe32cafd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
0 deletions
+121
-0
MessageViewModel.kt
...hat/rocket/android/chatroom/viewmodel/MessageViewModel.kt
+104
-0
MessageViewModelMapper.kt
...cket/android/chatroom/viewmodel/MessageViewModelMapper.kt
+17
-0
No files found.
app/src/main/java/chat/rocket/android/chatroom/viewmodel/MessageViewModel.kt
0 → 100644
View file @
e358a7c5
package
chat.rocket.android.chatroom.viewmodel
import
DateTimeHelper
import
android.content.Context
import
android.graphics.Color
import
android.graphics.Typeface
import
android.text.SpannableString
import
android.text.style.ForegroundColorSpan
import
android.text.style.StyleSpan
import
chat.rocket.android.R
import
chat.rocket.android.app.RocketChatApplication
import
chat.rocket.android.helper.UrlHelper
import
chat.rocket.android.server.domain.USE_REALNAME
import
chat.rocket.core.model.Message
import
chat.rocket.core.model.MessageType.*
import
chat.rocket.core.model.Value
data class
MessageViewModel
(
private
val
message
:
Message
,
private
val
settings
:
Map
<
String
,
Value
<
Any
>>?)
{
val
id
:
String
=
message
.
id
val
time
:
CharSequence
val
sender
:
CharSequence
val
content
:
CharSequence
init
{
sender
=
getSenderName
()
content
=
getContent
(
RocketChatApplication
.
instance
)
time
=
getTime
()
}
fun
getAvatarUrl
(
serverUrl
:
String
):
String
?
{
return
message
.
sender
?.
username
.
let
{
return
@let
UrlHelper
.
getAvatarUrl
(
serverUrl
,
it
.
toString
())
}
}
fun
getTime
()
=
DateTimeHelper
.
getTime
(
DateTimeHelper
.
getLocalDateTime
(
message
.
timestamp
))
fun
getSenderName
():
CharSequence
{
val
useRealName
=
settings
?.
get
(
USE_REALNAME
)
?.
value
as
Boolean
val
username
=
message
.
sender
?.
username
val
realName
=
message
.
sender
?.
name
val
senderName
=
if
(
useRealName
)
realName
else
username
return
if
(
senderName
==
null
)
username
.
toString
()
else
senderName
.
toString
()
}
fun
getContent
(
context
:
Context
):
CharSequence
{
val
contentMessage
:
CharSequence
when
(
message
.
type
)
{
//TODO: Add implementation for Welcome type.
MESSAGE_REMOVED
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_removed
))
USER_JOINED
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_user_joined_channel
))
USER_LEFT
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_user_left
))
USER_ADDED
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_user_added_by
,
message
.
message
,
message
.
sender
?.
username
))
ROOM_NAME_CHANGED
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_room_name_changed
,
message
.
message
,
message
.
sender
?.
username
))
USER_REMOVED
->
contentMessage
=
getSystemMessage
(
context
.
getString
(
R
.
string
.
message_user_removed_by
,
message
.
message
,
message
.
sender
?.
username
))
else
->
contentMessage
=
getNormalMessage
()
}
return
contentMessage
}
private
fun
getNormalMessage
()
=
message
.
message
private
fun
getSystemMessage
(
content
:
String
):
CharSequence
{
val
spannableMsg
=
SpannableString
(
content
)
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
ITALIC
),
0
,
spannableMsg
.
length
,
0
)
spannableMsg
.
setSpan
(
ForegroundColorSpan
(
Color
.
GRAY
),
0
,
spannableMsg
.
length
,
0
)
if
(
message
.
type
==
USER_ADDED
)
{
val
userAddedStartIndex
=
5
val
userAddedLastIndex
=
userAddedStartIndex
+
message
.
message
.
length
val
addedByStartIndex
=
userAddedLastIndex
+
10
val
addedByLastIndex
=
addedByStartIndex
+
message
.
sender
?.
username
!!
.
length
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
userAddedStartIndex
,
userAddedLastIndex
,
0
)
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
addedByStartIndex
,
addedByLastIndex
,
0
)
}
else
if
(
message
.
type
==
ROOM_NAME_CHANGED
)
{
val
userAddedStartIndex
=
22
val
userAddedLastIndex
=
userAddedStartIndex
+
message
.
message
.
length
val
addedByStartIndex
=
userAddedLastIndex
+
4
val
addedByLastIndex
=
addedByStartIndex
+
message
.
sender
?.
username
!!
.
length
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
userAddedStartIndex
,
userAddedLastIndex
,
0
)
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
addedByStartIndex
,
addedByLastIndex
,
0
)
}
else
if
(
message
.
type
==
USER_REMOVED
)
{
val
userAddedStartIndex
=
5
val
userAddedLastIndex
=
userAddedStartIndex
+
message
.
message
.
length
val
addedByStartIndex
=
userAddedLastIndex
+
12
val
addedByLastIndex
=
addedByStartIndex
+
message
.
sender
?.
username
!!
.
length
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
userAddedStartIndex
,
userAddedLastIndex
,
0
)
spannableMsg
.
setSpan
(
StyleSpan
(
Typeface
.
BOLD_ITALIC
),
addedByStartIndex
,
addedByLastIndex
,
0
)
}
return
spannableMsg
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/chatroom/viewmodel/MessageViewModelMapper.kt
0 → 100644
View file @
e358a7c5
package
chat.rocket.android.chatroom.viewmodel
import
chat.rocket.core.model.Message
import
chat.rocket.core.model.Value
object
MessageViewModelMapper
{
suspend
fun
mapToViewModel
(
message
:
Message
,
settings
:
Map
<
String
,
Value
<
Any
>>?)
=
MessageViewModel
(
message
,
settings
)
suspend
fun
mapToViewModelList
(
messageList
:
List
<
Message
>,
settings
:
Map
<
String
,
Value
<
Any
>>?):
List
<
MessageViewModel
>
{
val
vmList
=
mutableListOf
<
MessageViewModel
>()
for
(
msg
in
messageList
)
{
vmList
.
add
(
MessageViewModel
(
msg
,
settings
))
}
return
vmList
}
}
\ 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