diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt index ef33459..87d3990 100644 --- a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt @@ -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() } diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt index 7a04e9a..058ce35 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt @@ -85,6 +85,15 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) { } } + fun deleteConversations(conversations: List) = realm.writeBlocking { + conversations.forEach { inConversation -> + val conversation = getConversationByGuid(inConversation.guid) + findLatest(conversation)?.let { + delete(it) + } + } + } + fun fetchConversations(): List { val items = realm.query(Conversation::class).find() return items.map { it.toConversation() } diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt index 57b0e0c..dd1c669 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/server/ChatRepository.kt @@ -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")