Unverified Commit a4111e3b authored by Rafael Kellermann Streit's avatar Rafael Kellermann Streit Committed by GitHub

Merge pull request #1024 from RocketChat/new/send-message-with-id

[IMPROVEMENT] Keep message on the text composer on fail to send message
parents 0c9fb9cf a2693fe4
...@@ -34,6 +34,7 @@ import kotlinx.coroutines.experimental.channels.Channel ...@@ -34,6 +34,7 @@ import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.launch
import org.threeten.bp.Instant import org.threeten.bp.Instant
import timber.log.Timber import timber.log.Timber
import java.util.*
import javax.inject.Inject import javax.inject.Inject
class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
...@@ -102,19 +103,19 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -102,19 +103,19 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
try { try {
// ignore message for now, will receive it on the stream // ignore message for now, will receive it on the stream
val message = if (messageId == null) { val message = if (messageId == null) {
client.sendMessage(chatRoomId, text) val id = UUID.randomUUID().toString()
client.sendMessage(id, chatRoomId, text)
} else { } else {
client.updateMessage(chatRoomId, messageId, text) client.updateMessage(chatRoomId, messageId, text)
} }
view.clearMessageComposition() view.enableSendMessageButton(false)
} catch (ex: Exception) { } catch (ex: Exception) {
ex.message?.let { ex.message?.let {
view.showMessage(it) view.showMessage(it)
}.ifNull { }.ifNull {
view.showGenericErrorMessage() view.showGenericErrorMessage()
} }
} finally { view.enableSendMessageButton(true)
view.enableSendMessageButton()
} }
} }
} }
...@@ -218,7 +219,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -218,7 +219,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
} }
if (messages.result.size == 50) { if (messages.result.size == 50) {
// we loade at least count messages, try one more to fetch more messages // we loaded at least count messages, try one more to fetch more messages
loadMissingMessages() loadMissingMessages()
} }
} }
...@@ -511,9 +512,11 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -511,9 +512,11 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
launchUI(strategy) { launchUI(strategy) {
try { try {
if (text.length == 1) { if (text.length == 1) {
view.disableSendMessageButton()
// we have just the slash, post it anyway // we have just the slash, post it anyway
sendMessage(roomId, text, null) sendMessage(roomId, text, null)
} else { } else {
view.disableSendMessageButton()
val command = text.split(" ") val command = text.split(" ")
val name = command[0].substring(1) val name = command[0].substring(1)
var params: String = "" var params: String = ""
...@@ -527,6 +530,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -527,6 +530,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
// failed, command is not valid so post it // failed, command is not valid so post it
sendMessage(roomId, text, null) sendMessage(roomId, text, null)
} }
view.enableSendMessageButton(false)
} }
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
Timber.e(ex) Timber.e(ex)
......
...@@ -92,8 +92,10 @@ interface ChatRoomView : LoadingView, MessageView { ...@@ -92,8 +92,10 @@ interface ChatRoomView : LoadingView, MessageView {
/** /**
* Enables the send message button. * Enables the send message button.
*
* @param sendFailed Whether the sent message has failed.
*/ */
fun enableSendMessageButton() fun enableSendMessageButton(sendFailed: Boolean)
/** /**
* Clears the message composition. * Clears the message composition.
......
...@@ -35,10 +35,8 @@ import kotlinx.android.synthetic.main.fragment_chat_room.* ...@@ -35,10 +35,8 @@ import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.* import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.* import kotlinx.android.synthetic.main.message_composer.*
import kotlinx.android.synthetic.main.message_list.* import kotlinx.android.synthetic.main.message_list.*
import timber.log.Timber
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.absoluteValue
fun newInstance(chatRoomId: String, fun newInstance(chatRoomId: String,
chatRoomName: String, chatRoomName: String,
...@@ -126,7 +124,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -126,7 +124,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
activity?.apply { activity?.apply {
(this as? ChatRoomActivity)?.showRoomTypeIcon(true) (this as? ChatRoomActivity)?.showRoomTypeIcon(true)
} }
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
...@@ -217,10 +214,10 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -217,10 +214,10 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
recycler_view.addOnLayoutChangeListener { _, _, _, _, bottom, _, _, _, oldBottom -> recycler_view.addOnLayoutChangeListener { _, _, _, _, bottom, _, _, _, oldBottom ->
val y = oldBottom - bottom val y = oldBottom - bottom
if (y.absoluteValue > 0) { if (Math.abs(y) > 0) {
// if y is positive the keyboard is up else it's down // if y is positive the keyboard is up else it's down
recycler_view.post { recycler_view.post {
if (y > 0 || verticalScrollOffset.get().absoluteValue >= y.absoluteValue) { if (y > 0 || Math.abs(verticalScrollOffset.get()) >= Math.abs(y)) {
recycler_view.scrollBy(0, y) recycler_view.scrollBy(0, y)
} else { } else {
recycler_view.scrollBy(0, verticalScrollOffset.get()) recycler_view.scrollBy(0, verticalScrollOffset.get())
...@@ -296,10 +293,12 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -296,10 +293,12 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
button_send.isEnabled = false button_send.isEnabled = false
} }
override fun enableSendMessageButton() { override fun enableSendMessageButton(sendFailed: Boolean) {
button_send.isEnabled = true button_send.isEnabled = true
text_message.isEnabled = true text_message.isEnabled = true
text_message.erase() if (!sendFailed) {
clearMessageComposition()
}
} }
override fun clearMessageComposition() { override fun clearMessageComposition() {
...@@ -465,7 +464,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -465,7 +464,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
private fun setupRecyclerView() { private fun setupRecyclerView() {
recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
Timber.i("Scrolling vertically: $dy")
if (!recyclerView.canScrollVertically(1)) { if (!recyclerView.canScrollVertically(1)) {
button_fab.hide() button_fab.hide()
} else { } else {
...@@ -522,8 +520,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -522,8 +520,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
var textMessage = citation ?: "" var textMessage = citation ?: ""
textMessage += text_message.textContent textMessage += text_message.textContent
sendMessage(textMessage) sendMessage(textMessage)
clearMessageComposition()
} }
button_show_attachment_options.setOnClickListener { button_show_attachment_options.setOnClickListener {
...@@ -588,7 +584,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -588,7 +584,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
text_message.requestFocus() text_message.requestFocus()
emojiKeyboardPopup.showAtBottomPending() emojiKeyboardPopup.showAtBottomPending()
KeyboardHelper.showSoftKeyboard(text_message) KeyboardHelper.showSoftKeyboard(text_message)
} }
setReactionButtonIcon(R.drawable.ic_keyboard_black_24dp) setReactionButtonIcon(R.drawable.ic_keyboard_black_24dp)
} else { } else {
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
<string name="msg_new_user_agreement">Ao proceder você concorda com nossos %1$s e %2$s</string> <string name="msg_new_user_agreement">Ao proceder você concorda com nossos %1$s e %2$s</string>
<string name="msg_2fa_code">Código 2FA</string> <string name="msg_2fa_code">Código 2FA</string>
<string name="msg_yesterday">ontem</string> <string name="msg_yesterday">ontem</string>
<string name="msg_message">Messagem</string> <string name="msg_message">Mensagem</string>
<string name="msg_this_room_is_read_only">Este chat é apenas de leitura</string> <string name="msg_this_room_is_read_only">Este chat é apenas de leitura</string>
<string name="msg_invalid_2fa_code">Código 2FA inválido</string> <string name="msg_invalid_2fa_code">Código 2FA inválido</string>
<string name="msg_invalid_file">Arquivo inválido</string> <string name="msg_invalid_file">Arquivo inválido</string>
......
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