Commit 1f996b5f authored by Hussein El Feky's avatar Hussein El Feky

Reformatted code

parent 094e076d
...@@ -224,7 +224,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -224,7 +224,7 @@ class ChatRoomPresenter @Inject constructor(
isBroadcast = chatIsBroadcast, isRoom = true isBroadcast = chatIsBroadcast, isRoom = true
) )
) )
lastMessageId = localMessages.first().id 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)
...@@ -237,7 +237,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -237,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)
...@@ -1079,7 +1079,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1079,7 +1079,7 @@ class ChatRoomPresenter @Inject constructor(
/** /**
* Send an emoji reaction to a message. * Send an emoji reaction to a message.
*/ */
fun react(messageId: String, emoji: String, roomId: String) { fun react(messageId: String, emoji: String) {
launchUI(strategy) { launchUI(strategy) {
try { try {
retryIO("toggleEmoji($messageId, $emoji)") { retryIO("toggleEmoji($messageId, $emoji)") {
...@@ -1088,10 +1088,31 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1088,10 +1088,31 @@ class ChatRoomPresenter @Inject constructor(
logReactionEvent() logReactionEvent()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
Timber.e(ex) Timber.e(ex)
// emoji is not valid, post it }
sendMessage(roomId, "+$emoji", null) }
} finally { }
view.clearMessageComposition(true)
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)
} }
} }
} }
...@@ -1180,7 +1201,9 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1180,7 +1201,9 @@ class ChatRoomPresenter @Inject constructor(
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)
} }
...@@ -1190,7 +1213,6 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1190,7 +1213,6 @@ class ChatRoomPresenter @Inject constructor(
// command is not valid, post it // command is not valid, post it
sendMessage(roomId, text, null) sendMessage(roomId, text, null)
} finally { } finally {
view.clearMessageComposition(true)
view.enableSendMessageButton() view.enableSendMessageButton()
} }
} }
...@@ -1304,8 +1326,4 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1304,8 +1326,4 @@ class ChatRoomPresenter @Inject constructor(
fun getDraftUnfinishedMessage(): String? { fun getDraftUnfinishedMessage(): String? {
return localRepository.get(draftKey) return localRepository.get(draftKey)
} }
fun getLastMessageId(): String? {
return lastMessageId
}
} }
...@@ -476,21 +476,16 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -476,21 +476,16 @@ 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.runCommand(text, chatRoomId) presenter.runCommand(text, chatRoomId)
return@ui } else if (text.startsWith("+")) {
} else if (text.startsWith("+") && presenter.getLastMessageId() != null) { presenter.reactToLastMessage(text, chatRoomId)
val trimmedText = text.substring(1).trimEnd() } else {
if (trimmedText.length - trimmedText.removeSurrounding(":").length == 2) { presenter.sendMessage(chatRoomId, text, editingMessageId)
presenter.react(presenter.getLastMessageId()!!, trimmedText, chatRoomId)
return@ui
}
} }
presenter.sendMessage(chatRoomId, text, editingMessageId)
} }
} }
} }
...@@ -680,11 +675,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -680,11 +675,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
override fun onReactionTouched(messageId: String, emojiShortname: String) { override fun onReactionTouched(messageId: String, emojiShortname: String) {
presenter.react(messageId, emojiShortname, chatRoomId) presenter.react(messageId, emojiShortname)
} }
override fun onReactionAdded(messageId: String, emoji: Emoji) { override fun onReactionAdded(messageId: String, emoji: Emoji) {
presenter.react(messageId, emoji.shortname, chatRoomId) presenter.react(messageId, emoji.shortname)
} }
override fun onReactionLongClicked( override fun onReactionLongClicked(
......
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