Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a temporary messages when sending and keep it on error #1259

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,7 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH
binding.messageQuote.quotedChatMessageView.visibility = View.GONE
}

val readStatusDrawableInt = when (message.readStatus) {
ReadStatus.READ -> R.drawable.ic_check_all
ReadStatus.SENT -> R.drawable.ic_check
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)
else -> null
}

readStatusDrawableInt?.let { drawableInt ->
readStatusDrawableInt(message)?.let { drawableInt ->
ResourcesCompat.getDrawable(context!!.resources, drawableInt, null)?.let {
binding.checkMark.setImageDrawable(it)
binding.checkMark.setColorFilter(
Expand All @@ -123,7 +111,7 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH
}
}

binding.checkMark.setContentDescription(readStatusContentDescriptionString)
binding.checkMark.setContentDescription(readStatusContentDescriptionString(message))

itemView.setTag(MessageSwipeCallback.REPLYABLE_VIEW_TAG, message.replyable)

Expand All @@ -146,6 +134,24 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH
commonMessageInterface.onClickReaction(chatMessage, emoji)
}

private fun readStatusContentDescriptionString(message: ChatMessage) =
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
}

private fun readStatusDrawableInt(message: ChatMessage) =
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
}

private fun processParentMessage(message: ChatMessage) {
val parentChatMessage = message.parentMessage
val textColor = viewThemeUtils.getScheme(binding.messageQuote.quotedMessage.context).onSurfaceVariant
Expand Down
59 changes: 56 additions & 3 deletions app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,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.Locale
Expand Down Expand Up @@ -2120,6 +2121,46 @@ class ChatController(args: Bundle) :
}

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

messObj.readStatus = ReadStatus.SENDING

if (conversationUser!!.userId != "?") {
// Logged in user
messObj.actorType = "users"
messObj.actorId = conversationUser.userId
messObj.actorDisplayName = conversationUser.username
} else if (currentConversation!!.actorType != null) {
// API v3 or later
messObj.actorType = currentConversation!!.actorType
messObj.actorId = currentConversation!!.actorId
messObj.actorDisplayName = ""
} else {
// API v1 or v2 as a guest
messObj.actorType = "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.actorId = sha1.toString()
messObj.actorDisplayName = ""
}

adapter!!.addToStart(
messObj,
true
)

if (conversationUser != null) {
val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1))

Expand All @@ -2140,6 +2181,8 @@ class ChatController(args: Bundle) :

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

myFirstMessage = message

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

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

messObj.readStatus = ReadStatus.FAILED
adapter!!.updateAndMoveToStart(messObj)

if (e is HttpException) {
val code = e.code()
if (code.toString().startsWith("2")) {
Expand Down Expand Up @@ -2996,7 +3044,13 @@ 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 @@ -3058,8 +3112,7 @@ class ChatController(args: Bundle) :
Log.e(TAG, e.message, e)
}

override fun onComplete() {
// unused atm
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 @@ -339,6 +339,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