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(
private var lastState = manager.state
private var typingStatusList = arrayListOf<String>()
private val roomChangesChannel = Channel<Room>(Channel.CONFLATED)
private var lastMessageId: String? = null
private lateinit var draftKey: String
fun setupChatRoom(
......@@ -223,6 +224,7 @@ class ChatRoomPresenter @Inject constructor(
isBroadcast = chatIsBroadcast, isRoom = true
)
)
lastMessageId = localMessages.firstOrNull()?.id
val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId)
if (oldMessages.isNotEmpty() && lastSyncDate != null) {
view.showMessages(oldMessages, clearDataSet)
......@@ -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)
// 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)
subscribeMessages(chatRoomId)
......@@ -371,6 +373,7 @@ class ChatRoomPresenter @Inject constructor(
throw ex
}
}
lastMessageId = id
} else {
client.updateMessage(chatRoomId, messageId, text)
}
......@@ -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() {
when {
roomTypeOf(chatRoomType) is RoomType.DirectMessage ->
......@@ -1162,18 +1190,19 @@ class ChatRoomPresenter @Inject constructor(
sendMessage(roomId, text, null)
} else {
view.disableSendMessageButton()
val command = text.split(" ")
val name = command[0].substring(1)
val index = text.indexOf(" ")
var name = ""
var params = ""
command.forEachIndexed { index, param ->
if (index > 0) {
params += "$param "
}
if (index >= 1) {
name = text.substring(1, index)
params = text.substring(index + 1).trim()
}
val result = retryIO("runCommand($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
sendMessage(roomId, text, null)
}
......@@ -1286,6 +1315,7 @@ class ChatRoomPresenter @Inject constructor(
fun clearDraftMessage() {
localRepository.clear(draftKey)
}
/**
* Get unfinished message from local repository, when user left chat room without
* sending a message and now the user is back.
......
......@@ -148,10 +148,14 @@ private const val BUNDLE_CHAT_ROOM_MESSAGE = "chat_room_message"
class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiReactionListener,
ChatRoomAdapter.OnActionSelected, Drawable.Callback {
@Inject lateinit var presenter: ChatRoomPresenter
@Inject lateinit var parser: MessageParser
@Inject lateinit var analyticsManager: AnalyticsManager
@Inject lateinit var navigator: ChatRoomNavigator
@Inject
lateinit var presenter: ChatRoomPresenter
@Inject
lateinit var parser: MessageParser
@Inject
lateinit var analyticsManager: AnalyticsManager
@Inject
lateinit var navigator: ChatRoomNavigator
private lateinit var adapter: ChatRoomAdapter
internal lateinit var chatRoomId: String
private lateinit var chatRoomName: String
......@@ -472,14 +476,15 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}
}
override fun sendMessage(text: String) {
ui {
if (!text.isBlank()) {
if (!text.startsWith("/")) {
presenter.sendMessage(chatRoomId, text, editingMessageId)
} else {
if (text.startsWith("/")) {
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
}
}
override fun clearMessageComposition(deleteMessage: Boolean) {
ui {
citation = null
......@@ -896,7 +900,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
button_take_a_photo.setOnClickListener {
// Check for camera permission
context?.let {
if(hasCameraPermission(it)) {
if (hasCameraPermission(it)) {
dispatchTakePictureIntent()
} else {
getCameraPermission(this)
......@@ -964,7 +968,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode) {
when (requestCode) {
AndroidPermissionsHelper.CAMERA_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_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