diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt index e1e78718..fe9b7dd3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt @@ -13,7 +13,9 @@ import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.dialogs.RenameGroupDialog import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.extensions.dbHelper import com.simplemobiletools.contacts.helpers.ContactsHelper +import com.simplemobiletools.contacts.helpers.FIRST_GROUP_ID import com.simplemobiletools.contacts.helpers.GROUPS_TAB_MASK import com.simplemobiletools.contacts.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.models.Group @@ -97,7 +99,11 @@ class GroupsAdapter(activity: SimpleActivity, var groups: ArrayList, val selectedPositions.sortedDescending().forEach { val group = groups[it] groupsToRemove.add(group) - ContactsHelper(activity).deleteGroup(group.id) + if (group.id >= FIRST_GROUP_ID) { + activity.dbHelper.deleteGroup(group.id) + } else { + ContactsHelper(activity).deleteGroup(group.id) + } } groups.removeAll(groupsToRemove) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt index 0d460631..b1a341c7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt @@ -14,6 +14,7 @@ const val CONTACT_ID = "contact_id" const val SMT_PRIVATE = "smt_private" // used at the contact source of local contacts hidden from other apps const val IS_PRIVATE = "is_private" const val GROUP = "group" +const val FIRST_GROUP_ID = 10000 const val LOCATION_CONTACTS_TAB = 1 const val LOCATION_FAVORITES_TAB = 2 diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index ff3ab534..bf955a01 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -390,6 +390,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { cursor?.close() } + groups.addAll(activity.dbHelper.getGroups()) return groups } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index 5b9a3c8a..0228f5b7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -13,6 +13,7 @@ import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.getBlobValue import com.simplemobiletools.commons.extensions.getIntValue +import com.simplemobiletools.commons.extensions.getLongValue import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.contacts.extensions.getByteArray import com.simplemobiletools.contacts.extensions.getPhotoThumbnailSize @@ -37,7 +38,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont private val COL_TITLE = "title" private val FIRST_CONTACT_ID = 1000000 - private val FIRST_GROUP_ID = 10000 private val mDb = writableDatabase @@ -162,12 +162,29 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return mDb.update(GROUPS_TABLE_NAME, contactValues, selection, selectionArgs) == 1 } + fun deleteGroup(id: Long) = deleteGroups(arrayOf(id.toString())) + fun deleteGroups(ids: Array) { val args = TextUtils.join(", ", ids) val selection = "$GROUPS_TABLE_NAME.$COL_ID IN ($args)" mDb.delete(GROUPS_TABLE_NAME, selection, null) } + fun getGroups(): ArrayList { + val groups = ArrayList() + val projection = arrayOf(COL_ID, COL_TITLE) + val cursor = mDb.query(GROUPS_TABLE_NAME, projection, null, null, null, null, null) + cursor.use { + while (cursor.moveToNext()) { + val id = cursor.getLongValue(COL_ID) + val title = cursor.getStringValue(COL_TITLE) + val group = Group(id, title) + groups.add(group) + } + } + return groups + } + private fun fillGroupValues(group: Group): ContentValues { return ContentValues().apply { put(COL_TITLE, group.title)