From 16d03a69ff3cc8c348ac1441dc57682d59f773ac Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 21 Nov 2024 17:59:43 +0100 Subject: [PATCH] show guest "first letter" avatars in chat also: create ChatMessageUtils as helper for the ViewHolders in this case. Could help to avoid duplicated code until there is a clear inheritance solution. Signed-off-by: Marcel Hibbe --- .../messages/IncomingDeckCardViewHolder.kt | 16 +------- .../IncomingLinkPreviewMessageViewHolder.kt | 16 +------- .../messages/IncomingPollMessageViewHolder.kt | 16 +------- .../messages/IncomingTextMessageViewHolder.kt | 18 ++------- .../IncomingVoiceMessageViewHolder.kt | 16 +------- .../nextcloud/talk/utils/ChatMessageUtils.kt | 37 +++++++++++++++++++ 6 files changed, 49 insertions(+), 70 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/utils/ChatMessageUtils.kt diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingDeckCardViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingDeckCardViewHolder.kt index aad20fdb3..771001cca 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingDeckCardViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingDeckCardViewHolder.kt @@ -31,6 +31,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.extensions.loadFederatedUserAvatar import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ChatMessageUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.message.MessageUtils import com.nextcloud.talk.utils.preferences.AppPreferences @@ -168,7 +169,7 @@ class IncomingDeckCardViewHolder(incomingView: View, payload: Any) : MessageHold } if (!message.isGrouped && !message.isOneToOneConversation && !message.isFormerOneToOneConversation) { - setAvatarOnMessage(message) + ChatMessageUtils().setAvatarOnMessage(binding.messageUserAvatar, message, viewThemeUtils) } else { if (message.isOneToOneConversation || message.isFormerOneToOneConversation) { binding.messageUserAvatar.visibility = View.GONE @@ -179,19 +180,6 @@ class IncomingDeckCardViewHolder(incomingView: View, payload: Any) : MessageHold } } - private fun setAvatarOnMessage(message: ChatMessage) { - binding.messageUserAvatar.visibility = View.VISIBLE - if (message.actorType == "guests") { - // do nothing, avatar is set - } else if (message.actorType == "bots" && message.actorId == "changelog") { - binding.messageUserAvatar.loadChangelogBotAvatar() - } else if (message.actorType == "bots") { - binding.messageUserAvatar.loadBotsAvatar() - } else if (message.actorType == "federated_users") { - binding.messageUserAvatar.loadFederatedUserAvatar(message) - } - } - private fun colorizeMessageBubble(message: ChatMessage) { viewThemeUtils.talk.themeIncomingMessageBubble(bubble, message.isGrouped, message.isDeleted) } diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt index 0657113f7..53f6506a6 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt @@ -28,6 +28,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.extensions.loadFederatedUserAvatar import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ChatMessageUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.message.MessageUtils import com.nextcloud.talk.utils.preferences.AppPreferences @@ -146,7 +147,7 @@ class IncomingLinkPreviewMessageViewHolder(incomingView: View, payload: Any) : } if (!message.isGrouped && !message.isOneToOneConversation && !message.isFormerOneToOneConversation) { - setAvatarOnMessage(message) + ChatMessageUtils().setAvatarOnMessage(binding.messageUserAvatar, message, viewThemeUtils) } else { if (message.isOneToOneConversation || message.isFormerOneToOneConversation) { binding.messageUserAvatar.visibility = View.GONE @@ -157,19 +158,6 @@ class IncomingLinkPreviewMessageViewHolder(incomingView: View, payload: Any) : } } - private fun setAvatarOnMessage(message: ChatMessage) { - binding.messageUserAvatar.visibility = View.VISIBLE - if (message.actorType == "guests") { - // do nothing, avatar is set - } else if (message.actorType == "bots" && message.actorId == "changelog") { - binding.messageUserAvatar.loadChangelogBotAvatar() - } else if (message.actorType == "bots") { - binding.messageUserAvatar.loadBotsAvatar() - } else if (message.actorType == "federated_users") { - binding.messageUserAvatar.loadFederatedUserAvatar(message) - } - } - private fun colorizeMessageBubble(message: ChatMessage) { viewThemeUtils.talk.themeIncomingMessageBubble(bubble, message.isGrouped, message.isDeleted) } diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt index cd8a46824..59b626df8 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt @@ -28,6 +28,7 @@ import com.nextcloud.talk.extensions.loadFederatedUserAvatar import com.nextcloud.talk.polls.ui.PollMainDialogFragment import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ChatMessageUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.message.MessageUtils import com.nextcloud.talk.utils.preferences.AppPreferences @@ -153,7 +154,7 @@ class IncomingPollMessageViewHolder(incomingView: View, payload: Any) : } if (!message.isGrouped && !message.isOneToOneConversation && !message.isFormerOneToOneConversation) { - setAvatarOnMessage(message) + ChatMessageUtils().setAvatarOnMessage(binding.messageUserAvatar, message, viewThemeUtils) } else { if (message.isOneToOneConversation || message.isFormerOneToOneConversation) { binding.messageUserAvatar.visibility = View.GONE @@ -164,19 +165,6 @@ class IncomingPollMessageViewHolder(incomingView: View, payload: Any) : } } - private fun setAvatarOnMessage(message: ChatMessage) { - binding.messageUserAvatar.visibility = View.VISIBLE - if (message.actorType == "guests") { - // do nothing, avatar is set - } else if (message.actorType == "bots" && message.actorId == "changelog") { - binding.messageUserAvatar.loadChangelogBotAvatar() - } else if (message.actorType == "bots") { - binding.messageUserAvatar.loadBotsAvatar() - } else if (message.actorType == "federated_users") { - binding.messageUserAvatar.loadFederatedUserAvatar(message) - } - } - private fun colorizeMessageBubble(message: ChatMessage) { viewThemeUtils.talk.themeIncomingMessageBubble(bubble, message.isGrouped, message.isDeleted) } diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt index 6a8ea3280..f71b6f150 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt @@ -26,9 +26,12 @@ import com.nextcloud.talk.chat.data.model.ChatMessage import com.nextcloud.talk.databinding.ItemCustomIncomingTextMessageBinding import com.nextcloud.talk.extensions.loadBotsAvatar import com.nextcloud.talk.extensions.loadChangelogBotAvatar +import com.nextcloud.talk.extensions.loadDefaultAvatar import com.nextcloud.talk.extensions.loadFederatedUserAvatar +import com.nextcloud.talk.extensions.loadFirstLetterAvatar import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ChatMessageUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.TextMatchers import com.nextcloud.talk.utils.message.MessageUtils @@ -153,7 +156,7 @@ class IncomingTextMessageViewHolder(itemView: View, payload: Any) : } if (!message.isGrouped && !message.isOneToOneConversation && !message.isFormerOneToOneConversation) { - setAvatarOnMessage(message) + ChatMessageUtils().setAvatarOnMessage(binding.messageUserAvatar, message, viewThemeUtils) } else { if (message.isOneToOneConversation || message.isFormerOneToOneConversation) { binding.messageUserAvatar.visibility = View.GONE @@ -164,19 +167,6 @@ class IncomingTextMessageViewHolder(itemView: View, payload: Any) : } } - private fun setAvatarOnMessage(message: ChatMessage) { - binding.messageUserAvatar.visibility = View.VISIBLE - if (message.actorType == "guests") { - // do nothing, avatar is set - } else if (message.actorType == "bots" && message.actorId == "changelog") { - binding.messageUserAvatar.loadChangelogBotAvatar() - } else if (message.actorType == "bots") { - binding.messageUserAvatar.loadBotsAvatar() - } else if (message.actorType == "federated_users") { - binding.messageUserAvatar.loadFederatedUserAvatar(message) - } - } - private fun colorizeMessageBubble(message: ChatMessage) { viewThemeUtils.talk.themeIncomingMessageBubble(bubble, message.isGrouped, message.isDeleted) } diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt index 0ba927370..36155bf09 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt @@ -32,6 +32,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.extensions.loadFederatedUserAvatar import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ChatMessageUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.message.MessageUtils import com.nextcloud.talk.utils.preferences.AppPreferences @@ -249,7 +250,7 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) : } if (!message.isGrouped && !message.isOneToOneConversation && !message.isFormerOneToOneConversation) { - setAvatarOnMessage(message) + ChatMessageUtils().setAvatarOnMessage(binding.messageUserAvatar, message, viewThemeUtils) } else { if (message.isOneToOneConversation || message.isFormerOneToOneConversation) { binding.messageUserAvatar.visibility = View.GONE @@ -260,19 +261,6 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) : } } - private fun setAvatarOnMessage(message: ChatMessage) { - binding.messageUserAvatar.visibility = View.VISIBLE - if (message.actorType == "guests") { - // do nothing, avatar is set - } else if (message.actorType == "bots" && message.actorId == "changelog") { - binding.messageUserAvatar.loadChangelogBotAvatar() - } else if (message.actorType == "bots") { - binding.messageUserAvatar.loadBotsAvatar() - } else if (message.actorType == "federated_users") { - binding.messageUserAvatar.loadFederatedUserAvatar(message) - } - } - private fun colorizeMessageBubble(message: ChatMessage) { viewThemeUtils.talk.themeIncomingMessageBubble(bubble, message.isGrouped, message.isDeleted) } diff --git a/app/src/main/java/com/nextcloud/talk/utils/ChatMessageUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/ChatMessageUtils.kt new file mode 100644 index 000000000..92a8827d8 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/ChatMessageUtils.kt @@ -0,0 +1,37 @@ +/* + * Nextcloud Talk - Android Client + * + * SPDX-FileCopyrightText: 2024 Marcel Hibbe + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package com.nextcloud.talk.utils + +import android.view.View +import android.widget.ImageView +import com.nextcloud.talk.chat.data.model.ChatMessage +import com.nextcloud.talk.extensions.loadBotsAvatar +import com.nextcloud.talk.extensions.loadChangelogBotAvatar +import com.nextcloud.talk.extensions.loadDefaultAvatar +import com.nextcloud.talk.extensions.loadFederatedUserAvatar +import com.nextcloud.talk.extensions.loadFirstLetterAvatar +import com.nextcloud.talk.ui.theme.ViewThemeUtils + +class ChatMessageUtils { + + fun setAvatarOnMessage(view: ImageView, message: ChatMessage, viewThemeUtils : ViewThemeUtils) { + view.visibility = View.VISIBLE + if (message.actorType == "guests" || message.actorType == "emails") { + if (message.actorDisplayName?.isNotEmpty() == true) { + view.loadFirstLetterAvatar(message.actorDisplayName?.first().toString()) + } else { + view.loadDefaultAvatar(viewThemeUtils) + } + } else if (message.actorType == "bots" && message.actorId == "changelog") { + view.loadChangelogBotAvatar() + } else if (message.actorType == "bots") { + view.loadBotsAvatar() + } else if (message.actorType == "federated_users") { + view.loadFederatedUserAvatar(message) + } + } +}