From 4d29a4b63866ad85f8acef87b11426b755fdfbae Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 27 May 2021 17:35:19 +0200 Subject: [PATCH] add LocationMessageViewHolder for ChatKit Signed-off-by: Marcel Hibbe --- .../messages/LocationMessageViewHolder.java | 78 +++++++++++++++++++ .../talk/controllers/ChatController.kt | 36 +++++++-- .../talk/interfaces/ExtendedIMessage.kt | 11 +++ .../talk/models/json/chat/ChatMessage.java | 23 ++++-- .../layout/item_custom_location_message.xml | 45 +++++++++++ 5 files changed, 183 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/adapters/messages/LocationMessageViewHolder.java create mode 100644 app/src/main/java/com/nextcloud/talk/interfaces/ExtendedIMessage.kt create mode 100644 app/src/main/res/layout/item_custom_location_message.xml diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/LocationMessageViewHolder.java b/app/src/main/java/com/nextcloud/talk/adapters/messages/LocationMessageViewHolder.java new file mode 100644 index 000000000..8c146296e --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/LocationMessageViewHolder.java @@ -0,0 +1,78 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * @author Marcel Hibbe + * Copyright (C) 2017-2018 Mario Danic + * Copyright (C) 2021 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.adapters.messages; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.util.Log; +import android.view.View; +import android.widget.TextView; + +import com.nextcloud.talk.R; +import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.nextcloud.talk.models.json.chat.ChatMessage; +import com.stfalcon.chatkit.messages.MessageHolders; + +import javax.inject.Inject; + +import autodagger.AutoInjector; +import butterknife.BindView; +import butterknife.ButterKnife; +import okhttp3.OkHttpClient; + +@AutoInjector(NextcloudTalkApplication.class) +public class LocationMessageViewHolder extends MessageHolders.IncomingTextMessageViewHolder { + + private static String TAG = "LocationMessageViewHolder"; + + @BindView(R.id.locationText) + TextView messageText; + + View progressBar; + + @Inject + Context context; + + @Inject + OkHttpClient okHttpClient; + + public LocationMessageViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + progressBar = itemView.findViewById(R.id.progress_bar); + NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); + } + + @SuppressLint("SetTextI18n") + @Override + public void onBind(ChatMessage message) { + super.onBind(message); + + if (message.getMessageType() == ChatMessage.MessageType.SINGLE_NC_GEOLOCATION_MESSAGE) { + Log.d(TAG, "handle geolocation here"); + messageText.setText("geolocation..."); + } + +// text.setText("bbbbbb"); + } +} 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 1b7afd498..435ed4a17 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -79,6 +79,7 @@ import com.google.android.flexbox.FlexboxLayout import com.nextcloud.talk.R import com.nextcloud.talk.activities.LeafletWebView import com.nextcloud.talk.activities.MagicCallActivity +import com.nextcloud.talk.adapters.messages.LocationMessageViewHolder import com.nextcloud.talk.adapters.messages.MagicIncomingTextMessageViewHolder import com.nextcloud.talk.adapters.messages.MagicOutcomingTextMessageViewHolder import com.nextcloud.talk.adapters.messages.MagicPreviewMessageViewHolder @@ -134,6 +135,9 @@ import com.otaliastudios.autocomplete.Autocomplete import com.stfalcon.chatkit.commons.ImageLoader import com.stfalcon.chatkit.commons.models.IMessage import com.stfalcon.chatkit.messages.MessageHolders +import com.stfalcon.chatkit.messages.MessageHolders.ContentChecker +import com.stfalcon.chatkit.messages.MessageInput +import com.stfalcon.chatkit.messages.MessagesList import com.stfalcon.chatkit.messages.MessagesListAdapter import com.stfalcon.chatkit.utils.DateFormatter import com.vanniktech.emoji.EmojiPopup @@ -164,7 +168,7 @@ class ChatController(args: Bundle) : MessagesListAdapter.OnLoadMoreListener, MessagesListAdapter.Formatter, MessagesListAdapter.OnMessageViewLongClickListener, - MessageHolders.ContentChecker { + ContentChecker { private val binding: ControllerChatBinding by viewBinding(ControllerChatBinding::bind) @Inject @@ -406,9 +410,20 @@ class ChatController(args: Bundle) : R.layout.item_custom_outcoming_preview_message ) + // messageHolders.setIncomingLocationConfig( + // LocationMessageViewHolder::class.java, + // R.layout.item_custom_location_message + // ) + // messageHolders.setOutcomingLocationConfig( + // LocationMessageViewHolder::class.java, + // R.layout.item_custom_location_message + // ) + messageHolders.registerContentType( - CONTENT_TYPE_SYSTEM_MESSAGE, MagicSystemMessageViewHolder::class.java, - R.layout.item_system_message, MagicSystemMessageViewHolder::class.java, + CONTENT_TYPE_SYSTEM_MESSAGE, + MagicSystemMessageViewHolder::class.java, + R.layout.item_system_message, + MagicSystemMessageViewHolder::class.java, R.layout.item_system_message, this ) @@ -421,6 +436,15 @@ class ChatController(args: Bundle) : R.layout.item_date_header, this ) + messageHolders.registerContentType( + CONTENT_TYPE_LOCATION, + LocationMessageViewHolder::class.java, + R.layout.item_custom_location_message, + LocationMessageViewHolder::class.java, + R.layout.item_custom_location_message, + this + ) + var senderId = "" if (!conversationUser?.userId.equals("?")) { senderId = "users/" + conversationUser?.userId @@ -794,7 +818,7 @@ class ChatController(args: Bundle) : ) } - fun showShareLocationScreen(){ + fun showShareLocationScreen() { Log.d(TAG, "showShareLocationScreen") // val bundle = Bundle() @@ -1897,8 +1921,9 @@ class ChatController(args: Bundle) : return true } - override fun hasContentFor(message: IMessage, type: Byte): Boolean { + override fun hasContentFor(message: ChatMessage, type: Byte): Boolean { return when (type) { + CONTENT_TYPE_LOCATION -> return message.isLocationMessage() CONTENT_TYPE_SYSTEM_MESSAGE -> !TextUtils.isEmpty(message.systemMessage) CONTENT_TYPE_UNREAD_NOTICE_MESSAGE -> message.id == "-1" else -> false @@ -2004,6 +2029,7 @@ class ChatController(args: Bundle) : private const val TAG = "ChatController" private const val CONTENT_TYPE_SYSTEM_MESSAGE: Byte = 1 private const val CONTENT_TYPE_UNREAD_NOTICE_MESSAGE: Byte = 2 + private const val CONTENT_TYPE_LOCATION: Byte = 3 private const val NEW_MESSAGES_POPUP_BUBBLE_DELAY: Long = 200 private const val POP_CURRENT_CONTROLLER_DELAY: Long = 100 private const val LOBBY_TIMER_DELAY: Long = 5000 diff --git a/app/src/main/java/com/nextcloud/talk/interfaces/ExtendedIMessage.kt b/app/src/main/java/com/nextcloud/talk/interfaces/ExtendedIMessage.kt new file mode 100644 index 000000000..5af22bb37 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/interfaces/ExtendedIMessage.kt @@ -0,0 +1,11 @@ +package com.nextcloud.talk.interfaces + +import com.stfalcon.chatkit.commons.models.IMessage + +interface ExtendedIMessage : IMessage { + + // var isLocationMessage: Boolean + + fun isLocationMessage() : Boolean + +} \ No newline at end of file diff --git a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java index 676ace0ac..7d7158ed7 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java +++ b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java @@ -20,17 +20,18 @@ package com.nextcloud.talk.models.json.chat; import android.text.TextUtils; +import android.util.Log; import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonIgnore; import com.bluelinelabs.logansquare.annotation.JsonObject; import com.nextcloud.talk.R; import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.nextcloud.talk.interfaces.ExtendedIMessage; import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.models.json.converters.EnumSystemMessageTypeConverter; import com.nextcloud.talk.utils.ApiUtils; import com.nextcloud.talk.utils.TextMatchers; -import com.stfalcon.chatkit.commons.models.IMessage; import com.stfalcon.chatkit.commons.models.IUser; import com.stfalcon.chatkit.commons.models.MessageContentType; @@ -46,7 +47,9 @@ import androidx.annotation.Nullable; @Parcel @JsonObject -public class ChatMessage implements IMessage, MessageContentType, MessageContentType.Image { +public class ChatMessage implements ExtendedIMessage, MessageContentType, MessageContentType.Image { + private final String TAG = "ChatMessage"; + @JsonIgnore public boolean isGrouped; @JsonIgnore @@ -87,9 +90,13 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent public Enum readStatus = ReadStatus.NONE; @JsonIgnore - List messageTypesToIgnore = Arrays.asList(MessageType.REGULAR_TEXT_MESSAGE, - MessageType.SYSTEM_MESSAGE, MessageType.SINGLE_LINK_VIDEO_MESSAGE, - MessageType.SINGLE_LINK_AUDIO_MESSAGE, MessageType.SINGLE_LINK_MESSAGE); + List messageTypesToIgnore = Arrays.asList( + MessageType.REGULAR_TEXT_MESSAGE, + MessageType.SYSTEM_MESSAGE, + MessageType.SINGLE_LINK_VIDEO_MESSAGE, + MessageType.SINGLE_LINK_AUDIO_MESSAGE, + MessageType.SINGLE_LINK_MESSAGE, + MessageType.SINGLE_NC_GEOLOCATION_MESSAGE); public boolean hasFileAttachment() { if (messageParameters != null && messageParameters.size() > 0) { @@ -108,6 +115,7 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent for (String key : messageParameters.keySet()) { Map individualHashMap = messageParameters.get(key); if (individualHashMap.get("type").equals("geo-location")) { + Log.d(TAG, "is geo-location"); return true; } } @@ -555,6 +563,11 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent return "ChatMessage(isGrouped=" + this.isGrouped() + ", isOneToOneConversation=" + this.isOneToOneConversation() + ", activeUser=" + this.getActiveUser() + ", selectedIndividualHashMap=" + this.getSelectedIndividualHashMap() + ", isLinkPreviewAllowed=" + this.isLinkPreviewAllowed() + ", isDeleted=" + this.isDeleted() + ", jsonMessageId=" + this.getJsonMessageId() + ", token=" + this.getToken() + ", actorType=" + this.getActorType() + ", actorId=" + this.getActorId() + ", actorDisplayName=" + this.getActorDisplayName() + ", timestamp=" + this.getTimestamp() + ", message=" + this.getMessage() + ", messageParameters=" + this.getMessageParameters() + ", systemMessageType=" + this.getSystemMessageType() + ", replyable=" + this.isReplyable() + ", parentMessage=" + this.getParentMessage() + ", readStatus=" + this.getReadStatus() + ", messageTypesToIgnore=" + this.getMessageTypesToIgnore() + ")"; } + @Override + public boolean isLocationMessage() { + return hasGeoLocation(); + } + public enum MessageType { REGULAR_TEXT_MESSAGE, SYSTEM_MESSAGE, diff --git a/app/src/main/res/layout/item_custom_location_message.xml b/app/src/main/res/layout/item_custom_location_message.xml new file mode 100644 index 000000000..ae10d7579 --- /dev/null +++ b/app/src/main/res/layout/item_custom_location_message.xml @@ -0,0 +1,45 @@ + + + + + + +