properly handle inserting and deleting threads from local db

This commit is contained in:
tibbi 2020-05-30 20:01:09 +02:00
parent 2b3df2719b
commit 35c205605d
4 changed files with 40 additions and 13 deletions

View file

@ -158,15 +158,15 @@ class MainActivity : SimpleActivity() {
private fun getCachedConversations() {
ensureBackgroundThread {
val conversations = conversationsDB.getAll().toMutableList() as ArrayList<Conversation>
val conversations = conversationsDB.getAll().sortedByDescending { it.date }.toMutableList() as ArrayList<Conversation>
runOnUiThread {
setupConversations(conversations)
getNewConversations()
getNewConversations(conversations)
}
}
}
private fun getNewConversations() {
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground()
ensureBackgroundThread {
val conversations = getConversations()
@ -186,7 +186,25 @@ class MainActivity : SimpleActivity() {
setupConversations(conversations)
}
conversationsDB.insertAll(conversations)
conversations.forEach { clonedConversation ->
if (!cachedConversations.map { it.system_id }.contains(clonedConversation.system_id)) {
conversationsDB.insertOrUpdate(clonedConversation)
cachedConversations.add(clonedConversation)
}
}
cachedConversations.forEach { cachedConversation ->
if (!conversations.map { it.system_id }.contains(cachedConversation.system_id)) {
conversationsDB.delete(cachedConversation.id!!)
}
}
cachedConversations.forEach { cachedConversation ->
val conv = conversations.firstOrNull { it.system_id == cachedConversation.system_id && it.getStringToCompare() != cachedConversation.getStringToCompare() }
if (conv != null) {
conversationsDB.insertOrUpdate(conv)
}
}
}
}