handle deleting local private contacts

This commit is contained in:
tibbi 2018-02-11 21:17:24 +01:00
parent 062ee8a6d4
commit 21cc46de54
2 changed files with 20 additions and 2 deletions

View File

@ -639,14 +639,23 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
} }
} }
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact)) fun deleteContact(contact: Contact) {
if (contact.source == SMT_PRIVATE) {
activity.dbHelper.deleteContact(contact.id)
} else {
deleteContacts(arrayListOf(contact))
}
}
fun deleteContacts(contacts: ArrayList<Contact>) { fun deleteContacts(contacts: ArrayList<Contact>) {
val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray()
activity.dbHelper.deleteContacts(localContacts)
try { try {
val contactIDs = HashSet<String>() val contactIDs = HashSet<String>()
val operations = ArrayList<ContentProviderOperation>() val operations = ArrayList<ContentProviderOperation>()
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?"
contacts.forEach { contacts.filter { it.source != SMT_PRIVATE }.forEach {
ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply {
val selectionArgs = arrayOf(it.id.toString()) val selectionArgs = arrayOf(it.id.toString())
withSelection(selection, selectionArgs) withSelection(selection, selectionArgs)

View File

@ -4,6 +4,7 @@ import android.content.ContentValues
import android.content.Context import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
@ -68,6 +69,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, selectionArgs) == 1 return mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, selectionArgs) == 1
} }
fun deleteContact(id: Int) = deleteContacts(arrayOf(id.toString()))
fun deleteContacts(ids: Array<String>) {
val args = TextUtils.join(", ", ids)
val selection = "$CONTACTS_TABLE_NAME.$COL_ID IN ($args)"
mDb.delete(CONTACTS_TABLE_NAME, selection, null)
}
private fun fillContactValues(contact: Contact): ContentValues { private fun fillContactValues(contact: Contact): ContentValues {
return ContentValues().apply { return ContentValues().apply {
put(COL_FIRST_NAME, contact.firstName) put(COL_FIRST_NAME, contact.firstName)