Unverified Commit 1089435c authored by Filipe Brito's avatar Filipe Brito Committed by GitHub

Merge pull request #2271 from HusseinElFeky/emoji-react

[NEW] Typing +:emoji_name: reacts to last message
parents da53bc60 b3a4c770
...@@ -124,6 +124,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -124,6 +124,7 @@ class ChatRoomPresenter @Inject constructor(
private var lastState = manager.state private var lastState = manager.state
private var typingStatusList = arrayListOf<String>() private var typingStatusList = arrayListOf<String>()
private val roomChangesChannel = Channel<Room>(Channel.CONFLATED) private val roomChangesChannel = Channel<Room>(Channel.CONFLATED)
private var lastMessageId: String? = null
private lateinit var draftKey: String private lateinit var draftKey: String
fun setupChatRoom( fun setupChatRoom(
...@@ -223,6 +224,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -223,6 +224,7 @@ class ChatRoomPresenter @Inject constructor(
isBroadcast = chatIsBroadcast, isRoom = true isBroadcast = chatIsBroadcast, isRoom = true
) )
) )
lastMessageId = localMessages.firstOrNull()?.id
val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId) val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId)
if (oldMessages.isNotEmpty() && lastSyncDate != null) { if (oldMessages.isNotEmpty() && lastSyncDate != null) {
view.showMessages(oldMessages, clearDataSet) view.showMessages(oldMessages, clearDataSet)
...@@ -235,7 +237,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -235,7 +237,7 @@ class ChatRoomPresenter @Inject constructor(
} }
// TODO: For now we are marking the room as read if we can get the messages (I mean, no exception occurs) // TODO: For now we are marking the room as read if we can get the messages (I mean, no exception occurs)
// but should mark only when the user see the first unread message. // but should mark only when the user sees the first unread message.
markRoomAsRead(chatRoomId) markRoomAsRead(chatRoomId)
subscribeMessages(chatRoomId) subscribeMessages(chatRoomId)
...@@ -371,6 +373,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -371,6 +373,7 @@ class ChatRoomPresenter @Inject constructor(
throw ex throw ex
} }
} }
lastMessageId = id
} else { } else {
client.updateMessage(chatRoomId, messageId, text) client.updateMessage(chatRoomId, messageId, text)
} }
...@@ -1089,6 +1092,31 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1089,6 +1092,31 @@ class ChatRoomPresenter @Inject constructor(
} }
} }
fun reactToLastMessage(text: String, roomId: String) {
launchUI(strategy) {
lastMessageId?.let { messageId ->
val emoji = text.substring(1).trimEnd()
if (emoji.length >= 2 && emoji.startsWith(":") && emoji.endsWith(":")) {
try {
retryIO("toggleEmoji($messageId, $emoji)") {
client.toggleReaction(messageId, emoji.removeSurrounding(":"))
}
logReactionEvent()
view.clearMessageComposition(true)
} catch (ex: RocketChatException) {
Timber.e(ex)
// emoji is not valid, post it
sendMessage(roomId, text, null)
}
} else {
sendMessage(roomId, text, null)
}
}.ifNull {
sendMessage(roomId, text, null)
}
}
}
private fun logReactionEvent() { private fun logReactionEvent() {
when { when {
roomTypeOf(chatRoomType) is RoomType.DirectMessage -> roomTypeOf(chatRoomType) is RoomType.DirectMessage ->
...@@ -1162,18 +1190,19 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1162,18 +1190,19 @@ class ChatRoomPresenter @Inject constructor(
sendMessage(roomId, text, null) sendMessage(roomId, text, null)
} else { } else {
view.disableSendMessageButton() view.disableSendMessageButton()
val command = text.split(" ") val index = text.indexOf(" ")
val name = command[0].substring(1) var name = ""
var params = "" var params = ""
command.forEachIndexed { index, param -> if (index >= 1) {
if (index > 0) { name = text.substring(1, index)
params += "$param " params = text.substring(index + 1).trim()
}
} }
val result = retryIO("runCommand($name, $params, $roomId)") { val result = retryIO("runCommand($name, $params, $roomId)") {
client.runCommand(Command(name, params), roomId) client.runCommand(Command(name, params), roomId)
} }
if (!result) { if (result) {
view.clearMessageComposition(true)
} else {
// failed, command is not valid so post it // failed, command is not valid so post it
sendMessage(roomId, text, null) sendMessage(roomId, text, null)
} }
...@@ -1286,6 +1315,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1286,6 +1315,7 @@ class ChatRoomPresenter @Inject constructor(
fun clearDraftMessage() { fun clearDraftMessage() {
localRepository.clear(draftKey) localRepository.clear(draftKey)
} }
/** /**
* Get unfinished message from local repository, when user left chat room without * Get unfinished message from local repository, when user left chat room without
* sending a message and now the user is back. * sending a message and now the user is back.
......
...@@ -148,10 +148,14 @@ private const val BUNDLE_CHAT_ROOM_MESSAGE = "chat_room_message" ...@@ -148,10 +148,14 @@ private const val BUNDLE_CHAT_ROOM_MESSAGE = "chat_room_message"
class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiReactionListener, class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiReactionListener,
ChatRoomAdapter.OnActionSelected, Drawable.Callback { ChatRoomAdapter.OnActionSelected, Drawable.Callback {
@Inject lateinit var presenter: ChatRoomPresenter @Inject
@Inject lateinit var parser: MessageParser lateinit var presenter: ChatRoomPresenter
@Inject lateinit var analyticsManager: AnalyticsManager @Inject
@Inject lateinit var navigator: ChatRoomNavigator lateinit var parser: MessageParser
@Inject
lateinit var analyticsManager: AnalyticsManager
@Inject
lateinit var navigator: ChatRoomNavigator
private lateinit var adapter: ChatRoomAdapter private lateinit var adapter: ChatRoomAdapter
internal lateinit var chatRoomId: String internal lateinit var chatRoomId: String
private lateinit var chatRoomName: String private lateinit var chatRoomName: String
...@@ -472,14 +476,15 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -472,14 +476,15 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
} }
override fun sendMessage(text: String) { override fun sendMessage(text: String) {
ui { ui {
if (!text.isBlank()) { if (!text.isBlank()) {
if (!text.startsWith("/")) { if (text.startsWith("/")) {
presenter.sendMessage(chatRoomId, text, editingMessageId)
} else {
presenter.runCommand(text, chatRoomId) presenter.runCommand(text, chatRoomId)
} else if (text.startsWith("+")) {
presenter.reactToLastMessage(text, chatRoomId)
} else {
presenter.sendMessage(chatRoomId, text, editingMessageId)
} }
} }
} }
...@@ -524,7 +529,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -524,7 +529,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
} }
override fun clearMessageComposition(deleteMessage: Boolean) { override fun clearMessageComposition(deleteMessage: Boolean) {
ui { ui {
citation = null citation = null
...@@ -896,7 +900,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -896,7 +900,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
button_take_a_photo.setOnClickListener { button_take_a_photo.setOnClickListener {
// Check for camera permission // Check for camera permission
context?.let { context?.let {
if(hasCameraPermission(it)) { if (hasCameraPermission(it)) {
dispatchTakePictureIntent() dispatchTakePictureIntent()
} else { } else {
getCameraPermission(this) getCameraPermission(this)
...@@ -964,7 +968,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -964,7 +968,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
grantResults: IntArray grantResults: IntArray
) { ) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode) { when (requestCode) {
AndroidPermissionsHelper.CAMERA_CODE -> { AndroidPermissionsHelper.CAMERA_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted // permission was granted
......
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