From abbeb218b82d55b23641024afdb1faac2f4f85b0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 21 Mar 2018 19:51:06 +0100 Subject: [PATCH] handle adding and removing secret contacts to/from groups --- .../contacts/helpers/DBHelper.kt | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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 b2cddb09..0067b33e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -194,13 +194,35 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } fun addContactsToGroup(contacts: ArrayList, groupId: Long) { - + contacts.forEach { + val currentGroupIds = it.groups.map { it.id } as ArrayList + currentGroupIds.add(groupId) + updateContactGroups(it, currentGroupIds) + } } fun removeContactsFromGroup(contacts: ArrayList, groupId: Long) { - + contacts.forEach { + val currentGroupIds = it.groups.map { it.id } as ArrayList + currentGroupIds.remove(groupId) + updateContactGroups(it, currentGroupIds) + } } + fun updateContactGroups(contact: Contact, groupIds: ArrayList) { + val contactValues = fillContactGroupValues(groupIds) + val selection = "$COL_ID = ?" + val selectionArgs = arrayOf(contact.id.toString()) + mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, selectionArgs) + } + + private fun fillContactGroupValues(groupIds: ArrayList): ContentValues { + return ContentValues().apply { + put(COL_GROUPS, Gson().toJson(groupIds)) + } + } + + fun getContacts(activity: BaseSimpleActivity, selection: String? = null, selectionArgs: Array? = null): ArrayList { val storedGroups = ContactsHelper(activity).getStoredGroups() val contacts = ArrayList()