handle Favorite items modification in FavoritesFragment itself

This commit is contained in:
tibbi 2018-03-20 10:02:07 +01:00
parent 3c413347d6
commit e60fa65eef
2 changed files with 15 additions and 13 deletions

View File

@ -11,7 +11,7 @@ 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 AddFavoritesDialog(val activity: SimpleActivity, private val callback: () -> Unit) { class SelectContactsDialog(val activity: SimpleActivity, private val callback: (displayedContacts: ArrayList<Contact>, selectedContacts: HashSet<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 val config = activity.config
private var allContacts = ArrayList<Contact>() private var allContacts = ArrayList<Contact>()
@ -51,17 +51,10 @@ class AddFavoritesDialog(val activity: SimpleActivity, private val callback: ()
private fun dialogConfirmed() { private fun dialogConfirmed() {
Thread { Thread {
val contactsHelper = ContactsHelper(activity)
val allDisplayedContacts = ArrayList<Contact>() val allDisplayedContacts = ArrayList<Contact>()
allContacts.mapTo(allDisplayedContacts, { it }) allContacts.mapTo(allDisplayedContacts, { it })
val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet() val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet()
val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact> callback(allDisplayedContacts, selectedContacts)
contactsHelper.addFavorites(contactsToAdd)
allDisplayedContacts.removeAll(selectedContacts)
contactsHelper.removeFavorites(allDisplayedContacts)
callback()
}.start() }.start()
} }
} }

View File

@ -2,7 +2,10 @@ package com.simplemobiletools.contacts.fragments
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import com.simplemobiletools.contacts.dialogs.AddFavoritesDialog 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) { class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
override fun fabClicked() { override fun fabClicked() {
@ -15,7 +18,13 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
private fun showAddFavoritesDialog() { private fun showAddFavoritesDialog() {
AddFavoritesDialog(activity!!) { SelectContactsDialog(activity!!) { displayedContacts, selectedContacts ->
val contactsHelper = ContactsHelper(activity as SimpleActivity)
val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact>
contactsHelper.addFavorites(contactsToAdd)
displayedContacts.removeAll(selectedContacts)
contactsHelper.removeFavorites(displayedContacts)
activity!!.refreshContacts(false, true) activity!!.refreshContacts(false, true)
} }
} }