handle showing and deleting local private groups

This commit is contained in:
tibbi 2018-03-21 17:51:13 +01:00
parent b28e85727d
commit 30a7c1770d
4 changed files with 27 additions and 2 deletions

View File

@ -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<Group>, 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)

View File

@ -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

View File

@ -390,6 +390,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
cursor?.close()
}
groups.addAll(activity.dbHelper.getGroups())
return groups
}

View File

@ -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<String>) {
val args = TextUtils.join(", ", ids)
val selection = "$GROUPS_TABLE_NAME.$COL_ID IN ($args)"
mDb.delete(GROUPS_TABLE_NAME, selection, null)
}
fun getGroups(): ArrayList<Group> {
val groups = ArrayList<Group>()
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)