Fix adapter position vs layout position usage

This commit is contained in:
Niels Andriesse 2021-07-14 09:37:18 +10:00
parent b6134b42c4
commit a554aa574e
5 changed files with 15 additions and 21 deletions

View File

@ -860,17 +860,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
conversationRecyclerView.scrollToPosition(lastSeenItemPosition)
}
override fun playNextAudioIfPossible(current: Int) {
if (current > 0) {
val nextVisibleMessageView = conversationRecyclerView[current - 1] as? VisibleMessageView
nextVisibleMessageView?.let { visibleMessageView ->
visibleMessageView.messageContentView.mainContainer.children.forEach { child ->
val nextVoiceMessageView = child as? VoiceMessageView
nextVoiceMessageView?.let { voiceMessageView ->
voiceMessageView.togglePlayback()
return@forEach
}
}
override fun playVoiceMessageAtIndexIfPossible(indexInAdapter: Int) {
if (indexInAdapter < 0 || indexInAdapter >= adapter.itemCount) { return }
val viewHolder = conversationRecyclerView.findViewHolderForAdapterPosition(indexInAdapter) as? ConversationAdapter.VisibleMessageViewHolder
val nextVisibleMessageView = viewHolder?.view ?: return
nextVisibleMessageView.messageContentView.mainContainer.children.forEach { view ->
if (view is VoiceMessageView) {
return@forEach view.togglePlayback()
}
}
}

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.conversation.v2
import android.content.Context
import android.database.Cursor
import android.graphics.Rect
import android.view.MotionEvent
import android.view.ViewGroup
import androidx.core.view.isVisible
@ -72,7 +71,7 @@ class ConversationAdapter(context: Context, cursor: Cursor, private val onItemPr
view.snIsSelected = isSelected
view.messageTimestampTextView.isVisible = isSelected
val position = viewHolder.adapterPosition
view.viewHolderIndex = position
view.indexInAdapter = position
view.bind(message, getMessageBefore(position, cursor), getMessageAfter(position, cursor), glide, searchQuery)
view.onPress = { event -> onItemPress(message, viewHolder.adapterPosition, view, event) }
view.onSwipeToReply = { onItemSwipeToReply(message, viewHolder.adapterPosition) }

View File

@ -22,7 +22,6 @@ import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat
import androidx.core.text.getSpans
import androidx.core.text.toSpannable
import kotlinx.android.synthetic.main.view_link_preview.view.*
import kotlinx.android.synthetic.main.view_visible_message_content.view.*
import network.loki.messenger.R
import org.session.libsession.utilities.ThemeUtil
@ -49,7 +48,7 @@ class VisibleMessageContentView : LinearLayout {
var onContentClick: ((event: MotionEvent) -> Unit)? = null
var onContentDoubleTap: (() -> Unit)? = null
var delegate: VisibleMessageContentViewDelegate? = null
var viewHolderIndex: Int = -1
var indexInAdapter: Int = -1
// region Lifecycle
constructor(context: Context) : super(context) { initialize() }
@ -111,7 +110,7 @@ class VisibleMessageContentView : LinearLayout {
// Audio attachment
if (contactIsTrusted || message.isOutgoing) {
val voiceMessageView = VoiceMessageView(context)
voiceMessageView.index = viewHolderIndex
voiceMessageView.indexInAdapter = indexInAdapter
voiceMessageView.delegate = context as? ConversationActivityV2
voiceMessageView.bind(message, isStartOfMessageCluster, isEndOfMessageCluster)
mainContainer.addView(voiceMessageView)

View File

@ -43,7 +43,7 @@ class VisibleMessageView : LinearLayout {
private var longPressCallback: Runnable? = null
private var onDownTimestamp = 0L
private var onDoubleTap: (() -> Unit)? = null
var viewHolderIndex: Int = -1
var indexInAdapter: Int = -1
var snIsSelected = false
set(value) { field = value; handleIsSelectedChanged()}
var onPress: ((event: MotionEvent) -> Unit)? = null
@ -149,7 +149,7 @@ class VisibleMessageView : LinearLayout {
var maxWidth = screenWidth - startPadding - endPadding
if (profilePictureContainer.visibility != View.GONE) { maxWidth -= profilePictureContainer.width }
// Populate content view
messageContentView.viewHolderIndex = viewHolderIndex
messageContentView.indexInAdapter = indexInAdapter
messageContentView.bind(message, isStartOfMessageCluster, isEndOfMessageCluster, glide, maxWidth, thread, searchQuery, isGroupThread || (contact?.isTrusted ?: false))
messageContentView.delegate = contentViewDelegate
onDoubleTap = { messageContentView.onContentDoubleTap?.invoke() }

View File

@ -32,7 +32,7 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
private var duration = 0L
private var player: AudioSlidePlayer? = null
var delegate: VoiceMessageViewDelegate? = null
var index = -1
var indexInAdapter = -1
// region Lifecycle
constructor(context: Context) : super(context) { initialize() }
@ -85,7 +85,7 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
if (progress == 1.0) {
togglePlayback()
handleProgressChanged(0.0)
delegate?.playNextAudioIfPossible(index)
delegate?.playVoiceMessageAtIndexIfPossible(indexInAdapter - 1)
} else {
handleProgressChanged(progress)
}
@ -138,5 +138,5 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
interface VoiceMessageViewDelegate {
fun playNextAudioIfPossible(current: Int)
fun playVoiceMessageAtIndexIfPossible(indexInAdapter: Int)
}