feat: add in activity finish if recipient no longer exists (deleted thread) from sync

This commit is contained in:
0x330a 2023-03-24 11:18:14 +11:00
parent 61e8935725
commit aadbcf36d0
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
3 changed files with 28 additions and 12 deletions

View File

@ -241,7 +241,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
}
} ?: finish()
}
viewModelFactory.create(threadId, MessagingModuleConfiguration.shared.getUserED25519KeyPair())
viewModelFactory.create(threadId, MessagingModuleConfiguration.shared.getUserED25519KeyPair(), contentResolver)
}
private var actionMode: ActionMode? = null
private var unreadCount = 0
@ -665,6 +665,10 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
if (uiState.isMessageRequestAccepted == true) {
binding?.messageRequestBar?.visibility = View.GONE
}
if (!uiState.conversationExists && !isFinishing) {
// Conversation should be deleted now, just go back
finish()
}
}
}
}

View File

@ -1,8 +1,10 @@
package org.thoughtcrime.securesms.conversation.v2
import android.content.ContentResolver
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import app.cash.copper.flow.observeQuery
import com.goterl.lazysodium.utils.KeyPair
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
@ -19,6 +21,7 @@ import org.session.libsession.messaging.utilities.SodiumUtilities
import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.IdPrefix
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.database.DatabaseContentProviders
import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.repository.ConversationRepository
@ -27,11 +30,12 @@ import java.util.UUID
class ConversationViewModel(
val threadId: Long,
val edKeyPair: KeyPair?,
private val contentResolver: ContentResolver,
private val repository: ConversationRepository,
private val storage: Storage
) : ViewModel() {
private val _uiState = MutableStateFlow(ConversationUiState())
private val _uiState = MutableStateFlow(ConversationUiState(conversationExists = recipient != null))
val uiState: StateFlow<ConversationUiState> = _uiState
val recipient: Recipient?
@ -49,6 +53,18 @@ class ConversationViewModel(
?.let { SessionId(IdPrefix.BLINDED, it) }?.hexString
}
init {
viewModelScope.launch(Dispatchers.IO) {
contentResolver.observeQuery(DatabaseContentProviders.Conversation.getUriForThread(threadId))
.collect {
val recipientExists = storage.getRecipientForThread(threadId) != null
if (!recipientExists && _uiState.value.conversationExists) {
_uiState.update { it.copy(conversationExists = false) }
}
}
}
}
fun saveDraft(text: String) {
GlobalScope.launch(Dispatchers.IO) {
repository.saveDraft(threadId, text)
@ -172,19 +188,20 @@ class ConversationViewModel(
@dagger.assisted.AssistedFactory
interface AssistedFactory {
fun create(threadId: Long, edKeyPair: KeyPair?): Factory
fun create(threadId: Long, edKeyPair: KeyPair?, contentResolver: ContentResolver): Factory
}
@Suppress("UNCHECKED_CAST")
class Factory @AssistedInject constructor(
@Assisted private val threadId: Long,
@Assisted private val edKeyPair: KeyPair?,
@Assisted private val contentResolver: ContentResolver,
private val repository: ConversationRepository,
private val storage: Storage
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ConversationViewModel(threadId, edKeyPair, repository, storage) as T
return ConversationViewModel(threadId, edKeyPair, contentResolver, repository, storage) as T
}
}
}
@ -193,5 +210,6 @@ data class UiMessage(val id: Long, val message: String)
data class ConversationUiState(
val uiMessages: List<UiMessage> = emptyList(),
val isMessageRequestAccepted: Boolean? = null
val isMessageRequestAccepted: Boolean? = null,
val conversationExists: Boolean
)

View File

@ -138,7 +138,7 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
namespace,
updateLatestHash = true,
updateStoredHashes = true,
) // TODO: might not be needed anymore .filter { (_, hash) -> !forConfigObject.currentHashes().contains(hash) }
)
if (messages.isEmpty()) {
// no new messages to process
@ -186,12 +186,6 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
requestSparseArray[request.namespace!!] = request
}
if (requestSparseArray.size() == 1) {
// only one (the personal messages)
Log.d("Loki-DBG", "Not building requests for the configs, current config state:")
Log.d("Loki-DBG", "${listOf(configFactory.user, configFactory.contacts, configFactory.convoVolatile)}")
}
val requests = requestSparseArray.valueIterator().asSequence().toList()
SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses ->