Added possibility to send reply & scroll to original message on click

This commit is contained in:
Sylvain Berfini 2023-10-26 10:22:43 +02:00
parent e8ca20a7e2
commit 85f97b86da
10 changed files with 228 additions and 7 deletions

View file

@ -53,6 +53,9 @@ class ConversationEventAdapter(
val showReactionForChatMessageModelEvent: MutableLiveData<Event<ChatMessageModel>> by lazy {
MutableLiveData<Event<ChatMessageModel>>()
}
val scrollToRepliedMessageEvent: MutableLiveData<Event<ChatMessageModel>> by lazy {
MutableLiveData<Event<ChatMessageModel>>()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
@ -129,6 +132,9 @@ class ConversationEventAdapter(
setShowReactionInfoClickListener {
showReactionForChatMessageModelEvent.value = Event(message)
}
setScrollToRepliedMessageClickListener {
scrollToRepliedMessageEvent.value = Event(message)
}
lifecycleOwner = viewLifecycleOwner
executePendingBindings()
@ -149,6 +155,9 @@ class ConversationEventAdapter(
setShowReactionInfoClickListener {
showReactionForChatMessageModelEvent.value = Event(message)
}
setScrollToRepliedMessageClickListener {
scrollToRepliedMessageEvent.value = Event(message)
}
lifecycleOwner = viewLifecycleOwner
executePendingBindings()

View file

@ -203,6 +203,26 @@ class ConversationFragment : GenericFragment() {
}
}
adapter.scrollToRepliedMessageEvent.observe(viewLifecycleOwner) {
it.consume { model ->
val repliedMessageId = model.replyToMessageId
if (repliedMessageId.isNullOrEmpty()) {
Log.w("$TAG Chat message [${model.id}] doesn't have a reply to ID!")
} else {
val originalMessage = adapter.currentList.find {
!it.isEvent && (it.model as ChatMessageModel).id == repliedMessageId
}
if (originalMessage != null) {
val position = adapter.currentList.indexOf(originalMessage)
Log.i("$TAG Scrolling to position [$position]")
binding.eventsList.scrollToPosition(position)
} else {
Log.w("$TAG Failed to find matching message in adapter's items!")
}
}
}
}
binding.setOpenFilePickerClickListener {
Log.i("$TAG Opening media picker")
pickMedia.launch(
@ -280,11 +300,13 @@ class ConversationFragment : GenericFragment() {
}
layout.setDeleteClickListener {
Log.i("$TAG Deleting message")
viewModel.deleteChatMessage(chatMessageModel)
dialog.dismiss()
}
layout.setCopyClickListener {
Log.i("$TAG Copying message text into clipboard")
val text = chatMessageModel.text
val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val label = "Message"
@ -294,10 +316,17 @@ class ConversationFragment : GenericFragment() {
}
layout.setPickEmojiClickListener {
Log.i("$TAG Opening emoji-picker for reaction")
val emojiSheetBehavior = BottomSheetBehavior.from(layout.emojiPickerBottomSheet.root)
emojiSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
layout.setReplyClickListener {
Log.i("$TAG Updating sending area to reply to selected message")
viewModel.replyToMessage(chatMessageModel)
dialog.dismiss()
}
layout.model = chatMessageModel
chatMessageModel.dismissLongPressMenuEvent.observe(viewLifecycleOwner) {
dialog.dismiss()

View file

@ -39,6 +39,7 @@ class ChatMessageModel @WorkerThread constructor(
val isFromGroup: Boolean,
val isReply: Boolean,
val replyText: String,
val replyToMessageId: String?,
val isGroupedWithPreviousOne: Boolean,
val isGroupedWithNextOne: Boolean
) {

View file

@ -64,6 +64,7 @@ class EventLogModel @WorkerThread constructor(
isFromGroup,
chatMessage.isReply,
reply,
chatMessage.replyMessageId,
isGroupedWithPreviousOne,
isGroupedWithNextOne
)

View file

@ -26,6 +26,7 @@ import androidx.lifecycle.ViewModel
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.core.Address
import org.linphone.core.ChatMessage
import org.linphone.core.ChatRoom
import org.linphone.core.ChatRoomListenerStub
import org.linphone.core.EventLog
@ -68,6 +69,12 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
val isEmojiPickerOpen = MutableLiveData<Boolean>()
val isReplying = MutableLiveData<Boolean>()
val isReplyingTo = MutableLiveData<String>()
val isReplyingToMessage = MutableLiveData<String>()
val requestKeyboardHidingEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
@ -80,6 +87,8 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
private lateinit var chatRoom: ChatRoom
private var chatMessageToReplyTo: ChatMessage? = null
private val chatRoomListener = object : ChatRoomListenerStub() {
@WorkerThread
override fun onChatMessageSending(chatRoom: ChatRoom, eventLog: EventLog) {
@ -237,10 +246,35 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
textToSend.value = "${textToSend.value.orEmpty()}$emoji"
}
@UiThread
fun replyToMessage(model: ChatMessageModel) {
coreContext.postOnCoreThread {
val message = model.chatMessage
Log.i("$TAG Pending reply to chat message [${message.messageId}]")
chatMessageToReplyTo = message
isReplyingTo.postValue(model.avatarModel.friend.name)
isReplyingToMessage.postValue(LinphoneUtils.getTextDescribingMessage(message))
isReplying.postValue(true)
}
}
@UiThread
fun cancelReply() {
Log.i("$TAG Cancelling reply")
isReplying.value = false
chatMessageToReplyTo = null
}
@UiThread
fun sendMessage() {
coreContext.postOnCoreThread { core ->
val message = chatRoom.createEmptyMessage()
coreContext.postOnCoreThread {
val messageToReplyTo = chatMessageToReplyTo
val message = if (messageToReplyTo != null) {
Log.i("$TAG Sending message as reply to [${messageToReplyTo.messageId}]")
chatRoom.createReplyMessage(messageToReplyTo)
} else {
chatRoom.createEmptyMessage()
}
val toSend = textToSend.value.orEmpty().trim()
if (toSend.isNotEmpty()) {
@ -251,7 +285,10 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
Log.i("$TAG Sending message")
message.send()
}
Log.i("$TAG Message sent, re-setting defaults")
textToSend.postValue("")
cancelReply()
}
}

View file

@ -13,6 +13,9 @@
<variable
name="showDeliveryInfoClickListener"
type="View.OnClickListener" />
<variable
name="scrollToRepliedMessageClickListener"
type="View.OnClickListener" />
<variable
name="showReactionInfoClickListener"
type="View.OnClickListener" />
@ -66,17 +69,32 @@
app:layout_constraintBottom_toTopOf="@id/reply"
app:layout_constraintStart_toStartOf="@id/background" />
<View
android:id="@+id/reply_background"
android:onClick="@{scrollToRepliedMessageClickListener}"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:background="@drawable/shape_chat_bubble_incoming_reply"
android:visibility="@{model.isReply ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toTopOf="@id/reply"
app:layout_constraintStart_toStartOf="@id/reply"
app:layout_constraintEnd_toEndOf="@id/reply"
app:layout_constraintBottom_toBottomOf="@id/background" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/reply"
android:onClick="@{scrollToRepliedMessageClickListener}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@{model.replyText, default=`Ceci est une réponse!`}"
android:textColor="@color/gray_main2_400"
android:textSize="14sp"
android:maxLines="2"
android:ellipsize="end"
android:padding="10dp"
android:background="@drawable/shape_chat_bubble_incoming_reply"
android:visibility="@{model.isReply ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="@id/background"

View file

@ -13,6 +13,9 @@
<variable
name="showDeliveryInfoClickListener"
type="View.OnClickListener" />
<variable
name="scrollToRepliedMessageClickListener"
type="View.OnClickListener" />
<variable
name="showReactionInfoClickListener"
type="View.OnClickListener" />
@ -37,6 +40,38 @@
app:barrierMargin="-10dp"
app:constraint_referenced_ids="date_time, text_message" />
<View
android:id="@+id/reply_background"
android:onClick="@{scrollToRepliedMessageClickListener}"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/shape_chat_bubble_incoming_reply"
android:visibility="@{model.isReply ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toTopOf="@id/reply"
app:layout_constraintStart_toStartOf="@id/reply"
app:layout_constraintEnd_toEndOf="@id/reply"
app:layout_constraintBottom_toBottomOf="@id/background" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:onClick="@{scrollToRepliedMessageClickListener}"
android:id="@+id/reply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@{model.replyText, default=`Ceci est une réponse!`}"
android:textColor="@color/gray_main2_400"
android:textSize="14sp"
android:maxLines="2"
android:ellipsize="end"
android:padding="10dp"
android:visibility="@{model.isReply ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="@id/background"
app:layout_constraintEnd_toEndOf="@id/background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/background"/>
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
@ -44,7 +79,7 @@
android:src="@{model.isGroupedWithPreviousOne ? @drawable/shape_chat_bubble_outgoing_full : @drawable/shape_chat_bubble_outgoing_first, default=@drawable/shape_chat_bubble_outgoing_first}"
app:layout_constraintStart_toStartOf="@id/background_start_barrier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/reply"
app:layout_constraintBottom_toBottomOf="@id/date_time"/>
<androidx.appcompat.widget.AppCompatTextView
@ -59,11 +94,11 @@
android:text="@{model.text, default=`Hi!`}"
android:textSize="14sp"
android:textColor="@color/gray_main2_700"
android:gravity="center_vertical|start"
android:gravity="center_vertical|end"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/reply"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView

View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="org.linphone.ui.main.chat.viewmodel.ConversationViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_main2_300"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/cancel"
android:onClick="@{() -> viewModel.cancelReply()}"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="15dp"
android:adjustViewBounds="true"
android:src="@drawable/x"
app:tint="@color/icon_color_selector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_300"
android:id="@+id/reply_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:text="@string/conversation_reply_to_message_title"
android:textSize="12sp"
android:textColor="@color/gray_main2_500"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_700"
android:id="@+id/reply_to_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="5dp"
android:text="@{viewModel.isReplyingTo, default=`John Doe`}"
android:textSize="12sp"
android:textColor="@color/gray_main2_500"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/reply_header"
app:layout_constraintEnd_toStartOf="@id/cancel" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_300"
android:id="@+id/reply_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="10dp"
android:text="@{viewModel.isReplyingToMessage, default=`Hello this is John! How are you?`}"
android:textSize="12sp"
android:textColor="@color/gray_main2_400"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintTop_toBottomOf="@id/reply_header"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/cancel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View file

@ -19,13 +19,20 @@
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!-- Keep behavior to have it at the bottom -->
<include
android:id="@+id/reply_area"
layout="@layout/chat_conversation_reply_area"
viewModel="@{viewModel}"
android:visibility="@{viewModel.isReplying ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.emoji2.emojipicker.EmojiPickerView
android:id="@+id/emoji_picker"
android:layout_width="match_parent"
android:layout_height="@dimen/chat_room_emoji_picker_height"
android:visibility="@{viewModel.isEmojiPickerOpen ? View.VISIBLE : View.GONE, default=gone}"
app:emojiPickedListener="@{(emoji) -> viewModel.insertEmoji(emoji.emoji)}"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintTop_toBottomOf="@id/reply_area"/>
<ImageView
android:id="@+id/emoji_picker_toggle"

View file

@ -356,6 +356,7 @@
<item quantity="other">%s are composing…</item>
</plurals>
<string name="conversation_add_participants_title">Add participants</string>
<string name="conversation_reply_to_message_title">Replying to:</string>
<string name="conversation_info_participants_list_title">Group members</string>
<string name="conversation_info_add_participants_label">Add participants</string>