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
0af84cf7
Commit
0af84cf7
authored
Feb 03, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement delete message action
parent
e09068ec
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
26 deletions
+55
-26
ChatRoomPresenter.kt
...rocket/android/chatroom/presentation/ChatRoomPresenter.kt
+33
-16
ChatRoomAdapter.kt
...n/java/chat/rocket/android/chatroom/ui/ChatRoomAdapter.kt
+19
-7
ChatRoomFragment.kt
.../java/chat/rocket/android/chatroom/ui/ChatRoomFragment.kt
+2
-3
MessageViewModel.kt
...hat/rocket/android/chatroom/viewmodel/MessageViewModel.kt
+1
-0
No files found.
app/src/main/java/chat/rocket/android/chatroom/presentation/ChatRoomPresenter.kt
View file @
0af84cf7
...
...
@@ -7,12 +7,14 @@ import chat.rocket.android.server.domain.GetSettingsInteractor
import
chat.rocket.android.server.domain.MessagesRepository
import
chat.rocket.android.server.infraestructure.RocketChatClientFactory
import
chat.rocket.android.util.launchUI
import
chat.rocket.common.RocketChatException
import
chat.rocket.common.model.roomTypeOf
import
chat.rocket.common.util.ifNull
import
chat.rocket.core.internal.realtime.State
import
chat.rocket.core.internal.realtime.connect
import
chat.rocket.core.internal.realtime.subscribeRoomMessages
import
chat.rocket.core.internal.realtime.unsubscibre
import
chat.rocket.core.internal.rest.deleteMessage
import
chat.rocket.core.internal.rest.messages
import
chat.rocket.core.internal.rest.sendMessage
import
chat.rocket.core.model.Message
...
...
@@ -31,7 +33,6 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
factory
:
RocketChatClientFactory
,
private
val
mapper
:
MessageViewModelMapper
)
{
private
val
client
=
factory
.
create
(
serverInteractor
.
get
()
!!
)
private
val
roomMessages
=
ArrayList
<
Message
>()
private
var
subId
:
String
?
=
null
private
var
settings
:
Map
<
String
,
Value
<
Any
>>?
=
null
...
...
@@ -46,10 +47,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
view
.
showLoading
()
try
{
val
messages
=
client
.
messages
(
chatRoomId
,
roomTypeOf
(
chatRoomType
),
offset
,
30
).
result
synchronized
(
roomMessages
)
{
roomMessages
.
addAll
(
messages
)
messagesRepository
.
saveAll
(
messages
)
}
messagesRepository
.
saveAll
(
messages
)
val
messagesViewModels
=
mapper
.
mapToViewModelList
(
messages
,
settings
)
view
.
showMessages
(
messagesViewModels
,
serverInteractor
.
get
()
!!
)
...
...
@@ -142,6 +140,26 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
}
}
/**
* Delete the message with the given id.
*
* @param roomId The room id of the message to be deleted.
* @param id The id of the message to be deleted.
*/
fun
deleteMessage
(
roomId
:
String
,
id
:
String
)
{
launchUI
(
strategy
)
{
//TODO: Default delete message always to true. Until we have the permissions system
//implemented, a user will only be able to delete his own messages.
try
{
//TODO: Should honor permission 'Message_ShowDeletedStatus'
client
.
deleteMessage
(
roomId
,
id
,
true
)
}
catch
(
e
:
RocketChatException
)
{
//TODO: Handle permission error.
Timber
.
e
(
e
)
}
}
}
private
suspend
fun
listenMessages
(
roomId
:
String
)
{
launch
(
CommonPool
+
strategy
.
jobs
)
{
for
(
message
in
client
.
messagesChannel
)
{
...
...
@@ -157,17 +175,16 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
private
fun
updateMessage
(
streamedMessage
:
Message
)
{
launchUI
(
strategy
)
{
val
viewModelStreamedMessage
=
mapper
.
mapToViewModel
(
streamedMessage
,
settings
)
synchronized
(
roomMessages
)
{
val
index
=
roomMessages
.
indexOfFirst
{
msg
->
msg
.
id
==
streamedMessage
.
id
}
if
(
index
!=
-
1
)
{
Timber
.
d
(
"Updatind message at $index"
)
roomMessages
[
index
]
=
streamedMessage
view
.
dispatchUpdateMessage
(
index
,
viewModelStreamedMessage
)
}
else
{
Timber
.
d
(
"Adding new message"
)
roomMessages
.
add
(
0
,
streamedMessage
)
view
.
showNewMessage
(
viewModelStreamedMessage
)
}
val
roomMessages
=
messagesRepository
.
getByRoomId
(
streamedMessage
.
roomId
)
val
index
=
roomMessages
.
indexOfFirst
{
msg
->
msg
.
id
==
streamedMessage
.
id
}
if
(
index
>
-
1
)
{
Timber
.
d
(
"Updating message at $index"
)
messagesRepository
.
save
(
streamedMessage
)
view
.
dispatchUpdateMessage
(
index
,
viewModelStreamedMessage
)
}
else
{
Timber
.
d
(
"Adding new message"
)
messagesRepository
.
save
(
streamedMessage
)
view
.
showNewMessage
(
viewModelStreamedMessage
)
}
}
}
...
...
app/src/main/java/chat/rocket/android/chatroom/ui/ChatRoomAdapter.kt
View file @
0af84cf7
...
...
@@ -8,6 +8,7 @@ import android.widget.ImageView
import
android.widget.PopupMenu
import
android.widget.TextView
import
chat.rocket.android.R
import
chat.rocket.android.chatroom.presentation.ChatRoomPresenter
import
chat.rocket.android.chatroom.viewmodel.AttachmentType
import
chat.rocket.android.chatroom.viewmodel.MessageViewModel
import
chat.rocket.android.player.PlayerActivity
...
...
@@ -21,7 +22,8 @@ import kotlinx.android.synthetic.main.avatar.view.*
import
kotlinx.android.synthetic.main.item_message.view.*
import
kotlinx.android.synthetic.main.message_attachment.view.*
class
ChatRoomAdapter
(
private
val
serverUrl
:
String
)
:
RecyclerView
.
Adapter
<
ChatRoomAdapter
.
ViewHolder
>()
{
class
ChatRoomAdapter
(
private
val
serverUrl
:
String
,
private
val
presenter
:
ChatRoomPresenter
)
:
RecyclerView
.
Adapter
<
ChatRoomAdapter
.
ViewHolder
>()
{
init
{
setHasStableIds
(
true
)
...
...
@@ -30,7 +32,7 @@ class ChatRoomAdapter(private val serverUrl: String) : RecyclerView.Adapter<Chat
val
dataSet
=
ArrayList
<
MessageViewModel
>()
override
fun
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
ViewHolder
=
ViewHolder
(
parent
.
inflate
(
R
.
layout
.
item_message
),
serverUrl
)
ViewHolder
(
parent
.
inflate
(
R
.
layout
.
item_message
),
serverUrl
,
presenter
)
override
fun
onBindViewHolder
(
holder
:
ViewHolder
,
position
:
Int
)
=
holder
.
bind
(
dataSet
[
position
])
...
...
@@ -53,16 +55,19 @@ class ChatRoomAdapter(private val serverUrl: String) : RecyclerView.Adapter<Chat
notifyItemInserted
(
0
)
}
fun
updateItem
(
index
:
Int
,
message
:
MessageViewModel
)
{
dataSet
[
index
]
=
message
notifyItemChanged
(
index
)
fun
updateItem
(
message
:
MessageViewModel
)
{
val
index
=
dataSet
.
indexOfFirst
{
it
.
id
==
message
.
id
}
if
(
index
>
-
1
)
{
dataSet
[
index
]
=
message
notifyItemChanged
(
index
)
}
}
override
fun
getItemId
(
position
:
Int
):
Long
{
return
dataSet
[
position
].
id
.
hashCode
().
toLong
()
}
class
ViewHolder
(
itemView
:
View
,
val
serverUrl
:
String
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
class
ViewHolder
(
itemView
:
View
,
val
serverUrl
:
String
,
val
presenter
:
ChatRoomPresenter
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
fun
bind
(
message
:
MessageViewModel
)
=
with
(
itemView
)
{
bindUserAvatar
(
message
,
image_avatar
,
image_unknown_avatar
)
...
...
@@ -74,9 +79,16 @@ class ChatRoomAdapter(private val serverUrl: String) : RecyclerView.Adapter<Chat
bindAttachment
(
message
,
message_attachment
,
image_attachment
,
audio_video_attachment
,
file_name
)
itemView
.
setOnClickListener
{
text_content
.
setOnClickListener
{
val
popup
=
PopupMenu
(
it
.
context
,
it
)
popup
.
menuInflater
.
inflate
(
R
.
menu
.
message_actions
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
when
(
it
.
itemId
)
{
R
.
id
.
action_menu_msg_delete
->
presenter
.
deleteMessage
(
message
.
roomId
,
message
.
id
)
else
->
TODO
(
"Not implemented"
)
}
true
}
popup
.
show
()
}
}
...
...
app/src/main/java/chat/rocket/android/chatroom/ui/ChatRoomFragment.kt
View file @
0af84cf7
...
...
@@ -39,7 +39,6 @@ private const val BUNDLE_CHAT_ROOM_TYPE = "chat_room_type"
private
const
val
BUNDLE_IS_CHAT_ROOM_READ_ONLY
=
"is_chat_room_read_only"
class
ChatRoomFragment
:
Fragment
(),
ChatRoomView
{
@Inject
lateinit
var
presenter
:
ChatRoomPresenter
private
lateinit
var
chatRoomId
:
String
private
lateinit
var
chatRoomName
:
String
...
...
@@ -78,7 +77,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override
fun
showMessages
(
dataSet
:
List
<
MessageViewModel
>,
serverUrl
:
String
)
{
activity
?.
apply
{
if
(
recycler_view
.
adapter
==
null
)
{
adapter
=
ChatRoomAdapter
(
serverUrl
)
adapter
=
ChatRoomAdapter
(
serverUrl
,
presenter
)
recycler_view
.
adapter
=
adapter
val
linearLayoutManager
=
LinearLayoutManager
(
context
,
LinearLayoutManager
.
VERTICAL
,
true
)
recycler_view
.
layoutManager
=
linearLayoutManager
...
...
@@ -120,7 +119,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
}
override
fun
dispatchUpdateMessage
(
index
:
Int
,
message
:
MessageViewModel
)
{
adapter
.
updateItem
(
index
,
message
)
adapter
.
updateItem
(
message
)
}
override
fun
showLoading
()
=
view_loading
.
setVisible
(
true
)
...
...
app/src/main/java/chat/rocket/android/chatroom/viewmodel/MessageViewModel.kt
View file @
0af84cf7
...
...
@@ -32,6 +32,7 @@ data class MessageViewModel(val context: Context,
private
val
parser
:
MessageParser
,
private
val
messagesRepository
:
MessagesRepository
)
{
val
id
:
String
=
message
.
id
val
roomId
:
String
=
message
.
roomId
val
time
:
CharSequence
val
sender
:
CharSequence
val
content
:
CharSequence
...
...
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