show the modified contact source at the FilterContactSources adapter

This commit is contained in:
tibbi 2018-02-02 12:30:33 +01:00
parent 3bb7860bb7
commit 996a9ead0a
3 changed files with 34 additions and 16 deletions

View File

@ -9,19 +9,24 @@ import com.simplemobiletools.commons.interfaces.MyAdapterListener
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.models.ContactSource
import kotlinx.android.synthetic.main.item_filter_contact_source.view.*
import java.util.*
class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List<String>, private val displayContactSources: Set<String>) :
class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List<ContactSource>, private val displayContactSources: Set<String>) :
RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
init {
contactSources.forEachIndexed { index, value ->
if (activity.config.showAllContacts() || displayContactSources.contains(value)) {
contactSources.forEachIndexed { index, contactSource ->
if (activity.config.showAllContacts() || displayContactSources.contains(contactSource.name)) {
selectedPositions.add(index)
}
if (contactSource.name == activity.config.localAccountName && contactSource.type == activity.config.localAccountType) {
contactSource.name = activity.getString(R.string.phone_storage)
}
}
}
@ -47,11 +52,7 @@ class FilterContactSourcesAdapter(val activity: SimpleActivity, private val cont
override fun itemLongClicked(position: Int) {}
}
fun getSelectedItemsSet(): HashSet<String> {
val selectedItemsSet = HashSet<String>(selectedPositions.size)
selectedPositions.forEach { selectedItemsSet.add(contactSources[it]) }
return selectedItemsSet
}
fun getSelectedItemsSet() = selectedPositions
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = activity.layoutInflater.inflate(R.layout.item_filter_contact_source, parent, false)
@ -60,7 +61,7 @@ class FilterContactSourcesAdapter(val activity: SimpleActivity, private val cont
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val contactSource = contactSources[position]
itemViews.put(position, holder.bindView(contactSource))
itemViews.put(position, holder.bindView(contactSource.name))
toggleItemSelection(selectedPositions.contains(position), position)
}

View File

@ -8,10 +8,13 @@ import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.ContactSource
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
import java.io.File
import java.util.*
class ExportContactsDialog(val activity: SimpleActivity, val path: String, private val callback: (file: File, contactSources: HashSet<String>) -> Unit) {
private var contactSources = ArrayList<ContactSource>()
init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_contacts, null) as ViewGroup).apply {
@ -19,8 +22,9 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
export_contacts_filename.setText("contacts_${System.currentTimeMillis() / 1000}")
ContactsHelper(activity).getContactSources {
it.mapTo(contactSources, { it.copy() })
activity.runOnUiThread {
export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it.map { it.name }, activity.config.displayContactSources)
export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.config.displayContactSources)
}
}
}
@ -41,8 +45,12 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
return@setOnClickListener
}
val contactSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet()
callback(file, contactSources)
val selectedIndexes = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet()
val selectedContactSources = HashSet<String>()
selectedIndexes.forEach {
selectedContactSources.add(contactSources[it].name)
}
callback(file, selectedContactSources)
dismiss()
}
else -> activity.toast(R.string.invalid_name)

View File

@ -7,11 +7,14 @@ import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.ContactSource
import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.*
import java.util.*
class FilterContactSourcesDialog(val activity: SimpleActivity, private val callback: () -> Unit) {
private var dialog: AlertDialog? = null
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null)
private var contactSources = ArrayList<ContactSource>()
init {
ContactsHelper(activity).getContactSources {
@ -19,9 +22,10 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb
return@getContactSources
}
it.mapTo(contactSources, { it.copy() })
val selectedSources = activity.config.displayContactSources
activity.runOnUiThread {
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it.map { it.name }, selectedSources)
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources)
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialogInterface, i -> confirmEventTypes() })
@ -34,9 +38,14 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb
}
private fun confirmEventTypes() {
val selectedItems = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet()
if (activity.config.displayContactSources != selectedItems) {
activity.config.displayContactSources = selectedItems
val selectedIndexes = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet()
val selectedContactSources = HashSet<String>()
selectedIndexes.forEach {
selectedContactSources.add(contactSources[it].name)
}
if (activity.config.displayContactSources != selectedContactSources) {
activity.config.displayContactSources = selectedContactSources
callback()
}
dialog?.dismiss()