forked from upstream-mirrors/quik
support message grouping
This commit is contained in:
@@ -2,6 +2,7 @@ package com.moez.QKSMS.ui.messages
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.dagger.AppComponentManager
|
||||
@@ -16,8 +17,10 @@ import javax.inject.Inject
|
||||
class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
||||
RealmRecyclerViewAdapter<Message, MessageViewHolder>(context, data, true) {
|
||||
|
||||
val VIEWTYPE_IN = 0
|
||||
val VIEWTYPE_OUT = 1
|
||||
companion object {
|
||||
private val VIEWTYPE_IN = 0
|
||||
private val VIEWTYPE_OUT = 1
|
||||
}
|
||||
|
||||
@Inject lateinit var dateFormatter: DateFormatter
|
||||
|
||||
@@ -27,21 +30,68 @@ class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MessageViewHolder {
|
||||
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||
val layoutRes = if (viewType == VIEWTYPE_IN) R.layout.message_list_item_in else R.layout.message_list_item_out
|
||||
val layoutRes = when (viewType) {
|
||||
VIEWTYPE_IN -> R.layout.message_list_item_in
|
||||
else -> R.layout.message_list_item_out
|
||||
}
|
||||
return MessageViewHolder(layoutInflater.inflate(layoutRes, parent, false))
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: MessageViewHolder, position: Int) {
|
||||
val message = getItem(position)!!
|
||||
|
||||
viewHolder.itemView.setOnClickListener {
|
||||
Timber.v(message.toString())
|
||||
}
|
||||
viewHolder.itemView.setOnClickListener { Timber.v(message.toString()) }
|
||||
|
||||
bindGrouping(viewHolder, position)
|
||||
|
||||
viewHolder.avatar.contact = Contact()
|
||||
viewHolder.body.text = message.body
|
||||
viewHolder.timestamp.text = dateFormatter.getMessageTimestamp(message.date)
|
||||
}
|
||||
|
||||
private fun bindGrouping(viewHolder: MessageViewHolder, position: Int) {
|
||||
|
||||
val message = getItem(position)!!
|
||||
val previous = if (position == 0) null else getItem(position - 1)
|
||||
val next = if (position == itemCount - 1) null else getItem(position + 1)
|
||||
|
||||
val sent = message.isMe()
|
||||
if (previous != null && next != null) { // Mid message
|
||||
if (previous.isMe() == sent && sent == next.isMe()) { // Mid in group
|
||||
viewHolder.body.setBackgroundResource(if (sent) R.drawable.message_out_middle else R.drawable.message_in_middle)
|
||||
viewHolder.avatar.visibility = View.INVISIBLE
|
||||
} else if (previous.isMe() == sent && sent != next.isMe()) { // Last in group
|
||||
viewHolder.body.setBackgroundResource(if (sent) R.drawable.message_out_last else R.drawable.message_in_last)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
} else if (previous.isMe() != sent && sent == next.isMe()) { // First in group
|
||||
viewHolder.body.setBackgroundResource(if (sent) R.drawable.message_out_first else R.drawable.message_in_first)
|
||||
viewHolder.avatar.visibility = View.INVISIBLE
|
||||
} else { // Only in group
|
||||
viewHolder.body.setBackgroundResource(R.drawable.message_only)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
}
|
||||
} else if (previous != null && next == null) { // Last message
|
||||
if (previous.isMe() == sent) { // Last in group
|
||||
viewHolder.body.setBackgroundResource(if (sent) R.drawable.message_out_last else R.drawable.message_in_last)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
} else { // Only in group
|
||||
viewHolder.body.setBackgroundResource(R.drawable.message_only)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
}
|
||||
} else if (previous == null && next != null) { // First message
|
||||
if (sent == next.isMe()) { // First in group
|
||||
viewHolder.body.setBackgroundResource(if (sent) R.drawable.message_out_first else R.drawable.message_in_first)
|
||||
viewHolder.avatar.visibility = View.INVISIBLE
|
||||
} else { // Only in group
|
||||
viewHolder.body.setBackgroundResource(R.drawable.message_only)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
}
|
||||
} else { // Only message
|
||||
viewHolder.body.setBackgroundResource(R.drawable.message_only)
|
||||
viewHolder.avatar.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
val message = getItem(position)!!
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="start|bottom"
|
||||
android:orientation="horizontal"
|
||||
android:paddingBottom="8dp"
|
||||
android:paddingBottom="2dp"
|
||||
android:paddingEnd="60dp"
|
||||
android:paddingStart="12dp"
|
||||
android:paddingTop="8dp">
|
||||
android:paddingTop="2dp">
|
||||
|
||||
<com.moez.QKSMS.ui.view.AvatarView
|
||||
android:id="@+id/avatar"
|
||||
@@ -29,6 +29,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="start"
|
||||
android:background="@drawable/message_only"
|
||||
android:backgroundTint="@android:color/white"
|
||||
android:gravity="start|center_vertical"
|
||||
android:minHeight="36dp"
|
||||
android:paddingBottom="8dp"
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="end|bottom"
|
||||
android:orientation="horizontal"
|
||||
android:paddingBottom="8dp"
|
||||
android:paddingBottom="2dp"
|
||||
android:paddingEnd="12dp"
|
||||
android:paddingStart="60dp"
|
||||
android:paddingTop="8dp">
|
||||
android:paddingTop="2dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
|
||||
Reference in New Issue
Block a user