properly handle inserting and deleting threads from local db
This commit is contained in:
parent
2b3df2719b
commit
35c205605d
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -343,11 +343,15 @@ class ThreadActivity : SimpleActivity() {
|
|||
|
||||
private fun askConfirmDelete() {
|
||||
ConfirmationDialog(this, getString(R.string.delete_whole_conversation_confirmation)) {
|
||||
ensureBackgroundThread {
|
||||
deleteConversation(threadId)
|
||||
runOnUiThread {
|
||||
refreshMessages()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun managePeople() {
|
||||
if (thread_add_contacts.isVisible()) {
|
||||
|
|
|
@ -462,10 +462,12 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
|
|||
return newUri?.lastPathSegment?.toInt() ?: 0
|
||||
}
|
||||
|
||||
fun Context.deleteConversation(id: Int) {
|
||||
fun Context.deleteConversation(threadId: Int) {
|
||||
conversationsDB.deleteThreadId(threadId.toLong())
|
||||
|
||||
var uri = Sms.CONTENT_URI
|
||||
val selection = "${Sms.THREAD_ID} = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
val selectionArgs = arrayOf(threadId.toString())
|
||||
contentResolver.delete(uri, selection, selectionArgs)
|
||||
|
||||
uri = Mms.CONTENT_URI
|
||||
|
|
|
@ -9,11 +9,14 @@ import com.simplemobiletools.smsmessenger.models.Conversation
|
|||
@Dao
|
||||
interface ConversationsDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(conversation: Conversation): Long
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertAll(conversations: List<Conversation>)
|
||||
fun insertOrUpdate(conversation: Conversation): Long
|
||||
|
||||
@Query("SELECT * FROM conversations")
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue