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 {
// TODO: Is this the best place to put these?
// TODO: Need error handling (exceptions thrown below)
viewModelScope.launch {
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> {
val items = realm.query(Conversation::class).find()
return items.map { it.toConversation() }

View File

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