mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	refactor ContactSourceModel
This commit is contained in:
		| @@ -14,27 +14,25 @@ import kotlinx.android.synthetic.main.item_filter_contact_source.view.* | ||||
|  | ||||
| class FilterContactSourcesAdapter( | ||||
|     val activity: SimpleActivity, | ||||
|     private val data: List<ContactSourceModel>, | ||||
|     private val contactSources: List<ContactSource>, | ||||
|     private val displayContactSources: ArrayList<String> | ||||
| ) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { | ||||
|  | ||||
|     private val selectedKeys = HashSet<Int>() | ||||
|  | ||||
|     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) | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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<FilterContactSourcesAdapter.ContactSourceModel>() | ||||
|         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||
|         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()) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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<ContactSourceModel>() | ||||
|         for (source in contactSources) { | ||||
|             val count = contacts.filter { it.source == source.name }.count() | ||||
|             adapterData.add(ContactSourceModel(source, count)) | ||||
|         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||
|         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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user