allow removing favorites by long pressing them + Remove

This commit is contained in:
tibbi 2017-12-30 19:08:42 +01:00
parent d21b3e7c4d
commit 5efc2fadb6
2 changed files with 30 additions and 0 deletions

View file

@ -22,6 +22,7 @@ import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.interfaces.RefreshContactsListener
import kotlinx.android.synthetic.main.item_contact_with_number.view.*
import java.util.*
class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Contact>, val listener: RefreshContactsListener?,
val isFavoritesFragment: Boolean, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
@ -119,7 +120,20 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
}
private fun removeFavorites() {
if (selectedPositions.isEmpty()) {
return
}
val favoritesToRemove = ArrayList<Contact>()
selectedPositions.sortedDescending().forEach {
favoritesToRemove.add(contactItems[it])
}
contactItems.removeAll(favoritesToRemove)
val favoriteIDsToRemove = HashSet<String>()
favoritesToRemove.map { favoriteIDsToRemove.add(it.id.toString()) }
activity.config.removeFavorites(favoriteIDsToRemove)
removeSelectedItems()
}
private fun addToFavorites() {

View file

@ -44,4 +44,20 @@ class Config(context: Context) : BaseConfig(context) {
var favorites: Set<String>
get() = prefs.getStringSet(FAVORITES, HashSet<String>())
set(favorites) = prefs.edit().remove(FAVORITES).putStringSet(FAVORITES, favorites).apply()
fun addFavorite(id: String) {
addFavorites(HashSet<String>(Arrays.asList(id)))
}
private fun addFavorites(favs: Set<String>) {
val currFavorites = HashSet<String>(favs)
currFavorites.addAll(favs)
favorites = currFavorites
}
fun removeFavorites(favs: Set<String>) {
val currFavorites = HashSet<String>(favorites)
currFavorites.removeAll(favs)
favorites = currFavorites
}
}