From a554aa574ec7ac4ee4135d8268124f7dd578add9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 14 Jul 2021 09:37:18 +1000 Subject: [PATCH] Fix adapter position vs layout position usage --- .../conversation/v2/ConversationActivityV2.kt | 18 +++++++----------- .../conversation/v2/ConversationAdapter.kt | 3 +-- .../v2/messages/VisibleMessageContentView.kt | 5 ++--- .../v2/messages/VisibleMessageView.kt | 4 ++-- .../v2/messages/VoiceMessageView.kt | 6 +++--- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 0f63bc9641..f156f6d45b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -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() } } } 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 a4a5f1ba28..15de18a608 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 @@ -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) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt index 1d90f8475a..ec55af5427 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt @@ -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) 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 index 0fcbf685b4..b1a0fa38e0 100644 --- 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 @@ -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() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt index 6498a1a472..31545ceace 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt @@ -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) }