Commit 0af84cf7 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement delete message action

parent e09068ec
......@@ -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)
}
}
}
......
......@@ -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()
}
}
......
......@@ -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)
......
......@@ -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
......
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