Private
Public Access
1
0

Implement deletion of non-existent conversations

This commit is contained in:
2023-08-17 01:53:37 -07:00
parent 84e3a27c62
commit 19a6960ab2
3 changed files with 14 additions and 4 deletions

View File

@@ -26,7 +26,7 @@ class ConversationListViewModel @Inject constructor(
init { init {
// TODO: Is this the best place to put these? // TODO: Is this the best place to put these?
// TODO: Need error handling (exceptions thrown below) // TODO: Need error handling (exceptions thrown below)
viewModelScope.launch { viewModelScope.launch {
repository.synchronize() repository.synchronize()
} }

View File

@@ -85,6 +85,15 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
} }
} }
fun deleteConversations(conversations: List<ModelConversation>) = realm.writeBlocking {
conversations.forEach { inConversation ->
val conversation = getConversationByGuid(inConversation.guid)
findLatest(conversation)?.let {
delete(it)
}
}
}
fun fetchConversations(): List<ModelConversation> { fun fetchConversations(): List<ModelConversation> {
val items = realm.query(Conversation::class).find() val items = realm.query(Conversation::class).find()
return items.map { it.toConversation() } return items.map { it.toConversation() }

View File

@@ -121,10 +121,11 @@ class ChatRepository(
Log.d(REPO_LOG, "Synchronizing conversations") Log.d(REPO_LOG, "Synchronizing conversations")
// Sync conversations // Sync conversations
val conversations = fetchConversations() val serverConversations = fetchConversations()
database.writeConversations(conversations) val deletedConversations = conversations.minus(serverConversations)
// TODO: Delete non-existent conversations. database.deleteConversations(deletedConversations)
database.writeConversations(serverConversations)
// Sync top N number of conversations' message content // Sync top N number of conversations' message content
Log.d(REPO_LOG, "Synchronizing messages") Log.d(REPO_LOG, "Synchronizing messages")