From 4abe8ae41da2471bc6d61f9cbcbd6f51a5b63926 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 4 Apr 2022 19:37:28 +0200 Subject: [PATCH] add details view for reactions (WIP) TODO: encoding for emoji is wrong to send it to server Signed-off-by: Marcel Hibbe --- .../adapters/messages/ReactionsInterface.kt | 2 +- .../java/com/nextcloud/talk/api/NcApi.java | 6 + .../talk/controllers/ChatController.kt | 8 +- .../models/json/reactions/ReactionVoter.kt | 42 ++++++ .../models/json/reactions/ReactionsOCS.kt | 36 +++++ .../models/json/reactions/ReactionsOverall.kt | 35 +++++ .../talk/ui/dialog/ShowReactionsDialog.kt | 126 +++++++++++++++++- .../com/nextcloud/talk/utils/ApiUtils.java | 7 + .../res/layout/dialog_message_reactions.xml | 39 +++--- .../layout/rv_item_emoji_details_contact.xml | 54 ++++++++ 10 files changed, 331 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt create mode 100644 app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOCS.kt create mode 100644 app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOverall.kt create mode 100644 app/src/main/res/layout/rv_item_emoji_details_contact.xml diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/ReactionsInterface.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/ReactionsInterface.kt index e789a3af8..d6e9c47e9 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/ReactionsInterface.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/ReactionsInterface.kt @@ -3,5 +3,5 @@ package com.nextcloud.talk.adapters.messages import com.nextcloud.talk.models.json.chat.ChatMessage interface ReactionsInterface { - fun onClickReactions(message: ChatMessage) + fun onClickReactions(chatMessage: ChatMessage) } 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 f8d0260a9..aeb5ea461 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -37,6 +37,7 @@ import com.nextcloud.talk.models.json.notifications.NotificationOverall; import com.nextcloud.talk.models.json.participants.AddParticipantOverall; import com.nextcloud.talk.models.json.participants.ParticipantsOverall; import com.nextcloud.talk.models.json.push.PushRegistrationOverall; +import com.nextcloud.talk.models.json.reactions.ReactionsOverall; import com.nextcloud.talk.models.json.search.ContactsByNumberOverall; import com.nextcloud.talk.models.json.signaling.SignalingOverall; import com.nextcloud.talk.models.json.signaling.settings.SignalingSettingsOverall; @@ -62,6 +63,7 @@ import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.Header; +import retrofit2.http.Headers; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.PUT; @@ -488,4 +490,8 @@ public interface NcApi { @GET Observable getUserStatuses(@Header("Authorization") String authorization, @Url String url); + @GET + Observable getParticipantsForEmojiReaction(@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 9fffc106e..bf753cb02 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -923,11 +923,13 @@ class ChatController(args: Bundle) : } override fun onClickReactions(chatMessage: ChatMessage) { - Log.d(TAG, "onClickReactions" + chatMessage) - activity?.let { ShowReactionsDialog( - activity!! + activity!!, + currentConversation, + chatMessage, + conversationUser, + ncApi!! ).show() } } diff --git a/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt new file mode 100644 index 000000000..5014db144 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionVoter.kt @@ -0,0 +1,42 @@ +/* + * + * Nextcloud Talk application + * + * @author Marcel Hibbe + * Copyright (C) 2022 Marcel Hibbe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.models.json.reactions + +import android.os.Parcelable +import com.bluelinelabs.logansquare.annotation.JsonField +import com.bluelinelabs.logansquare.annotation.JsonObject +import kotlinx.android.parcel.Parcelize + +@Parcelize +@JsonObject +data class ReactionVoter( + @JsonField(name = ["actorType"]) + var actorType: String?, + @JsonField(name = ["actorId"]) + var actorId: String?, + @JsonField(name = ["actorDisplayName"]) + var actorDisplayName: String?, + @JsonField(name = ["timestamp"]) + var timestamp: Long = 0 +) : Parcelable { + // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' + constructor() : this(null, null, null, 0) +} diff --git a/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOCS.kt b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOCS.kt new file mode 100644 index 000000000..160528298 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOCS.kt @@ -0,0 +1,36 @@ +/* + * Nextcloud Talk application + * + * @author Marcel Hibbe + * Copyright (C) 2022 Marcel Hibbe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.models.json.reactions + +import android.os.Parcelable +import com.bluelinelabs.logansquare.annotation.JsonField +import com.bluelinelabs.logansquare.annotation.JsonObject +import com.nextcloud.talk.models.json.generic.GenericOCS +import kotlinx.android.parcel.Parcelize + +@Parcelize +@JsonObject +data class ReactionsOCS( + @JsonField(name = ["data"]) + var data: List? +) : GenericOCS(), Parcelable { + // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' + constructor() : this(null) +} diff --git a/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOverall.kt b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOverall.kt new file mode 100644 index 000000000..6a9d88fcd --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/models/json/reactions/ReactionsOverall.kt @@ -0,0 +1,35 @@ +/* + * Nextcloud Talk application + * + * @author Marcel Hibbe + * Copyright (C) 2022 Marcel Hibbe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.models.json.reactions + +import android.os.Parcelable +import com.bluelinelabs.logansquare.annotation.JsonField +import com.bluelinelabs.logansquare.annotation.JsonObject +import kotlinx.android.parcel.Parcelize + +@Parcelize +@JsonObject +data class ReactionsOverall( + @JsonField(name = ["ocs"]) + var ocs: ReactionsOCS? +) : Parcelable { + // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' + constructor() : this(null) +} 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 26a3b8f63..f9d034569 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 @@ -2,20 +2,138 @@ package com.nextcloud.talk.ui.dialog import android.app.Activity import android.os.Bundle +import android.util.Log +import android.view.Gravity import android.view.ViewGroup +import android.widget.LinearLayout +import androidx.annotation.NonNull +import autodagger.AutoInjector import com.google.android.material.bottomsheet.BottomSheetDialog +import com.nextcloud.talk.api.NcApi +import com.nextcloud.talk.application.NextcloudTalkApplication 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.ReactionVoter +import com.nextcloud.talk.models.json.reactions.ReactionsOverall +import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DisplayUtils +import com.vanniktech.emoji.EmojiTextView +import io.reactivex.Observer +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers +import org.apache.commons.lang3.StringEscapeUtils +import java.net.URLDecoder +import java.net.URLEncoder +import javax.inject.Inject +@AutoInjector(NextcloudTalkApplication::class) class ShowReactionsDialog( - val activity: Activity + val activity: Activity, + val currentConversation: Conversation?, + val chatMessage: ChatMessage, + val userEntity: UserEntity?, + val ncApi: NcApi ) : BottomSheetDialog(activity) { - private lateinit var dialogMessageReactionsBinding: DialogMessageReactionsBinding + private lateinit var binding: DialogMessageReactionsBinding + + // @Inject + // lateinit var ncApi: NcApi override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - dialogMessageReactionsBinding = DialogMessageReactionsBinding.inflate(layoutInflater) - setContentView(dialogMessageReactionsBinding.root) + binding = DialogMessageReactionsBinding.inflate(layoutInflater) + setContentView(binding.root) window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + + initEmojiReactions() + } + + private fun initEmojiReactions() { + if (chatMessage.reactions != null && chatMessage.reactions.isNotEmpty()) { + for ((emoji, amount) in chatMessage.reactions) { + var emojiView = EmojiTextView(activity) + emojiView.setEmojiSize(DisplayUtils.convertDpToPixel(EMOJI_SIZE, context).toInt()) + emojiView.text = emoji + + val params = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + params.setMargins(0, 0, EMOJI_RIGHT_MARGIN, 0) + params.gravity = Gravity.CENTER + emojiView.layoutParams = params + + binding.emojiReactionsWrapper.addView(emojiView) + + emojiView.setOnClickListener { + updateParticipantsForEmoji(chatMessage, emoji) + } + } + } + } + + private fun updateParticipantsForEmoji(chatMessage: ChatMessage, emoji: String?) { + + val credentials = ApiUtils.getCredentials(userEntity?.username, userEntity?.token) + + Log.d(TAG, "emoji= " + emoji) + + // TODO: fix encoding for emoji. set this in NcApi or here... + + // var emojiForServer = StringEscapeUtils.escapeJava(emoji) + // Log.d(TAG, "emojiForServer= " + emojiForServer) // ?reaction=%5Cu2764%5CuFE0F + + ncApi.getParticipantsForEmojiReaction( + credentials, + ApiUtils.getUrlForParticipantsForEmojiReaction( + userEntity?.baseUrl, + currentConversation!!.token, + chatMessage.id), + emoji + ) + ?.subscribeOn(Schedulers.io()) + ?.observeOn(AndroidSchedulers.mainThread()) + ?.subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + Log.d(TAG, "onSubscribe") + } + + override fun onNext(@NonNull reactionsOverall: ReactionsOverall) { + Log.d(TAG, "onNext") + + val reactionVoters: ArrayList = ArrayList() + if (reactionsOverall.ocs?.data != null){ + for (reactionVoter in reactionsOverall.ocs?.data!!) { + reactionVoters.add(reactionVoter) + } + } else { + Log.e(TAG, "no voters for this reaction") + } + } + + override fun onError(e: Throwable) { + Log.e(TAG, "failed to retrieve list of reaction voters") + } + + override fun onComplete() { + Log.d(TAG, "onComplete") + } + }) + + + + + // binding.emojiReactionsContactList + } + + companion object { + const val TAG = "ShowReactionsDialog" + const val EMOJI_RIGHT_MARGIN: Int = 12 + const val EMOJI_SIZE: Float = 30F } } 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 a2cf1dcc8..a3987bbec 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -441,4 +441,11 @@ public class ApiUtils { 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) { + return baseUrl + ocsApiVersion + spreedApiVersion + "/reaction/" + roomToken + "/" + messageId; + } + } diff --git a/app/src/main/res/layout/dialog_message_reactions.xml b/app/src/main/res/layout/dialog_message_reactions.xml index 5d585b7d1..d61e1ebd7 100644 --- a/app/src/main/res/layout/dialog_message_reactions.xml +++ b/app/src/main/res/layout/dialog_message_reactions.xml @@ -1,8 +1,8 @@ - + tools:listitem="@layout/rv_item_emoji_details_contact" + tools:itemCount="4"/> + + + + + + + diff --git a/app/src/main/res/layout/rv_item_emoji_details_contact.xml b/app/src/main/res/layout/rv_item_emoji_details_contact.xml new file mode 100644 index 000000000..06aa6b78e --- /dev/null +++ b/app/src/main/res/layout/rv_item_emoji_details_contact.xml @@ -0,0 +1,54 @@ + + + + + + + + +