handle adding and removing secret contacts to/from groups

This commit is contained in:
tibbi 2018-03-21 19:51:06 +01:00
parent 485e73afa7
commit abbeb218b8
1 changed files with 24 additions and 2 deletions

View File

@ -194,13 +194,35 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
fun addContactsToGroup(contacts: ArrayList<Contact>, groupId: Long) { fun addContactsToGroup(contacts: ArrayList<Contact>, groupId: Long) {
contacts.forEach {
val currentGroupIds = it.groups.map { it.id } as ArrayList<Long>
currentGroupIds.add(groupId)
updateContactGroups(it, currentGroupIds)
}
} }
fun removeContactsFromGroup(contacts: ArrayList<Contact>, groupId: Long) { fun removeContactsFromGroup(contacts: ArrayList<Contact>, groupId: Long) {
contacts.forEach {
val currentGroupIds = it.groups.map { it.id } as ArrayList<Long>
currentGroupIds.remove(groupId)
updateContactGroups(it, currentGroupIds)
}
} }
fun updateContactGroups(contact: Contact, groupIds: ArrayList<Long>) {
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<Long>): ContentValues {
return ContentValues().apply {
put(COL_GROUPS, Gson().toJson(groupIds))
}
}
fun getContacts(activity: BaseSimpleActivity, selection: String? = null, selectionArgs: Array<String>? = null): ArrayList<Contact> { fun getContacts(activity: BaseSimpleActivity, selection: String? = null, selectionArgs: Array<String>? = null): ArrayList<Contact> {
val storedGroups = ContactsHelper(activity).getStoredGroups() val storedGroups = ContactsHelper(activity).getStoredGroups()
val contacts = ArrayList<Contact>() val contacts = ArrayList<Contact>()