Skip to content

Commit

Permalink
Show a temporary messages when sending and keep it on error
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and AndyScherzinger committed Jul 6, 2022
1 parent ad8df6d commit 81651a8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ class MagicOutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessage
val readStatusDrawableInt = when (message.readStatus) {
ReadStatus.READ -> R.drawable.ic_check_all
ReadStatus.SENT -> R.drawable.ic_check
ReadStatus.SENDING -> R.drawable.ic_sending
ReadStatus.FAILED -> R.drawable.ic_warning_white
else -> null
}

val readStatusContentDescriptionString = when (message.readStatus) {
ReadStatus.READ -> context?.resources?.getString(R.string.nc_message_read)
ReadStatus.SENT -> context?.resources?.getString(R.string.nc_message_sent)
ReadStatus.SENDING -> context?.resources?.getString(R.string.nc_message_sending)
ReadStatus.FAILED -> context?.resources?.getString(R.string.nc_message_send_error)
else -> null
}

Expand Down
82 changes: 68 additions & 14 deletions app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ import retrofit2.Response
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Objects
Expand Down Expand Up @@ -2021,6 +2022,46 @@ class ChatController(args: Bundle) :
}

private fun sendMessage(message: CharSequence, replyTo: Int?, sendWithoutNotification: Boolean) {
val messObj = ChatMessage()
messObj.setMessage(message.toString())
messObj.setActiveUser(conversationUser)
val tsLong = System.currentTimeMillis() / 1000
messObj.setTimestamp(tsLong)
messObj.setJsonMessageId(0 - tsLong.toInt())


messObj.setReadStatus(ReadStatus.SENDING)

if (conversationUser!!.userId != "?") {
// Logged in user
messObj.setActorType("users")
messObj.setActorId(conversationUser.userId)
messObj.setActorDisplayName(conversationUser.username)
} else if (currentConversation!!.actorType != null) {
// API v3 or later
messObj.setActorType(currentConversation!!.actorType)
messObj.setActorId(currentConversation!!.actorId)
messObj.setActorDisplayName("")
} else {
// API v1 or v2 as a guest
messObj.setActorType("guests")

val messageDigest: MessageDigest = MessageDigest.getInstance("SHA-1")
val digest: ByteArray = messageDigest.digest(currentConversation!!.sessionId.toByteArray())
val sha1: StringBuilder = StringBuilder()
val i = digest.iterator()
while (i.hasNext()) {
sha1.append(String.format("%02X", i.next()))
}

messObj.setActorId(sha1.toString())
messObj.setActorDisplayName("")
}

adapter!!.addToStart(
messObj,
true
)

if (conversationUser != null) {
val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1))
Expand All @@ -2042,6 +2083,8 @@ class ChatController(args: Bundle) :

@Suppress("Detekt.TooGenericExceptionCaught")
override fun onNext(genericOverall: GenericOverall) {
adapter!!.delete(messObj)

myFirstMessage = message

try {
Expand All @@ -2058,6 +2101,11 @@ class ChatController(args: Bundle) :
}

override fun onError(e: Throwable) {
Log.e(TAG, "An error occured while sending the chat message: " + e.message)

messObj.setReadStatus(ReadStatus.FAILED)
adapter!!.updateAndMoveToStart(messObj)

if (e is HttpException) {
val code = e.code()
if (Integer.toString(code).startsWith("2")) {
Expand Down Expand Up @@ -2408,10 +2456,12 @@ class ChatController(args: Bundle) :
if (message.item is ChatMessage) {
val chatMessage = message.item as ChatMessage

if (chatMessage.jsonMessageId <= it) {
chatMessage.readStatus = ReadStatus.READ
} else {
chatMessage.readStatus = ReadStatus.SENT
if (chatMessage.jsonMessageId > 0) {
if (chatMessage.jsonMessageId <= it) {
chatMessage.readStatus = ReadStatus.READ
} else {
chatMessage.readStatus = ReadStatus.SENT
}
}
}
}
Expand Down Expand Up @@ -2750,7 +2800,12 @@ class ChatController(args: Bundle) :

fun replyPrivately(message: IMessage?) {
val apiVersion =
ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
ApiUtils.getConversationApiVersion(
conversationUser, intArrayOf(
ApiUtils.APIv4,
1
)
)
val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion,
conversationUser?.baseUrl,
Expand Down Expand Up @@ -2802,21 +2857,20 @@ class ChatController(args: Bundle) :
)
}

override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}

override fun onComplete() {
// unused atm
override fun onComplete() {// unused atm
}
})
}

override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}

override fun onComplete() {
override fun onComplete() {
// unused atm
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
package com.nextcloud.talk.models.json.chat

enum class ReadStatus {
NONE, SENT, READ
NONE,
SENT,
READ,
SENDING,
FAILED
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_sending.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M9,5v2h6.59L4,18.59L5.41,20L17,8.41V15h2V5H9z"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@
<string name="nc_message_read">Message read</string>
<string name="nc_message_sent">Message sent</string>
<string name="nc_message_failed_to_send">Failed to send message:</string>
<string name="nc_message_sending">Message sending</string>
<string name="nc_message_send_error">Error while sending</string>
<string name="nc_remote_audio_off">Remote audio off</string>
<string name="nc_add_attachment">Add attachment</string>
<string name="emoji_category_recent">Recent</string>
Expand Down

0 comments on commit 81651a8

Please sign in to comment.