Browse Source

Reply from notification - initial error handling

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
Dariusz Olszewski 3 years ago
parent
commit
cee5564f39

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

@@ -26,12 +26,15 @@ import android.content.BroadcastReceiver
 import android.content.Context
 import android.content.Intent
 import android.os.Build
+import android.text.Html
+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 +106,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 +118,23 @@ class DirectReplyReceiver : BroadcastReceiver() {
             })
     }
 
+    @RequiresApi(Build.VERSION_CODES.N)
+    private fun confirmReplySent() {
+        appendMessageToNotification(replyMessage!!)
+    }
+
+    @RequiresApi(Build.VERSION_CODES.N)
+    private fun informReplyFailed() {
+        val errorMessage =
+            Html.fromHtml(
+                "<font color='red'><em>" +
+                    context.resources.getString(R.string.nc_message_failed_to_send) +
+                    "</em></font>",
+                Html.FROM_HTML_MODE_COMPACT
+            )
+        appendMessageToNotification(errorMessage)
+    }
+
     @RequiresApi(Build.VERSION_CODES.N)
     private fun findActiveNotification(notificationId: Int): Notification? {
         val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@@ -121,7 +142,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 +166,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 +175,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>