From 779101ddc0224be305fdb287e110b289563f7a4a Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 8 Feb 2024 12:05:34 +1030 Subject: [PATCH] Close MessageDetailsActivity when message is deleted --- .../conversation/v2/MessageDetailsViewModel.kt | 14 ++++++++++++++ .../securesms/repository/ConversationRepository.kt | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt index a73fe41139..b502c52675 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt @@ -5,9 +5,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.launch import network.loki.messenger.R @@ -26,6 +28,7 @@ import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.mms.ImageSlide import org.thoughtcrime.securesms.mms.Slide +import org.thoughtcrime.securesms.repository.ConversationRepository import org.thoughtcrime.securesms.ui.GetString import org.thoughtcrime.securesms.ui.TitledText import java.util.Date @@ -38,8 +41,11 @@ class MessageDetailsViewModel @Inject constructor( private val lokiMessageDatabase: LokiMessageDatabase, private val mmsSmsDatabase: MmsSmsDatabase, private val threadDb: ThreadDatabase, + private val repository: ConversationRepository, ) : ViewModel() { + private var job: Job? = null + private val state = MutableStateFlow(MessageDetailsState()) val stateFlow = state.asStateFlow() @@ -48,6 +54,8 @@ class MessageDetailsViewModel @Inject constructor( var timestamp: Long = 0L set(value) { + job?.cancel() + field = value val record = mmsSmsDatabase.getMessageForTimestamp(timestamp) @@ -58,6 +66,12 @@ class MessageDetailsViewModel @Inject constructor( val mmsRecord = record as? MmsMessageRecord + job = viewModelScope.launch { + repository.changes(record.threadId) + .filter { mmsSmsDatabase.getMessageForTimestamp(timestamp) == null } + .collect { event.send(Event.Finish) } + } + state.value = record.run { val slides = mmsRecord?.slideDeck?.slides ?: emptyList() diff --git a/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt index e994ea04f0..6f375f4b88 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/repository/ConversationRepository.kt @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.repository import network.loki.messenger.libsession_util.util.ExpiryMode import android.content.ContentResolver import android.content.Context +import app.cash.copper.Query import app.cash.copper.flow.observeQuery import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.Flow @@ -45,6 +46,7 @@ import kotlin.coroutines.suspendCoroutine interface ConversationRepository { fun maybeGetRecipientForThreadId(threadId: Long): Recipient? fun maybeGetBlindedRecipient(recipient: Recipient): Recipient? + fun changes(threadId: Long): Flow fun recipientUpdateFlow(threadId: Long): Flow fun saveDraft(threadId: Long, text: String) fun getDraft(threadId: Long): String? @@ -117,6 +119,9 @@ class DefaultConversationRepository @Inject constructor( ) } + override fun changes(threadId: Long): Flow = + contentResolver.observeQuery(DatabaseContentProviders.Conversation.getUriForThread(threadId)) + override fun recipientUpdateFlow(threadId: Long): Flow { return contentResolver.observeQuery(DatabaseContentProviders.Conversation.getUriForThread(threadId)).map { maybeGetRecipientForThreadId(threadId)