clean up SelectContactsDialog

This commit is contained in:
tibbi 2018-03-20 10:34:46 +01:00
parent e60fa65eef
commit ce917560da
4 changed files with 33 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.adapters.ContactsAdapter import com.simplemobiletools.contacts.adapters.ContactsAdapter
import com.simplemobiletools.contacts.dialogs.SelectContactsDialog
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.editContact import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.tryStartCall import com.simplemobiletools.contacts.extensions.tryStartCall
@ -15,14 +16,25 @@ import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.activity_group_contacts.* import kotlinx.android.synthetic.main.activity_group_contacts.*
class GroupContactsActivity : SimpleActivity() { class GroupContactsActivity : SimpleActivity() {
lateinit var group: Group
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_group_contacts) setContentView(R.layout.activity_group_contacts)
updateTextColors(group_contacts_coordinator) updateTextColors(group_contacts_coordinator)
val group = intent.extras.getSerializable(GROUP) as Group group = intent.extras.getSerializable(GROUP) as Group
supportActionBar?.title = group.title supportActionBar?.title = group.title
refreshContacts()
group_contacts_fab.setOnClickListener {
SelectContactsDialog(this) { displayedContacts, selectedContacts ->
refreshContacts()
}
}
}
private fun refreshContacts() {
ContactsHelper(this).getContacts { ContactsHelper(this).getContacts {
val contacts = it.filter { it.groups.map { it.id }.contains(group.id) } as ArrayList<Contact> val contacts = it.filter { it.groups.map { it.id }.contains(group.id) } as ArrayList<Contact>
updateContacts(contacts) updateContacts(contacts)

View File

@ -23,7 +23,7 @@ import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.* import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.*
import java.util.* import java.util.*
class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Contact>, private val selectedContacts: ArrayList<String>, private val allowPickMultiple: Boolean, class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Contact>, private val selectedContacts: ArrayList<Contact>, private val allowPickMultiple: Boolean,
private val itemClick: ((Contact) -> Unit)? = null) : RecyclerView.Adapter<SelectContactsAdapter.ViewHolder>() { private val itemClick: ((Contact) -> Unit)? = null) : RecyclerView.Adapter<SelectContactsAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>() private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>() private val selectedPositions = HashSet<Int>()
@ -39,7 +39,7 @@ class SelectContactsAdapter(val activity: SimpleActivity, val contacts: List<Con
init { init {
contacts.forEachIndexed { index, contact -> contacts.forEachIndexed { index, contact ->
if (selectedContacts.contains(contact.id.toString())) { if (selectedContacts.map { it.id }.contains(contact.id)) {
selectedPositions.add(index) selectedPositions.add(index)
} }
} }

View File

@ -11,28 +11,27 @@ import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.layout_select_contact.view.* import kotlinx.android.synthetic.main.layout_select_contact.view.*
class SelectContactsDialog(val activity: SimpleActivity, private val callback: (displayedContacts: ArrayList<Contact>, selectedContacts: HashSet<Contact>) -> Unit) { class SelectContactsDialog(val activity: SimpleActivity, private val callback: (addedContacts: ArrayList<Contact>, removedContacts: ArrayList<Contact>) -> Unit) {
private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null) private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null)
private val config = activity.config private var initiallySelectedContacts = ArrayList<Contact>()
private var allContacts = ArrayList<Contact>()
init { init {
ContactsHelper(activity).getContacts { ContactsHelper(activity).getContacts {
allContacts = it var allContacts = it
val contactSources = config.displayContactSources val contactSources = activity.config.displayContactSources
if (!activity.config.showAllContacts()) { if (!activity.config.showAllContacts()) {
allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact> allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
} }
val favorites = allContacts.filter { it.starred == 1 }.map { it.id.toString() } as ArrayList<String> initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList<Contact>
Contact.sorting = config.sorting Contact.sorting = activity.config.sorting
allContacts.sort() allContacts.sort()
activity.runOnUiThread { activity.runOnUiThread {
view.apply { 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.allowBubbleDisplay = activity.baseConfig.showInfoBubble
select_contact_fastscroller.setViews(select_contact_list) { select_contact_fastscroller.setViews(select_contact_list) {
select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText()) select_contact_fastscroller.updateBubbleText(allContacts[it].getBubbleText())
@ -51,10 +50,12 @@ class SelectContactsDialog(val activity: SimpleActivity, private val callback: (
private fun dialogConfirmed() { private fun dialogConfirmed() {
Thread { Thread {
val allDisplayedContacts = ArrayList<Contact>() val adapter = view?.select_contact_list?.adapter as? SelectContactsAdapter
allContacts.mapTo(allDisplayedContacts, { it }) val selectedContacts = adapter?.getSelectedItemsSet()?.toList() ?: ArrayList()
val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet()
callback(allDisplayedContacts, selectedContacts) val newlySelectedContacts = selectedContacts.filter { !initiallySelectedContacts.contains(it) } as ArrayList
val unselectedContacts = initiallySelectedContacts.filter { !selectedContacts.contains(it) } as ArrayList
callback(newlySelectedContacts, unselectedContacts)
}.start() }.start()
} }
} }

View File

@ -5,7 +5,6 @@ import android.util.AttributeSet
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.dialogs.SelectContactsDialog import com.simplemobiletools.contacts.dialogs.SelectContactsDialog
import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact
class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
override fun fabClicked() { override fun fabClicked() {
@ -18,13 +17,12 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
private fun showAddFavoritesDialog() { private fun showAddFavoritesDialog() {
SelectContactsDialog(activity!!) { displayedContacts, selectedContacts -> SelectContactsDialog(activity!!) { addedContacts, removedContacts ->
val contactsHelper = ContactsHelper(activity as SimpleActivity) ContactsHelper(activity as SimpleActivity).apply {
val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact> addFavorites(addedContacts)
contactsHelper.addFavorites(contactsToAdd) removeFavorites(removedContacts)
}
displayedContacts.removeAll(selectedContacts)
contactsHelper.removeFavorites(displayedContacts)
activity!!.refreshContacts(false, true) activity!!.refreshContacts(false, true)
} }
} }