Эх сурвалжийг харах

Merge pull request #2048 from nextcloud/feature/230/reply-from-notification-error

Reply from notification - initial error handling
Marcel Hibbe 2 жил өмнө
parent
commit
968e8d2bab

+ 26 - 4
app/src/main/java/com/nextcloud/talk/receivers/DirectReplyReceiver.kt

@@ -26,12 +26,16 @@ import android.content.BroadcastReceiver
 import android.content.Context
 import android.content.Intent
 import android.os.Build
+import android.text.SpannableStringBuilder
+import android.text.style.ForegroundColorSpan
+import android.util.Log
 import androidx.annotation.RequiresApi
 import androidx.core.app.NotificationCompat
 import androidx.core.app.NotificationManagerCompat
 import androidx.core.app.Person
 import androidx.core.app.RemoteInput
 import autodagger.AutoInjector
+import com.nextcloud.talk.R
 import com.nextcloud.talk.api.NcApi
 import com.nextcloud.talk.application.NextcloudTalkApplication
 import com.nextcloud.talk.models.database.UserEntity
@@ -103,9 +107,10 @@ class DirectReplyReceiver : BroadcastReceiver() {
                     confirmReplySent()
                 }
 
+                @RequiresApi(Build.VERSION_CODES.N)
                 override fun onError(e: Throwable) {
-                    // TODO - inform the user that sending of the reply failed
-                    // unused atm
+                    Log.e(TAG, "Failed to send reply", e)
+                    informReplyFailed()
                 }
 
                 override fun onComplete() {
@@ -114,6 +119,19 @@ class DirectReplyReceiver : BroadcastReceiver() {
             })
     }
 
+    @RequiresApi(Build.VERSION_CODES.N)
+    private fun confirmReplySent() {
+        appendMessageToNotification(replyMessage!!)
+    }
+
+    @RequiresApi(Build.VERSION_CODES.N)
+    private fun informReplyFailed() {
+        val errorColor = ForegroundColorSpan(context.resources.getColor(R.color.medium_emphasis_text, context.theme))
+        val errorMessageHeader = context.resources.getString(R.string.nc_message_failed_to_send)
+        val errorMessage = SpannableStringBuilder().append("$errorMessageHeader\n$replyMessage", errorColor, 0)
+        appendMessageToNotification(errorMessage)
+    }
+
     @RequiresApi(Build.VERSION_CODES.N)
     private fun findActiveNotification(notificationId: Int): Notification? {
         val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@@ -121,7 +139,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
     }
 
     @RequiresApi(Build.VERSION_CODES.N)
-    private fun confirmReplySent() {
+    private fun appendMessageToNotification(reply: CharSequence) {
         // Implementation inspired by the SO question and article below:
         // https://stackoverflow.com/questions/51549456/android-o-notification-for-direct-reply-message
         // https://medium.com/@sidorovroman3/android-how-to-use-messagingstyle-for-notifications-without-caching-messages-c414ef2b816c
@@ -145,7 +163,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
             .setName(currentUser.displayName)
             .setIcon(NotificationUtils.loadAvatarSync(avatarUrl))
             .build()
-        val message = NotificationCompat.MessagingStyle.Message(replyMessage, System.currentTimeMillis(), me)
+        val message = NotificationCompat.MessagingStyle.Message(reply, System.currentTimeMillis(), me)
         previousStyle?.addMessage(message)
 
         // Set the updated style
@@ -154,4 +172,8 @@ class DirectReplyReceiver : BroadcastReceiver() {
         // Update the active notification.
         NotificationManagerCompat.from(context).notify(systemNotificationId!!, previousBuilder.build())
     }
+
+    companion object {
+        const val TAG = "DirectReplyReceiver"
+    }
 }

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -325,6 +325,7 @@
     <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_failed_to_send">Failed to send message:</string>
     <string name="nc_remote_audio_off">Remote audio off</string>
     <string name="nc_add_attachment">Add attachment</string>