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 committed May 17, 2021
1 parent 9f5aeb7 commit 90a9de7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,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
75 changes: 66 additions & 9 deletions app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import org.parceler.Parcels
import retrofit2.HttpException
import retrofit2.Response
import java.net.HttpURLConnection
import java.security.MessageDigest
import java.util.ArrayList
import java.util.Date
import java.util.HashMap
Expand Down Expand Up @@ -547,7 +548,7 @@ class ChatController(args: Bundle) :
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.length >= lengthFilter) {
messageInput?.error = String.format(
Objects.requireNonNull<Resources> (resources).getString(R.string.nc_limit_hit),
Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit),
Integer.toString(lengthFilter)
)
} else {
Expand Down Expand Up @@ -1091,6 +1092,46 @@ class ChatController(args: Bundle) :
}

private fun sendMessage(message: CharSequence, replyTo: Int?) {
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 @@ -1109,6 +1150,8 @@ class ChatController(args: Bundle) :
}

override fun onNext(genericOverall: GenericOverall) {
adapter!!.delete(messObj)

myFirstMessage = message

if (popupBubble?.isShown ?: false) {
Expand All @@ -1119,6 +1162,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 @@ -1307,9 +1355,9 @@ class ChatController(args: Bundle) :
TextUtils.isEmpty(chatMessageList[i + 1].systemMessage) &&
chatMessageList[i + 1].actorId == chatMessageList[i].actorId &&
countGroupedMessages < 4 && DateFormatter.isSameDay(
chatMessageList[i].createdAt,
chatMessageList[i + 1].createdAt
)
chatMessageList[i].createdAt,
chatMessageList[i + 1].createdAt
)
) {
chatMessageList[i].isGrouped = true
countGroupedMessages++
Expand Down Expand Up @@ -1393,10 +1441,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 @@ -1620,7 +1670,12 @@ class ChatController(args: Bundle) :
true
}
R.id.action_reply_privately -> {
val apiVersion = ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
val apiVersion = ApiUtils.getConversationApiVersion(
conversationUser, intArrayOf(
ApiUtils.APIv4,
1
)
)
val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion,
conversationUser?.baseUrl,
Expand Down Expand Up @@ -1668,13 +1723,15 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}

override fun onComplete() {}
})
}

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

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

public enum 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 @@ -287,6 +287,8 @@
<string name="nc_formatted_message_you">You: %1$s</string>
<string name="nc_message_read">Message read</string>
<string name="nc_message_sent">Message sent</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>

Expand Down

0 comments on commit 90a9de7

Please sign in to comment.