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( | class FilterContactSourcesAdapter( | ||||||
|     val activity: SimpleActivity, |     val activity: SimpleActivity, | ||||||
|     private val data: List<ContactSourceModel>, |     private val contactSources: List<ContactSource>, | ||||||
|     private val displayContactSources: ArrayList<String> |     private val displayContactSources: ArrayList<String> | ||||||
| ) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { | ) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { | ||||||
|  |  | ||||||
|     private val selectedKeys = HashSet<Int>() |     private val selectedKeys = HashSet<Int>() | ||||||
|  |  | ||||||
|     data class ContactSourceModel(val contactSource: ContactSource, val count: Int) |  | ||||||
|  |  | ||||||
|     init { |     init { | ||||||
|         data.forEachIndexed { index, model -> |         contactSources.forEachIndexed { index, contactSource -> | ||||||
|             if (displayContactSources.contains(model.contactSource.name)) { |             if (displayContactSources.contains(contactSource.name)) { | ||||||
|                 selectedKeys.add(model.hashCode()) |                 selectedKeys.add(contactSource.hashCode()) | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if (model.contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) { |             if (contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) { | ||||||
|                 selectedKeys.add(model.hashCode()) |                 selectedKeys.add(contactSource.hashCode()) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun toggleItemSelection(select: Boolean, contactSource: ContactSourceModel, position: Int) { |     private fun toggleItemSelection(select: Boolean, contactSource: ContactSource, position: Int) { | ||||||
|         if (select) { |         if (select) { | ||||||
|             selectedKeys.add(contactSource.hashCode()) |             selectedKeys.add(contactSource.hashCode()) | ||||||
|         } else { |         } else { | ||||||
| @@ -44,7 +42,7 @@ class FilterContactSourcesAdapter( | |||||||
|         notifyItemChanged(position) |         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 { |     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||||||
|         val view = activity.layoutInflater.inflate(R.layout.item_filter_contact_source, parent, false) |         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) { |     override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||||||
|         val model = data[position] |         val contactSource = contactSources[position] | ||||||
|         holder.bindView(model) |         holder.bindView(contactSource) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     override fun getItemCount() = data.size |     override fun getItemCount() = contactSources.size | ||||||
|  |  | ||||||
|     inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { |     inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||||||
|         fun bindView(model: ContactSourceModel): View { |         fun bindView(contactSource: ContactSource): View { | ||||||
|             val isSelected = selectedKeys.contains(model.hashCode()) |             val isSelected = selectedKeys.contains(contactSource.hashCode()) | ||||||
|             itemView.apply { |             itemView.apply { | ||||||
|                 filter_contact_source_checkbox.isChecked = isSelected |                 filter_contact_source_checkbox.isChecked = isSelected | ||||||
|                 filter_contact_source_checkbox.setColors(activity.getProperTextColor(), activity.getProperPrimaryColor(), activity.getProperBackgroundColor()) |                 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_checkbox.text = displayName | ||||||
|                 filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, model) } |                 filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return itemView |             return itemView | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         private fun viewClicked(select: Boolean, contactSource: ContactSourceModel) { |         private fun viewClicked(select: Boolean, contactSource: ContactSource) { | ||||||
|             toggleItemSelection(select, contactSource, adapterPosition) |             toggleItemSelection(select, contactSource, adapterPosition) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -84,7 +84,7 @@ class ExportContactsDialog( | |||||||
|                                     activity.config.lastExportPath = file.absolutePath.getParentPath() |                                     activity.config.lastExportPath = file.absolutePath.getParentPath() | ||||||
|                                     val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() |                                     val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() | ||||||
|                                     val ignoredSources = contactSources |                                     val ignoredSources = contactSources | ||||||
|                                         .filter { !selectedSources.map { source -> source.contactSource }.contains(it) } |                                         .filter { !selectedSources.contains(it) } | ||||||
|                                         .map { it.getFullIdentifier() } |                                         .map { it.getFullIdentifier() } | ||||||
|                                         .toHashSet() |                                         .toHashSet() | ||||||
|                                     callback(file, ignoredSources) |                                     callback(file, ignoredSources) | ||||||
| @@ -103,14 +103,17 @@ class ExportContactsDialog( | |||||||
|             return |             return | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         val adapterData = ArrayList<FilterContactSourcesAdapter.ContactSourceModel>() |         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||||
|         for (source in contactSources) { |         for (source in contactSources) { | ||||||
|             val count = contacts.filter { it.source == source.name }.count() |             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 { |         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.R | ||||||
| import com.simplemobiletools.contacts.pro.activities.SimpleActivity | import com.simplemobiletools.contacts.pro.activities.SimpleActivity | ||||||
| import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter | 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.config | ||||||
| import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources | import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources | ||||||
| import com.simplemobiletools.contacts.pro.helpers.ContactsHelper | import com.simplemobiletools.contacts.pro.helpers.ContactsHelper | ||||||
| @@ -41,15 +40,18 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb | |||||||
|             return |             return | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         val adapterData = ArrayList<ContactSourceModel>() |         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||||
|         for (source in contactSources) { |         for (contactSource in contactSources) { | ||||||
|             val count = contacts.filter { it.source == source.name }.count() |             val count = contacts.filter { it.source == contactSource.name }.count() | ||||||
|             adapterData.add(ContactSourceModel(source, count)) |             contactSourcesWithCount.add(contactSource.copy(count = count)) | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         contactSources.clear() | ||||||
|  |         contactSources.addAll(contactSourcesWithCount) | ||||||
|  |  | ||||||
|         val selectedSources = activity.getVisibleContactSources() |         val selectedSources = activity.getVisibleContactSources() | ||||||
|         activity.runOnUiThread { |         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) |             dialog = AlertDialog.Builder(activity) | ||||||
|                 .setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() } |                 .setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() } | ||||||
| @@ -62,9 +64,7 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb | |||||||
|  |  | ||||||
|     private fun confirmContactSources() { |     private fun confirmContactSources() { | ||||||
|         val selectedContactSources = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() |         val selectedContactSources = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() | ||||||
|         val ignoredContactSources = contactSources |         val ignoredContactSources = contactSources.filter { !selectedContactSources.contains(it) }.map { | ||||||
|             .filter { !selectedContactSources.map { it.contactSource }.contains(it) } |  | ||||||
|             .map { |  | ||||||
|             if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier() |             if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier() | ||||||
|         }.toHashSet() |         }.toHashSet() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ package com.simplemobiletools.contacts.pro.models | |||||||
|  |  | ||||||
| import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE | 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 { |     fun getFullIdentifier(): String { | ||||||
|         return if (type == SMT_PRIVATE) { |         return if (type == SMT_PRIVATE) { | ||||||
|             type |             type | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user