Contacts/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt

120 lines
4.5 KiB
Kotlin

package com.simplemobiletools.contacts.activities
import android.os.Bundle
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.helpers.*
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.interfaces.RemoveFromGroupListener
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.activity_group_contacts.*
class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, RefreshContactsListener {
private var allContacts = ArrayList<Contact>()
private var groupContacts = ArrayList<Contact>()
lateinit var group: Group
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_group_contacts)
updateTextColors(group_contacts_coordinator)
group = intent.extras.getSerializable(GROUP) as Group
supportActionBar?.title = group.title
group_contacts_fab.setOnClickListener {
fabClicked()
}
group_contacts_placeholder_2.setOnClickListener {
fabClicked()
}
group_contacts_placeholder_2.underlineText()
group_contacts_placeholder_2.setTextColor(getAdjustedPrimaryColor())
}
override fun onResume() {
super.onResume()
refreshContacts()
}
private fun fabClicked() {
SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts ->
ContactsHelper(this).apply {
addContactsToGroup(addedContacts, group.id)
removeContactsFromGroup(removedContacts, group.id)
}
refreshContacts()
}
}
private fun refreshContacts() {
ContactsHelper(this).getContacts {
allContacts = it
groupContacts = it.filter { it.groups.map { it.id }.contains(group.id) } as ArrayList<Contact>
group_contacts_placeholder_2.beVisibleIf(groupContacts.isEmpty())
group_contacts_placeholder.beVisibleIf(groupContacts.isEmpty())
group_contacts_list.beVisibleIf(groupContacts.isNotEmpty())
Contact.sorting = config.sorting
groupContacts.sort()
updateContacts(groupContacts)
}
}
private fun updateContacts(contacts: ArrayList<Contact>) {
val currAdapter = group_contacts_list.adapter
if (currAdapter == null) {
ContactsAdapter(this, contacts, this, LOCATION_GROUP_CONTACTS, this, group_contacts_list, group_contacts_fastscroller) {
when (config.onContactClick) {
ON_CLICK_CALL_CONTACT -> {
val contact = it as Contact
if (contact.phoneNumbers.isNotEmpty()) {
tryStartCall(it)
} else {
toast(R.string.no_phone_number_found)
}
}
ON_CLICK_VIEW_CONTACT -> viewContact(it as Contact)
ON_CLICK_EDIT_CONTACT -> editContact(it as Contact)
}
}.apply {
setupDragListener(true)
addVerticalDividers(true)
group_contacts_list.adapter = this
}
group_contacts_fastscroller.setScrollTo(0)
group_contacts_fastscroller.setViews(group_contacts_list) {
val item = (group_contacts_list.adapter as ContactsAdapter).contactItems.getOrNull(it)
group_contacts_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
}
} else {
(currAdapter as ContactsAdapter).updateItems(contacts)
}
}
override fun refreshContacts(refreshContactsTab: Boolean, refreshFavoritesTab: Boolean) {
refreshContacts()
}
override fun refreshFavorites() {
}
override fun removeFromGroup(contacts: ArrayList<Contact>) {
ContactsHelper(this).removeContactsFromGroup(contacts, group.id)
if (groupContacts.size == 0) {
refreshContacts()
}
}
}