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() { private fun getCachedConversations() {
ensureBackgroundThread { ensureBackgroundThread {
val conversations = conversationsDB.getAll().toMutableList() as ArrayList<Conversation> val conversations = conversationsDB.getAll().sortedByDescending { it.date }.toMutableList() as ArrayList<Conversation>
runOnUiThread { runOnUiThread {
setupConversations(conversations) setupConversations(conversations)
getNewConversations() getNewConversations(conversations)
} }
} }
} }
private fun getNewConversations() { private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
val conversations = getConversations() val conversations = getConversations()
@ -186,7 +186,25 @@ class MainActivity : SimpleActivity() {
setupConversations(conversations) 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)
}
}
} }
} }

View File

@ -343,11 +343,15 @@ class ThreadActivity : SimpleActivity() {
private fun askConfirmDelete() { private fun askConfirmDelete() {
ConfirmationDialog(this, getString(R.string.delete_whole_conversation_confirmation)) { ConfirmationDialog(this, getString(R.string.delete_whole_conversation_confirmation)) {
ensureBackgroundThread {
deleteConversation(threadId) deleteConversation(threadId)
runOnUiThread {
refreshMessages() refreshMessages()
finish() finish()
} }
} }
}
}
private fun managePeople() { private fun managePeople() {
if (thread_add_contacts.isVisible()) { if (thread_add_contacts.isVisible()) {

View File

@ -462,10 +462,12 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
return newUri?.lastPathSegment?.toInt() ?: 0 return newUri?.lastPathSegment?.toInt() ?: 0
} }
fun Context.deleteConversation(id: Int) { fun Context.deleteConversation(threadId: Int) {
conversationsDB.deleteThreadId(threadId.toLong())
var uri = Sms.CONTENT_URI var uri = Sms.CONTENT_URI
val selection = "${Sms.THREAD_ID} = ?" val selection = "${Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(threadId.toString())
contentResolver.delete(uri, selection, selectionArgs) contentResolver.delete(uri, selection, selectionArgs)
uri = Mms.CONTENT_URI uri = Mms.CONTENT_URI

View File

@ -9,11 +9,14 @@ import com.simplemobiletools.smsmessenger.models.Conversation
@Dao @Dao
interface ConversationsDao { interface ConversationsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(conversation: Conversation): Long fun insertOrUpdate(conversation: Conversation): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(conversations: List<Conversation>)
@Query("SELECT * FROM conversations") @Query("SELECT * FROM conversations")
fun getAll(): List<Conversation> fun getAll(): List<Conversation>
@Query("DELETE FROM conversations WHERE id = :id")
fun delete(id: Long)
@Query("DELETE FROM conversations WHERE system_id = :threadId")
fun deleteThreadId(threadId: Long)
} }