add helper functions to handle contact adding and removing from groups

This commit is contained in:
tibbi 2018-03-21 19:20:55 +01:00
parent cf4b7ca9c5
commit 6eade329d6
4 changed files with 45 additions and 11 deletions

View file

@ -5,10 +5,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.adapters.ContactsAdapter
import com.simplemobiletools.contacts.dialogs.SelectContactsDialog
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.tryStartCall
import com.simplemobiletools.contacts.extensions.viewContact
import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.interfaces.RemoveFromGroupListener
@ -48,11 +45,11 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
private fun fabClicked() {
SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts ->
ContactsHelper(this).apply {
Thread {
addContactsToGroup(addedContacts, group.id)
removeContactsFromGroup(removedContacts, group.id)
}
refreshContacts()
refreshContacts()
}.start()
}
}

View file

@ -21,6 +21,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.dialogs.CreateNewGroupDialog
import com.simplemobiletools.contacts.extensions.addContactsToGroup
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.shareContacts
@ -197,13 +198,17 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
RadioGroupDialog(activity, items, 0) {
if (it as Int == NEW_GROUP_ID) {
CreateNewGroupDialog(activity) {
ContactsHelper(activity).addContactsToGroup(selectedContacts, it.id)
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
Thread {
activity.addContactsToGroup(selectedContacts, it.id)
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
}.start()
finishActMode()
}
} else {
ContactsHelper(activity).addContactsToGroup(selectedContacts, it.toLong())
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
Thread {
activity.addContactsToGroup(selectedContacts, it.toLong())
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
}.start()
finishActMode()
}
}

View file

@ -108,3 +108,27 @@ fun BaseSimpleActivity.getTempFile(): File? {
return File(folder, "contacts.vcf")
}
fun BaseSimpleActivity.addContactsToGroup(contacts: ArrayList<Contact>, groupId: Long) {
val publicContacts = contacts.filter { it.source != SMT_PRIVATE }
val privateContacts = contacts.filter { it.source == SMT_PRIVATE }
if (publicContacts.isNotEmpty()) {
ContactsHelper(this).addContactsToGroup(contacts, groupId)
}
if (privateContacts.isNotEmpty()) {
dbHelper.addContactsToGroup(contacts, groupId)
}
}
fun BaseSimpleActivity.removeContactsFromGroup(contacts: ArrayList<Contact>, groupId: Long) {
val publicContacts = contacts.filter { it.source != SMT_PRIVATE }
val privateContacts = contacts.filter { it.source == SMT_PRIVATE }
if (publicContacts.isNotEmpty()) {
ContactsHelper(this).removeContactsFromGroup(contacts, groupId)
}
if (privateContacts.isNotEmpty()) {
dbHelper.removeContactsFromGroup(contacts, groupId)
}
}

View file

@ -193,6 +193,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
fun addContactsToGroup(contacts: ArrayList<Contact>, groupId: Long) {
}
fun removeContactsFromGroup(contacts: ArrayList<Contact>, groupId: Long) {
}
fun getContacts(activity: BaseSimpleActivity, selection: String? = null, selectionArgs: Array<String>? = null): ArrayList<Contact> {
val storedGroups = ContactsHelper(activity).getStoredGroups()
val contacts = ArrayList<Contact>()