store selected favorites

This commit is contained in:
tibbi 2017-12-30 13:54:46 +01:00
parent c8a3bfb9b5
commit 0e3f500bb9
5 changed files with 28 additions and 3 deletions

View file

@ -20,7 +20,8 @@ import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.*
import java.util.*
class AddFavoritesAdapter(val activity: SimpleActivity, val contacts: List<Contact>) : RecyclerView.Adapter<AddFavoritesAdapter.ViewHolder>() {
class AddFavoritesAdapter(val activity: SimpleActivity, val contacts: List<Contact>, val selectedContacts: Set<String>)
: RecyclerView.Adapter<AddFavoritesAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
private val config = activity.config
@ -29,6 +30,14 @@ class AddFavoritesAdapter(val activity: SimpleActivity, val contacts: List<Conta
private val startNameWithSurname = config.startNameWithSurname
private val itemLayout = if (config.showPhoneNumbers) R.layout.item_add_favorite_with_number else R.layout.item_add_favorite_without_number
init {
contacts.forEachIndexed { index, contact ->
if (selectedContacts.contains(contact.id.toString())) {
selectedPositions.add(index)
}
}
}
private fun toggleItemSelection(select: Boolean, pos: Int) {
if (select) {
if (itemViews[pos] != null) {

View file

@ -18,7 +18,7 @@ class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit)
ContactsHelper(activity).getContacts {
Contact.sorting = activity.config.sorting
it.sort()
view.add_favorites_list.adapter = AddFavoritesAdapter(activity, it)
view.add_favorites_list.adapter = AddFavoritesAdapter(activity, it, activity.config.favorites)
activity.runOnUiThread {
dialog = AlertDialog.Builder(activity)
@ -33,6 +33,10 @@ class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit)
private fun dialogConfirmed() {
val selectedItems = (view.add_favorites_list.adapter as AddFavoritesAdapter).getSelectedItemsSet()
if (activity.config.favorites != selectedItems) {
activity.config.favorites = selectedItems
callback()
}
dialog?.dismiss()
}
}

View file

@ -15,6 +15,8 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
addNewFavorites()
}
}
initFavorites()
}
override fun textColorChanged(color: Int) {
@ -29,9 +31,13 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
override fun onActivityResume() {
}
private fun initFavorites() {
}
private fun addNewFavorites() {
AddFavoritesDialog(activity!!) {
initFavorites()
}
}
}

View file

@ -4,6 +4,7 @@ import android.content.Context
import com.simplemobiletools.commons.helpers.BaseConfig
import com.simplemobiletools.commons.helpers.SORTING
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import java.util.*
class Config(context: Context) : BaseConfig(context) {
companion object {
@ -39,4 +40,8 @@ class Config(context: Context) : BaseConfig(context) {
var lastUsedViewPagerPage: Int
get() = prefs.getInt(LAST_USED_VIEW_PAGER_PAGE, 0)
set(lastUsedViewPagerPage) = prefs.edit().putInt(LAST_USED_VIEW_PAGER_PAGE, lastUsedViewPagerPage).apply()
var favorites: Set<String>
get() = prefs.getStringSet(FAVORITES, HashSet<String>())
set(favorites) = prefs.edit().remove(FAVORITES).putStringSet(FAVORITES, favorites).apply()
}

View file

@ -7,6 +7,7 @@ val DISPLAY_CONTACT_SOURCES = "display_contact_sources"
val START_NAME_WITH_SURNAME = "start_name_with_surname"
val LAST_USED_CONTACT_SOURCE = "last_used_contact_source"
val LAST_USED_VIEW_PAGER_PAGE = "last_used_view_pager_page"
val FAVORITES = "favorites"
val CONTACT_ID = "contact_id"