diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt index 8a08ac2678..f68ea1da4a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt @@ -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(context, cursor) { +class ConversationAdapter(context: Context, cursor: Cursor) : CursorRecyclerViewAdapter(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 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? { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt similarity index 77% rename from app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageView.kt rename to app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt index 1c7b9941da..f7e23bc19b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt @@ -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() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt new file mode 100644 index 0000000000..684bb03f13 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/res/layout/view_message.xml b/app/src/main/res/layout/view_control_message.xml similarity index 100% rename from app/src/main/res/layout/view_message.xml rename to app/src/main/res/layout/view_control_message.xml diff --git a/app/src/main/res/layout/view_visible_message.xml b/app/src/main/res/layout/view_visible_message.xml new file mode 100644 index 0000000000..f9c3545d87 --- /dev/null +++ b/app/src/main/res/layout/view_visible_message.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file