Distinguish between control messages and visible messages

This commit is contained in:
nielsandriesse 2021-06-01 09:48:02 +10:00
parent 7ab9d3d8dc
commit 7710a2c32f
5 changed files with 110 additions and 14 deletions

View File

@ -3,30 +3,69 @@ package org.thoughtcrime.securesms.conversation.v2
import android.content.Context
import android.database.Cursor
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.thoughtcrime.securesms.conversation.v2.messages.MessageView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import org.thoughtcrime.securesms.conversation.v2.messages.ControlMessageView
import org.thoughtcrime.securesms.conversation.v2.messages.VisibleMessageView
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.model.MessageRecord
import java.lang.IllegalStateException
class ConversationAdapter(context: Context, cursor: Cursor) : CursorRecyclerViewAdapter<ConversationAdapter.ViewHolder>(context, cursor) {
class ConversationAdapter(context: Context, cursor: Cursor) : CursorRecyclerViewAdapter<ViewHolder>(context, cursor) {
private val messageDB = DatabaseFactory.getMmsSmsDatabase(context)
class ViewHolder(val view: MessageView) : RecyclerView.ViewHolder(view)
sealed class ViewType(val rawValue: Int) {
object Visible : ViewType(0)
object Control : ViewType(1)
companion object {
val allValues: Map<Int, ViewType> get() = mapOf(
Visible.rawValue to Visible,
Control.rawValue to Control
)
}
}
class VisibleMessageViewHolder(val view: VisibleMessageView) : ViewHolder(view)
class ControlMessageViewHolder(val view: ControlMessageView) : ViewHolder(view)
override fun getItemViewType(cursor: Cursor): Int {
val message = getMessage(cursor)!!
if (message.isExpirationTimerUpdate) { return ViewType.Control.rawValue }
return ViewType.Visible.rawValue
}
override fun onCreateItemViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = MessageView(context)
return ViewHolder(view)
@Suppress("NAME_SHADOWING")
val viewType = ViewType.allValues[viewType]
when (viewType) {
ViewType.Visible -> {
val view = VisibleMessageView(context)
return VisibleMessageViewHolder(view)
}
ViewType.Control -> {
val view = ControlMessageView(context)
return ControlMessageViewHolder(view)
}
else -> throw IllegalStateException("Unexpected view type: $viewType.")
}
}
override fun onBindItemViewHolder(viewHolder: ViewHolder, cursor: Cursor) {
val message = getMessage(cursor)!!
viewHolder.view.bind(message)
when (viewHolder) {
is VisibleMessageViewHolder -> viewHolder.view.bind(message)
is ControlMessageViewHolder -> viewHolder.view.bind(message)
}
}
override fun onItemViewRecycled(holder: ViewHolder?) {
holder?.view?.recycle()
super.onItemViewRecycled(holder)
override fun onItemViewRecycled(viewHolder: ViewHolder?) {
when (viewHolder) {
is VisibleMessageViewHolder -> viewHolder.view.recycle()
is ControlMessageViewHolder -> viewHolder.view.recycle()
}
super.onItemViewRecycled(viewHolder)
}
private fun getMessage(cursor: Cursor): MessageRecord? {

View File

@ -4,11 +4,11 @@ import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_message.view.*
import kotlinx.android.synthetic.main.view_visible_message.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.database.model.MessageRecord
class MessageView : LinearLayout {
class ControlMessageView : LinearLayout {
// region Lifecycle
constructor(context: Context) : super(context) {
@ -24,13 +24,13 @@ class MessageView : LinearLayout {
}
private fun setUpViewHierarchy() {
LayoutInflater.from(context).inflate(R.layout.view_message, this)
LayoutInflater.from(context).inflate(R.layout.view_control_message, this)
}
// endregion
// region Updating
fun bind(message: MessageRecord) {
testTextView.text = message.body
testTextView.text = "Control message: ${message.body}"
}
fun recycle() {

View File

@ -0,0 +1,40 @@
package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_visible_message.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.database.model.MessageRecord
class VisibleMessageView : LinearLayout {
// region Lifecycle
constructor(context: Context) : super(context) {
setUpViewHierarchy()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
setUpViewHierarchy()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
setUpViewHierarchy()
}
private fun setUpViewHierarchy() {
LayoutInflater.from(context).inflate(R.layout.view_visible_message, this)
}
// endregion
// region Updating
fun bind(message: MessageRecord) {
testTextView.text = "Visible message: ${message.body}"
}
fun recycle() {
// TODO: Implement
}
// endregion
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<TextView
android:id="@+id/testTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/medium_font_size"
android:textColor="@color/text" />
</LinearLayout>