From 20e67c803efcfd2107bf4fd3f0359c48629a6244 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 6 Apr 2022 13:06:02 +0200 Subject: [PATCH] add ability to send emoji Signed-off-by: Marcel Hibbe --- .../java/com/nextcloud/talk/api/NcApi.java | 15 ++++- .../talk/controllers/ChatController.kt | 12 +++- .../talk/ui/dialog/MessageActionsDialog.kt | 62 ++++++++++++++++--- .../talk/ui/dialog/ShowReactionsDialog.kt | 4 +- .../com/nextcloud/talk/utils/ApiUtils.java | 6 +- .../res/layout/dialog_message_actions.xml | 12 ++-- 6 files changed, 88 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApi.java b/app/src/main/java/com/nextcloud/talk/api/NcApi.java index aeb5ea4611..8f177a566b 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -490,8 +490,17 @@ Observable setStatusType(@Header("Authorization") String authori @GET Observable getUserStatuses(@Header("Authorization") String authorization, @Url String url); + + @POST + Observable sendReaction(@Header("Authorization") String authorization, @Url String url, + @Query("reaction") String reaction); + + @DELETE + Observable deleteReaction(@Header("Authorization") String authorization, @Url String url, + @Query("reaction") String reaction); + @GET - Observable getParticipantsForEmojiReaction(@Header("Authorization") String authorization, - @Url String url, - @Query("reaction") String reaction); + Observable getReactions(@Header("Authorization") String authorization, + @Url String url, + @Query("reaction") String reaction); } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 6139d08bf6..1f9a9925cc 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -2438,7 +2438,8 @@ class ChatController(args: Bundle) : message, conversationUser, currentConversation, - isShowMessageDeletionButton(message) + isShowMessageDeletionButton(message), + ncApi!! ).show() } } @@ -2720,6 +2721,15 @@ class ChatController(args: Bundle) : adapter?.update(messageTemp) } + fun updateAdapterAfterSendReaction(message: ChatMessage, emoji: String) { + var amount = message.reactions[emoji] + if (amount == null){ + amount = 0 + } + message.reactions[emoji] = amount + 1 + adapter?.update(message) + } + private fun isShowMessageDeletionButton(message: ChatMessage): Boolean { if (conversationUser == null) return false diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 9110b37512..8baf967c5a 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -21,25 +21,35 @@ package com.nextcloud.talk.ui.dialog import android.os.Bundle +import android.util.Log import android.view.View import android.view.ViewGroup +import androidx.annotation.NonNull import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import com.nextcloud.talk.BuildConfig import com.nextcloud.talk.R +import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.controllers.ChatController import com.nextcloud.talk.databinding.DialogMessageActionsBinding -import com.nextcloud.talk.models.database.UserEntity import com.nextcloud.talk.models.database.CapabilitiesUtil +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.utils.ApiUtils +import io.reactivex.Observer +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers class MessageActionsDialog( private val chatController: ChatController, private val message: ChatMessage, private val user: UserEntity?, private val currentConversation: Conversation?, - private val showMessageDeletionButton: Boolean + private val showMessageDeletionButton: Boolean, + private val ncApi: NcApi ) : BottomSheetDialog(chatController.activity!!) { private lateinit var dialogMessageActionsBinding: DialogMessageActionsBinding @@ -73,22 +83,22 @@ class MessageActionsDialog( private fun initEmojiBar() { if (CapabilitiesUtil.hasSpreedFeatureCapability(user, "reactions")) { dialogMessageActionsBinding.emojiThumbsUp.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiThumbsUp.text.toString()) } dialogMessageActionsBinding.emojiThumbsDown.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiThumbsDown.text.toString()) } dialogMessageActionsBinding.emojiLaugh.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiLaugh.text.toString()) } dialogMessageActionsBinding.emojiHeart.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiHeart.text.toString()) } dialogMessageActionsBinding.emojiConfused.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiConfused.text.toString()) } dialogMessageActionsBinding.emojiSad.setOnClickListener { - dismiss() + sendReaction(message, dialogMessageActionsBinding.emojiSad.text.toString()) } dialogMessageActionsBinding.emojiMore.setOnClickListener { dismiss() @@ -180,7 +190,43 @@ class MessageActionsDialog( } } + private fun sendReaction(message: ChatMessage, emoji: String) { + val credentials = ApiUtils.getCredentials(user?.username, user?.token) + + ncApi.sendReaction( + credentials, + ApiUtils.getUrlForMessageReaction( + user?.baseUrl, + currentConversation!!.token, + message.id + ), + emoji + ) + ?.subscribeOn(Schedulers.io()) + ?.observeOn(AndroidSchedulers.mainThread()) + ?.subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + } + + override fun onNext(@NonNull genericOverall: GenericOverall) { + val statusCode = genericOverall.ocs.meta.statusCode + if (statusCode == 200 || statusCode == 201) { + chatController.updateAdapterAfterSendReaction(message, emoji) + } + } + + override fun onError(e: Throwable) { + Log.e(TAG, "error while sending emoji") + } + + override fun onComplete() { + dismiss() + } + }) + } + companion object { + private const val TAG = "MessageActionsDialog" private const val ACTOR_LENGTH = 6 private const val NO_PREVIOUS_MESSAGE_ID: Int = -1 } diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt index 4822519751..8603991a41 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt @@ -122,9 +122,9 @@ class ShowReactionsDialog( val credentials = ApiUtils.getCredentials(userEntity?.username, userEntity?.token) - ncApi.getParticipantsForEmojiReaction( + ncApi.getReactions( credentials, - ApiUtils.getUrlForParticipantsForEmojiReaction( + ApiUtils.getUrlForMessageReaction( userEntity?.baseUrl, currentConversation!!.token, chatMessage.id diff --git a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java index a3987bbecc..9e3ea3c438 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -442,9 +442,9 @@ public static String getUrlForUserStatuses(String baseUrl) { return baseUrl + ocsApiVersion + "/apps/user_status/api/v1/statuses"; } - public static String getUrlForParticipantsForEmojiReaction(String baseUrl, - String roomToken, - String messageId) { + public static String getUrlForMessageReaction(String baseUrl, + String roomToken, + String messageId) { return baseUrl + ocsApiVersion + spreedApiVersion + "/reaction/" + roomToken + "/" + messageId; } diff --git a/app/src/main/res/layout/dialog_message_actions.xml b/app/src/main/res/layout/dialog_message_actions.xml index 39a4abe42d..1dd30d5e69 100644 --- a/app/src/main/res/layout/dialog_message_actions.xml +++ b/app/src/main/res/layout/dialog_message_actions.xml @@ -35,7 +35,7 @@ android:gravity="center_vertical" android:orientation="horizontal"> - - - - - -