extract a method to getItemFromAdapter

method was extracted for other purposes but was not used in the end. Anyway it made sense to use it for resumeAudioPlaybackIfNeeded.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2024-08-09 10:39:24 +02:00
parent 217ff6af74
commit d18bd20bf1
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B

View file

@ -2606,46 +2606,29 @@ class ChatActivity :
} }
/** /**
* this method must be called after that the adatper has finished loading ChatMessages items * this method must be called after that the adapter has finished loading ChatMessages items
* it searches by ID the message that was playing, * it searches by ID the message that was playing,s
* then, if it finds it, it restores audio position * then, if it finds it, it restores audio position
* and eventually resumes audio playback * and eventually resumes audio playback
* @author Giacomo Pacini * @author Giacomo Pacini
*/ */
private fun resumeAudioPlaybackIfNeeded() { private fun resumeAudioPlaybackIfNeeded() {
if (!voiceMessageToRestoreId.equals("")) { if (voiceMessageToRestoreId != "") {
Log.d(RESUME_AUDIO_TAG, "begin method to resume audio playback") Log.d(RESUME_AUDIO_TAG, "begin method to resume audio playback")
// TODO: replace this logic by calling getItemFromAdapter(messageId) val pair = getItemFromAdapter(voiceMessageToRestoreId)
if (adapter != null) { currentlyPlayedVoiceMessage = pair?.first
Log.d(RESUME_AUDIO_TAG, "adapter is not null, proceeding") val voiceMessagePosition = pair?.second!!
val voiceMessagePosition = adapter!!.items!!.indexOfFirst {
it.item is ChatMessage && (it.item as ChatMessage).id == voiceMessageToRestoreId lastRecordMediaPosition = voiceMessageToRestoreAudioPosition * 1000
} Log.d(RESUME_AUDIO_TAG, "trying to resume audio")
if (voiceMessagePosition >= 0) { binding.messagesListView.scrollToPosition(voiceMessagePosition)
val currentItem = adapter?.items?.get(voiceMessagePosition)?.item // WORKAROUND TO FETCH FILE INFO:
if (currentItem is ChatMessage && currentItem.id == voiceMessageToRestoreId) { currentlyPlayedVoiceMessage!!.getImageUrl()
currentlyPlayedVoiceMessage = currentItem // see getImageUrl() source code
lastRecordMediaPosition = voiceMessageToRestoreAudioPosition * 1000 setUpWaveform(currentlyPlayedVoiceMessage!!, voiceMessageToRestoreWasPlaying)
Log.d(RESUME_AUDIO_TAG, "trying to resume audio") Log.d(RESUME_AUDIO_TAG, "resume audio procedure completed")
binding.messagesListView.scrollToPosition(voiceMessagePosition)
// WORKAROUND TO FETCH FILE INFO:
currentlyPlayedVoiceMessage!!.getImageUrl()
// see getImageUrl() source code
setUpWaveform(currentlyPlayedVoiceMessage!!, voiceMessageToRestoreWasPlaying)
Log.d(RESUME_AUDIO_TAG, "resume audio procedure completed")
} else {
Log.d(RESUME_AUDIO_TAG, "currentItem retrieved was not chatmessage or its id was not correct")
}
} else {
Log.d(
RESUME_AUDIO_TAG,
"voiceMessagePosition is -1, adapter # of items: " + adapter!!.itemCount
)
}
} else {
Log.d(RESUME_AUDIO_TAG, "TalkMessagesListAdapter is null")
}
} else { } else {
Log.d(RESUME_AUDIO_TAG, "No voice message to restore") Log.d(RESUME_AUDIO_TAG, "No voice message to restore")
} }
@ -2654,7 +2637,7 @@ class ChatActivity :
voiceMessageToRestoreWasPlaying = false voiceMessageToRestoreWasPlaying = false
} }
private fun getItemFromAdapter(messageId: String): ChatMessage? { private fun getItemFromAdapter(messageId: String): Pair<ChatMessage,Int>? {
if (adapter != null) { if (adapter != null) {
val messagePosition = adapter!!.items!!.indexOfFirst { val messagePosition = adapter!!.items!!.indexOfFirst {
it.item is ChatMessage && (it.item as ChatMessage).id == messageId it.item is ChatMessage && (it.item as ChatMessage).id == messageId
@ -2662,7 +2645,7 @@ class ChatActivity :
if (messagePosition >= 0) { if (messagePosition >= 0) {
val currentItem = adapter?.items?.get(messagePosition)?.item val currentItem = adapter?.items?.get(messagePosition)?.item
if (currentItem is ChatMessage && currentItem.id == messageId) { if (currentItem is ChatMessage && currentItem.id == messageId) {
return currentItem return Pair(currentItem, messagePosition)
} else { } else {
Log.d(TAG, "currentItem retrieved was not chatmessage or its id was not correct") Log.d(TAG, "currentItem retrieved was not chatmessage or its id was not correct")
} }