Stop playing message if deleted

This commit is contained in:
fanchao 2024-05-17 14:02:34 +10:00
parent fbc82d7831
commit 1377e192a1
4 changed files with 30 additions and 7 deletions

View File

@ -80,6 +80,11 @@ public class AudioSlidePlayer implements SensorEventListener {
} }
} }
@Nullable
public synchronized static AudioSlidePlayer getInstance() {
return playing.orNull();
}
private AudioSlidePlayer(@NonNull Context context, private AudioSlidePlayer(@NonNull Context context,
@NonNull AudioSlide slide, @NonNull AudioSlide slide,
@NonNull Listener listener) @NonNull Listener listener)

View File

@ -103,6 +103,7 @@ import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.attachments.ScreenshotObserver import org.thoughtcrime.securesms.attachments.ScreenshotObserver
import org.thoughtcrime.securesms.audio.AudioRecorder import org.thoughtcrime.securesms.audio.AudioRecorder
import org.thoughtcrime.securesms.audio.AudioSlidePlayer
import org.thoughtcrime.securesms.components.emoji.RecentEmojiPageModel import org.thoughtcrime.securesms.components.emoji.RecentEmojiPageModel
import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey
import org.thoughtcrime.securesms.conversation.ConversationActionBarDelegate import org.thoughtcrime.securesms.conversation.ConversationActionBarDelegate

View File

@ -27,10 +27,11 @@ import org.session.libsession.utilities.Address
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.IdPrefix import org.session.libsignal.utilities.IdPrefix
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.database.MmsSmsDatabase import org.thoughtcrime.securesms.audio.AudioSlidePlayer
import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
import org.thoughtcrime.securesms.repository.ConversationRepository import org.thoughtcrime.securesms.repository.ConversationRepository
import java.util.UUID import java.util.UUID
@ -95,14 +96,15 @@ class ConversationViewModel(
init { init {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
repository.recipientUpdateFlow(threadId) repository.recipientUpdateFlow(threadId)
.collect { recipient ->
if (recipient == null && _uiState.value.conversationExists) {
_uiState.update { it.copy(conversationExists = false) }
}
}
} }
} }
override fun onCleared() {
super.onCleared()
AudioSlidePlayer.stopAll()
}
fun saveDraft(text: String) { fun saveDraft(text: String) {
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
repository.saveDraft(threadId, text) repository.saveDraft(threadId, text)
@ -142,10 +144,24 @@ class ConversationViewModel(
} }
fun deleteLocally(message: MessageRecord) { fun deleteLocally(message: MessageRecord) {
stopPlayingAudioMessage(message)
val recipient = recipient ?: return Log.w("Loki", "Recipient was null for delete locally action") val recipient = recipient ?: return Log.w("Loki", "Recipient was null for delete locally action")
repository.deleteLocally(recipient, message) repository.deleteLocally(recipient, message)
} }
/**
* Stops audio player if its current playing is the one given in the message.
*/
private fun stopPlayingAudioMessage(message: MessageRecord) {
val player = AudioSlidePlayer.getInstance()
val audioSlide = player?.audioSlide
if (audioSlide != null &&
message is MmsMessageRecord &&
message.slideDeck.audioSlide == audioSlide) {
player.stop()
}
}
fun setRecipientApproved() { fun setRecipientApproved() {
val recipient = recipient ?: return Log.w("Loki", "Recipient was null for set approved action") val recipient = recipient ?: return Log.w("Loki", "Recipient was null for set approved action")
repository.setApproved(recipient, true) repository.setApproved(recipient, true)
@ -157,6 +173,7 @@ class ConversationViewModel(
repository.deleteForEveryone(threadId, recipient, message) repository.deleteForEveryone(threadId, recipient, message)
.onSuccess { .onSuccess {
Log.d("Loki", "Deleted message ${message.id} ") Log.d("Loki", "Deleted message ${message.id} ")
stopPlayingAudioMessage(message)
} }
.onFailure { .onFailure {
Log.w("Loki", "FAILED TO delete message ${message.id} ") Log.w("Loki", "FAILED TO delete message ${message.id} ")

View File

@ -68,7 +68,7 @@ class VoiceMessageView : RelativeLayout, AudioSlidePlayer.Listener {
return return
} }
val player = AudioSlidePlayer.createFor(context, audio, this) val player = AudioSlidePlayer.createFor(context.applicationContext, audio, this)
this.player = player this.player = player
(audio.asAttachment() as? DatabaseAttachment)?.let { attachment -> (audio.asAttachment() as? DatabaseAttachment)?.let { attachment ->