Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Signal/ConversationView/CVAudioPlayback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public import SignalUI

protocol CVAudioPlayerListener {
func audioPlayerStateDidChange(attachmentId: Attachment.IDType)
func audioPlayerDidFinish(attachmentId: Attachment.IDType)
// Attachments are deduplicated by content across conversations, so
// attachmentId alone is not enough to identify which message finished.
func audioPlayerDidFinish(attachmentId: Attachment.IDType, forInteractionId interactionId: String?)
func audioPlayerDidMarkViewed(attachmentId: Attachment.IDType)
}

Expand Down Expand Up @@ -294,7 +296,10 @@ extension CVAudioPlayer: CVAudioPlaybackDelegate {
progressCache[audioPlayback.attachmentId] = 0

for listener in listeners.elements {
listener.audioPlayerDidFinish(attachmentId: audioPlayback.attachmentId)
listener.audioPlayerDidFinish(
attachmentId: audioPlayback.attachmentId,
forInteractionId: audioPlayback.owningInteractionId,
)
}
}
}
Expand All @@ -316,6 +321,7 @@ private class CVAudioPlayback: NSObject, AudioPlayerDelegate {
fileprivate weak var delegate: CVAudioPlaybackDelegate?

fileprivate let uniqueThreadId: String?
fileprivate let owningInteractionId: String?
fileprivate let attachmentId: Attachment.IDType

private let audioPlayer: AudioPlayer
Expand Down Expand Up @@ -391,6 +397,7 @@ private class CVAudioPlayback: NSObject, AudioPlayerDelegate {

audioPlayer = AudioPlayer(attachment: attachmentStream.attachmentStream, audioBehavior: .audioMessagePlayback)
uniqueThreadId = attachment.owningMessage?.uniqueThreadId
owningInteractionId = attachment.owningMessage?.uniqueId

super.init()

Expand Down
2 changes: 1 addition & 1 deletion Signal/ConversationView/CellViews/AudioMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ class AudioMessageView: ManualStackView, CVAudioPlayerListener {
updateContents(animated: true)
}

func audioPlayerDidFinish(attachmentId: Attachment.IDType) {
func audioPlayerDidFinish(attachmentId: Attachment.IDType, forInteractionId interactionId: String?) {
AssertIsOnMainThread()

guard attachmentId == attachment.id else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,12 @@ public class CVComponentAudioAttachment: CVComponentBase, CVComponent {
extension CVComponentAudioAttachment: CVAudioPlayerListener {
func audioPlayerStateDidChange(attachmentId: Attachment.IDType) {}

func audioPlayerDidFinish(attachmentId: Attachment.IDType) {
func audioPlayerDidFinish(attachmentId: Attachment.IDType, forInteractionId interactionId: String?) {
guard attachmentId == audioAttachment.attachment.id else { return }
// Prevent a stale listener in a different conversation (sharing the
// same deduplicated attachment) from incorrectly triggering autoplay
// of its own, unrelated "next" message.
guard interactionId == self.interaction.uniqueId else { return }
AppEnvironment.shared.cvAudioPlayerRef.autoplayNextAudioAttachmentIfNeeded(nextAudioAttachment)
}

Expand Down