Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

[WIP][NEW] Group same user messages #2299

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ abstract class BaseViewHolder<T : BaseUiModel<*>>(
) : RecyclerView.ViewHolder(itemView),
MenuItem.OnMenuItemClickListener {
var data: T? = null
var groupMessage = false

init {
setupActionMenu(itemView)
}

fun bind(data: T) {
fun bind(data: T, groupMessage: Boolean = false) {
this.data = data
this.groupMessage = groupMessage
bindViews(data)
bindReactions()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chat.rocket.android.chatroom.adapter

import android.content.res.Resources
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
Expand All @@ -25,6 +26,9 @@ import chat.rocket.core.model.attachment.actions.ButtonAction
import chat.rocket.core.model.isSystemMessage
import timber.log.Timber
import java.security.InvalidParameterException
import java.util.Calendar
import kotlin.Comparator
import kotlin.collections.ArrayList

class ChatRoomAdapter(
private val roomId: String? = null,
Expand Down Expand Up @@ -109,9 +113,24 @@ class ChatRoomAdapter(
}
}

var groupMessage = false

if (position + 1 < dataSet.size) {
val a = dataSet[position].message
val b = dataSet[position + 1].message

groupMessage = shouldGroupMessage(a, b)

a.attachments?.let {
if (it.any { a -> a.type != null }) { // check if message media attachment.
groupMessage = false
}
}
}

when (holder) {
is MessageViewHolder ->
holder.bind(dataSet[position] as MessageUiModel)
holder.bind(dataSet[position] as MessageUiModel, groupMessage)
is UrlPreviewViewHolder -> {
holder.bind(dataSet[position] as UrlPreviewUiModel)
}
Expand All @@ -122,6 +141,19 @@ class ChatRoomAdapter(
}
}


private fun shouldGroupMessage(a: Message, b: Message): Boolean {
if (a.senderAlias == b.senderAlias) {
val date1 = a.getDate()
val date2 = b.getDate()

if (date1.isSameDay(date2) && date1.timeInMillis - date2.timeInMillis <= 900000) // this should be depend on settings.
return true
}

return false
}

override fun getItemId(position: Int): Long {
val model = dataSet[position]
return when (model) {
Expand Down Expand Up @@ -373,3 +405,18 @@ class ChatRoomAdapter(
fun openConfigurableWebPage(roomId: String, url: String, heightRatio: String)
}
}

fun Message.getDate(): Calendar {
return Calendar.getInstance().apply {
timeInMillis = [email protected]
}
}

fun Calendar.isSameDay(other: Calendar): Boolean {
return this.get(Calendar.YEAR) == other.get(Calendar.YEAR) &&
this.get(Calendar.MONTH) == other.get(Calendar.MONTH) &&
this.get(Calendar.DAY_OF_MONTH) == other.get(Calendar.DAY_OF_MONTH)
}

val Int.toPx: Float
get() = (this * Resources.getSystem().displayMetrics.density)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.text.Spannable
import android.text.method.LinkMovementMethod
import android.text.style.ImageSpan
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import chat.rocket.android.R
import chat.rocket.android.chatroom.uimodel.MessageUiModel
Expand Down Expand Up @@ -77,11 +79,31 @@ class MessageViewHolder(
read_receipt_view.isVisible = true
}

image_avatar.setOnClickListener {
data.message.sender?.id?.let { userId ->
avatarListener(userId)
if (!groupMessage) {
layout_avatar.isVisible = true
message_header.isVisible = true

(text_content.layoutParams as ViewGroup.MarginLayoutParams).apply {
marginStart = 16.toPx.toInt()
}

image_avatar.setOnClickListener {
data.message.sender?.id?.let { userId ->
avatarListener(userId)
}
}
} else {
layout_avatar.isGone = true
message_header.isGone = true

(text_content.layoutParams as ViewGroup.MarginLayoutParams).apply {
marginStart = 56.toPx.toInt()
}

image_avatar.setOnClickListener(null)
}


}
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/item_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@

<TextView
android:id="@+id/text_content"
android:layout_marginStart="16dp"
style="@style/Message.TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/message_header"
app:layout_constraintStart_toEndOf="@+id/layout_avatar"
app:layout_constraintTop_toBottomOf="@+id/message_header"
tools:text="This is a multiline chat message from Bertie that will take more than just one line of text. I have made sure that everything is amazing!" />

Expand Down