From e5423218ebd8a39b9e5d8dc4d3a3379faedfa5e5 Mon Sep 17 00:00:00 2001 From: Pavel Poley Date: Thu, 5 May 2022 12:49:12 +0300 Subject: [PATCH] refactor ContactSourceModel --- .../adapters/FilterContactSourcesAdapter.kt | 34 +++++++++---------- .../pro/dialogs/ExportContactsDialog.kt | 11 +++--- .../pro/dialogs/FilterContactSourcesDialog.kt | 22 ++++++------ .../contacts/pro/models/ContactSource.kt | 2 +- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/FilterContactSourcesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/FilterContactSourcesAdapter.kt index 7ad1d146..868d83ef 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/FilterContactSourcesAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/FilterContactSourcesAdapter.kt @@ -14,27 +14,25 @@ import kotlinx.android.synthetic.main.item_filter_contact_source.view.* class FilterContactSourcesAdapter( val activity: SimpleActivity, - private val data: List, + private val contactSources: List, private val displayContactSources: ArrayList ) : RecyclerView.Adapter() { private val selectedKeys = HashSet() - data class ContactSourceModel(val contactSource: ContactSource, val count: Int) - init { - data.forEachIndexed { index, model -> - if (displayContactSources.contains(model.contactSource.name)) { - selectedKeys.add(model.hashCode()) + contactSources.forEachIndexed { index, contactSource -> + if (displayContactSources.contains(contactSource.name)) { + selectedKeys.add(contactSource.hashCode()) } - if (model.contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) { - selectedKeys.add(model.hashCode()) + if (contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) { + selectedKeys.add(contactSource.hashCode()) } } } - private fun toggleItemSelection(select: Boolean, contactSource: ContactSourceModel, position: Int) { + private fun toggleItemSelection(select: Boolean, contactSource: ContactSource, position: Int) { if (select) { selectedKeys.add(contactSource.hashCode()) } else { @@ -44,7 +42,7 @@ class FilterContactSourcesAdapter( notifyItemChanged(position) } - fun getSelectedContactSources() = data.filter { selectedKeys.contains(it.hashCode()) } + fun getSelectedContactSources() = contactSources.filter { selectedKeys.contains(it.hashCode()) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = activity.layoutInflater.inflate(R.layout.item_filter_contact_source, parent, false) @@ -52,27 +50,27 @@ class FilterContactSourcesAdapter( } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val model = data[position] - holder.bindView(model) + val contactSource = contactSources[position] + holder.bindView(contactSource) } - override fun getItemCount() = data.size + override fun getItemCount() = contactSources.size inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { - fun bindView(model: ContactSourceModel): View { - val isSelected = selectedKeys.contains(model.hashCode()) + fun bindView(contactSource: ContactSource): View { + val isSelected = selectedKeys.contains(contactSource.hashCode()) itemView.apply { filter_contact_source_checkbox.isChecked = isSelected filter_contact_source_checkbox.setColors(activity.getProperTextColor(), activity.getProperPrimaryColor(), activity.getProperBackgroundColor()) - val displayName = "${model.contactSource.publicName} (${model.count})" + val displayName = "${contactSource.publicName} (${contactSource.count})" filter_contact_source_checkbox.text = displayName - filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, model) } + filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) } } return itemView } - private fun viewClicked(select: Boolean, contactSource: ContactSourceModel) { + private fun viewClicked(select: Boolean, contactSource: ContactSource) { toggleItemSelection(select, contactSource, adapterPosition) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt index 604853e1..ef8e91df 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt @@ -84,7 +84,7 @@ class ExportContactsDialog( activity.config.lastExportPath = file.absolutePath.getParentPath() val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() val ignoredSources = contactSources - .filter { !selectedSources.map { source -> source.contactSource }.contains(it) } + .filter { !selectedSources.contains(it) } .map { it.getFullIdentifier() } .toHashSet() callback(file, ignoredSources) @@ -103,14 +103,17 @@ class ExportContactsDialog( return } - val adapterData = ArrayList() + val contactSourcesWithCount = ArrayList() for (source in contactSources) { val count = contacts.filter { it.source == source.name }.count() - adapterData.add(FilterContactSourcesAdapter.ContactSourceModel(source, count)) + contactSourcesWithCount.add(source.copy(count = count)) } + contactSources.clear() + contactSources.addAll(contactSourcesWithCount) + activity.runOnUiThread { - view.export_contacts_list.adapter = FilterContactSourcesAdapter(activity, adapterData, activity.getVisibleContactSources()) + view.export_contacts_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, activity.getVisibleContactSources()) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/FilterContactSourcesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/FilterContactSourcesDialog.kt index 71465224..ef07f938 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/FilterContactSourcesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/FilterContactSourcesDialog.kt @@ -5,7 +5,6 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter -import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter.ContactSourceModel import com.simplemobiletools.contacts.pro.extensions.config import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources import com.simplemobiletools.contacts.pro.helpers.ContactsHelper @@ -41,15 +40,18 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb return } - val adapterData = ArrayList() - for (source in contactSources) { - val count = contacts.filter { it.source == source.name }.count() - adapterData.add(ContactSourceModel(source, count)) + val contactSourcesWithCount = ArrayList() + for (contactSource in contactSources) { + val count = contacts.filter { it.source == contactSource.name }.count() + contactSourcesWithCount.add(contactSource.copy(count = count)) } + contactSources.clear() + contactSources.addAll(contactSourcesWithCount) + val selectedSources = activity.getVisibleContactSources() activity.runOnUiThread { - view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, adapterData, selectedSources) + view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, selectedSources) dialog = AlertDialog.Builder(activity) .setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() } @@ -62,11 +64,9 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb private fun confirmContactSources() { val selectedContactSources = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() - val ignoredContactSources = contactSources - .filter { !selectedContactSources.map { it.contactSource }.contains(it) } - .map { - if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier() - }.toHashSet() + val ignoredContactSources = contactSources.filter { !selectedContactSources.contains(it) }.map { + if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier() + }.toHashSet() if (activity.getVisibleContactSources() != ignoredContactSources) { activity.config.ignoredContactSources = ignoredContactSources diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/ContactSource.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/ContactSource.kt index a5f55e30..d9976154 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/ContactSource.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/ContactSource.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.contacts.pro.models import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE -data class ContactSource(var name: String, var type: String, var publicName: String) { +data class ContactSource(var name: String, var type: String, var publicName: String, var count: Int = 0) { fun getFullIdentifier(): String { return if (type == SMT_PRIVATE) { type