Implement deletion of non-existent conversations
This commit is contained in:
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() }
|
||||||
|
|||||||
@@ -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")
|
||||||
|
|||||||
Reference in New Issue
Block a user