瀏覽代碼

add ability to delete reactions

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 3 年之前
父節點
當前提交
aa5046cf27

+ 2 - 2
app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt

@@ -112,7 +112,7 @@ class MessageActionsDialog(
                 dialogMessageActionsBinding.emojiMore.clearFocus()
                 dialogMessageActionsBinding.messageActions.visibility = View.VISIBLE
 
-                val imm: InputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as
+                val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as
                     InputMethodManager
                 imm.hideSoftInputFromWindow(dialogMessageActionsBinding.emojiMore.windowToken, 0)
             }
@@ -257,7 +257,7 @@ class MessageActionsDialog(
                 }
 
                 override fun onError(e: Throwable) {
-                    Log.e(TAG, "error while sending emoji")
+                    Log.e(TAG, "error while sending reaction")
                 }
 
                 override fun onComplete() {

+ 43 - 8
app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt

@@ -45,6 +45,7 @@ import com.nextcloud.talk.databinding.DialogMessageReactionsBinding
 import com.nextcloud.talk.models.database.UserEntity
 import com.nextcloud.talk.models.json.chat.ChatMessage
 import com.nextcloud.talk.models.json.conversations.Conversation
+import com.nextcloud.talk.models.json.generic.GenericOverall
 import com.nextcloud.talk.models.json.reactions.ReactionsOverall
 import com.nextcloud.talk.utils.ApiUtils
 import com.nextcloud.talk.utils.DisplayUtils
@@ -56,11 +57,11 @@ import io.reactivex.schedulers.Schedulers
 
 @AutoInjector(NextcloudTalkApplication::class)
 class ShowReactionsDialog(
-    val activity: Activity,
-    val currentConversation: Conversation?,
-    val chatMessage: ChatMessage,
-    val userEntity: UserEntity?,
-    val ncApi: NcApi
+    private val activity: Activity,
+    private val currentConversation: Conversation?,
+    private val chatMessage: ChatMessage,
+    private val userEntity: UserEntity?,
+    private val ncApi: NcApi
 ) : BottomSheetDialog(activity), ReactionItemClickListener {
 
     private lateinit var binding: DialogMessageReactionsBinding
@@ -162,12 +163,46 @@ class ShowReactionsDialog(
 
     override fun onClick(reactionItem: ReactionItem) {
         Log.d(TAG, "onClick(reactionItem: ReactionItem): " + reactionItem.reaction)
-        // TODO implement removal of users reaction,
-        //  ownership needs to be checked, so only owned
-        //  reactions can be removed upon click
+
+        if (reactionItem.reactionVoter.actorId.equals(userEntity!!.userId)){
+            deleteReaction(chatMessage, reactionItem.reaction!!)
+        }
+
         dismiss()
     }
 
+    private fun deleteReaction(message: ChatMessage, emoji: String) {
+        val credentials = ApiUtils.getCredentials(userEntity?.username, userEntity?.token)
+
+        ncApi.deleteReaction(
+            credentials,
+            ApiUtils.getUrlForMessageReaction(
+                userEntity?.baseUrl,
+                currentConversation!!.token,
+                message.id
+            ),
+            emoji
+        )
+            ?.subscribeOn(Schedulers.io())
+            ?.observeOn(AndroidSchedulers.mainThread())
+            ?.subscribe(object : Observer<GenericOverall> {
+                override fun onSubscribe(d: Disposable) {
+                }
+
+                override fun onNext(@NonNull genericOverall: GenericOverall) {
+                    Log.d(TAG, "deleted reaction: $emoji")
+                }
+
+                override fun onError(e: Throwable) {
+                    Log.e(TAG, "error while deleting reaction: $emoji")
+                }
+
+                override fun onComplete() {
+                    dismiss()
+                }
+            })
+    }
+
     companion object {
         const val TAG = "ShowReactionsDialog"
         const val EMOJI_MARGIN: Float = 8F