From ce917560dae0939de886d81cc0cc770850aec42f Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Mar 2018 10:34:46 +0100 Subject: [PATCH] clean up SelectContactsDialog --- .../activities/GroupContactsActivity.kt | 14 ++++++++++- .../adapters/SelectContactsAdapter.kt | 4 +-- .../contacts/dialogs/SelectContactsDialog.kt | 25 ++++++++++--------- .../contacts/fragments/FavoritesFragment.kt | 12 ++++----- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt index c3e0d09d..3f345a05 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/GroupContactsActivity.kt @@ -5,6 +5,7 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors 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 @@ -15,14 +16,25 @@ import com.simplemobiletools.contacts.models.Group import kotlinx.android.synthetic.main.activity_group_contacts.* class GroupContactsActivity : SimpleActivity() { + lateinit var group: Group + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_group_contacts) updateTextColors(group_contacts_coordinator) - val group = intent.extras.getSerializable(GROUP) as Group + group = intent.extras.getSerializable(GROUP) as Group supportActionBar?.title = group.title + refreshContacts() + group_contacts_fab.setOnClickListener { + SelectContactsDialog(this) { displayedContacts, selectedContacts -> + refreshContacts() + } + } + } + + private fun refreshContacts() { ContactsHelper(this).getContacts { val contacts = it.filter { it.groups.map { it.id }.contains(group.id) } as ArrayList updateContacts(contacts) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/SelectContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/SelectContactsAdapter.kt index b2bc35cb..4e118f24 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/SelectContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/SelectContactsAdapter.kt @@ -23,7 +23,7 @@ import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.* import java.util.* -class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List, private val selectedContacts: ArrayList, private val allowPickMultiple: Boolean, +class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List, private val selectedContacts: ArrayList, private val allowPickMultiple: Boolean, private val itemClick: ((Contact) -> Unit)? = null) : RecyclerView.Adapter() { private val itemViews = SparseArray() private val selectedPositions = HashSet() @@ -39,7 +39,7 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List - if (selectedContacts.contains(contact.id.toString())) { + if (selectedContacts.map { it.id }.contains(contact.id)) { selectedPositions.add(index) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt index f8e9687d..8d4d0a6d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt @@ -11,28 +11,27 @@ import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.layout_select_contact.view.* -class SelectContactsDialog(val activity: SimpleActivity, private val callback: (displayedContacts: ArrayList, selectedContacts: HashSet) -> Unit) { +class SelectContactsDialog(val activity: SimpleActivity, private val callback: (addedContacts: ArrayList, removedContacts: ArrayList) -> Unit) { private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null) - private val config = activity.config - private var allContacts = ArrayList() + private var initiallySelectedContacts = ArrayList() init { ContactsHelper(activity).getContacts { - allContacts = it + var allContacts = it - val contactSources = config.displayContactSources + val contactSources = activity.config.displayContactSources if (!activity.config.showAllContacts()) { allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList } - val favorites = allContacts.filter { it.starred == 1 }.map { it.id.toString() } as ArrayList + initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList - Contact.sorting = config.sorting + Contact.sorting = activity.config.sorting allContacts.sort() activity.runOnUiThread { view.apply { - select_contact_list.adapter = SelectContactsAdapter(activity, allContacts, favorites, true) + select_contact_list.adapter = SelectContactsAdapter(activity, allContacts, initiallySelectedContacts, true) select_contact_fastscroller.allowBubbleDisplay = activity.baseConfig.showInfoBubble select_contact_fastscroller.setViews(select_contact_list) { select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText()) @@ -51,10 +50,12 @@ class SelectContactsDialog(val activity: SimpleActivity, private val callback: ( private fun dialogConfirmed() { Thread { - val allDisplayedContacts = ArrayList() - allContacts.mapTo(allDisplayedContacts, { it }) - val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet() - callback(allDisplayedContacts, selectedContacts) + val adapter = view?.select_contact_list?.adapter as? SelectContactsAdapter + val selectedContacts = adapter?.getSelectedItemsSet()?.toList() ?: ArrayList() + + val newlySelectedContacts = selectedContacts.filter { !initiallySelectedContacts.contains(it) } as ArrayList + val unselectedContacts = initiallySelectedContacts.filter { !selectedContacts.contains(it) } as ArrayList + callback(newlySelectedContacts, unselectedContacts) }.start() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt index 741bf30e..705eba44 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt @@ -5,7 +5,6 @@ import android.util.AttributeSet import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.dialogs.SelectContactsDialog import com.simplemobiletools.contacts.helpers.ContactsHelper -import com.simplemobiletools.contacts.models.Contact class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { override fun fabClicked() { @@ -18,13 +17,12 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa } private fun showAddFavoritesDialog() { - SelectContactsDialog(activity!!) { displayedContacts, selectedContacts -> - val contactsHelper = ContactsHelper(activity as SimpleActivity) - val contactsToAdd = selectedContacts.map { it } as ArrayList - contactsHelper.addFavorites(contactsToAdd) + SelectContactsDialog(activity!!) { addedContacts, removedContacts -> + ContactsHelper(activity as SimpleActivity).apply { + addFavorites(addedContacts) + removeFavorites(removedContacts) + } - displayedContacts.removeAll(selectedContacts) - contactsHelper.removeFavorites(displayedContacts) activity!!.refreshContacts(false, true) } }