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 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 val config = activity.config
private var allContacts = ArrayList<Contact>()
@ -45,23 +45,16 @@ class AddFavoritesDialog(val activity: SimpleActivity, private val callback: ()
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
activity.setupDialogStuff(view, this)
}
}
private fun dialogConfirmed() {
Thread {
val contactsHelper = ContactsHelper(activity)
val allDisplayedContacts = ArrayList<Contact>()
allContacts.mapTo(allDisplayedContacts, { it })
val selectedContacts = (view?.select_contact_list?.adapter as? SelectContactsAdapter)?.getSelectedItemsSet() ?: LinkedHashSet()
val contactsToAdd = selectedContacts.map { it } as ArrayList<Contact>
contactsHelper.addFavorites(contactsToAdd)
allDisplayedContacts.removeAll(selectedContacts)
contactsHelper.removeFavorites(allDisplayedContacts)
callback()
callback(allDisplayedContacts, selectedContacts)
}.start()
}
}

View File

@ -2,7 +2,10 @@ package com.simplemobiletools.contacts.fragments
import android.content.Context
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) {
override fun fabClicked() {
@ -15,7 +18,13 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
}
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)
}
}