mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Merge pull request #817 from pavelpoley/task/contact-count
Show contact count at contact sources
This commit is contained in:
		| @@ -16,8 +16,8 @@ class FilterContactSourcesAdapter( | ||||
|     val activity: SimpleActivity, | ||||
|     private val contactSources: List<ContactSource>, | ||||
|     private val displayContactSources: ArrayList<String> | ||||
| ) : | ||||
|     RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { | ||||
| ) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { | ||||
|  | ||||
|     private val selectedKeys = HashSet<Int>() | ||||
|  | ||||
|     init { | ||||
| @@ -62,7 +62,9 @@ class FilterContactSourcesAdapter( | ||||
|             itemView.apply { | ||||
|                 filter_contact_source_checkbox.isChecked = isSelected | ||||
|                 filter_contact_source_checkbox.setColors(activity.getProperTextColor(), activity.getProperPrimaryColor(), activity.getProperBackgroundColor()) | ||||
|                 filter_contact_source_checkbox.text = contactSource.publicName | ||||
|                 val countText = if (contactSource.count >= 0) " (${contactSource.count})" else "" | ||||
|                 val displayName = "${contactSource.publicName}$countText" | ||||
|                 filter_contact_source_checkbox.text = displayName | ||||
|                 filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) } | ||||
|             } | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package com.simplemobiletools.contacts.pro.dialogs | ||||
|  | ||||
| import android.view.View | ||||
| import android.view.ViewGroup | ||||
| import androidx.appcompat.app.AlertDialog | ||||
| import com.simplemobiletools.commons.dialogs.FilePickerDialog | ||||
| @@ -11,16 +12,21 @@ import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter | ||||
| import com.simplemobiletools.contacts.pro.extensions.config | ||||
| import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources | ||||
| import com.simplemobiletools.contacts.pro.helpers.ContactsHelper | ||||
| import com.simplemobiletools.contacts.pro.models.Contact | ||||
| import com.simplemobiletools.contacts.pro.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, val hidePath: Boolean, | ||||
|                            private val callback: (file: File, ignoredContactSources: HashSet<String>) -> Unit) { | ||||
|     private var contactSources = ArrayList<ContactSource>() | ||||
| class ExportContactsDialog( | ||||
|     val activity: SimpleActivity, val path: String, val hidePath: Boolean, | ||||
|     private val callback: (file: File, ignoredContactSources: HashSet<String>) -> Unit | ||||
| ) { | ||||
|     private var ignoreClicks = false | ||||
|     private var realPath = if (path.isEmpty()) activity.internalStoragePath else path | ||||
|     private var contactSources = ArrayList<ContactSource>() | ||||
|     private var contacts = ArrayList<Contact>() | ||||
|     private var isContactSourcesReady = false | ||||
|     private var isContactsReady = false | ||||
|  | ||||
|     init { | ||||
|         val view = (activity.layoutInflater.inflate(R.layout.dialog_export_contacts, null) as ViewGroup).apply { | ||||
| @@ -40,11 +46,16 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, val h | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             ContactsHelper(activity).getContactSources { | ||||
|                 it.mapTo(contactSources) { it.copy() } | ||||
|                 activity.runOnUiThread { | ||||
|                     export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.getVisibleContactSources()) | ||||
|             ContactsHelper(activity).getContactSources { contactSources -> | ||||
|                 contactSources.mapTo(this@ExportContactsDialog.contactSources) { it.copy() } | ||||
|                 isContactSourcesReady = true | ||||
|                 processDataIfReady(this) | ||||
|             } | ||||
|  | ||||
|             ContactsHelper(activity).getContacts(getAll = true) { contacts -> | ||||
|                 contacts.mapTo(this@ExportContactsDialog.contacts) { it.copy() } | ||||
|                 isContactsReady = true | ||||
|                 processDataIfReady(this) | ||||
|             } | ||||
|         } | ||||
|  | ||||
| @@ -72,7 +83,10 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, val h | ||||
|                                 ensureBackgroundThread { | ||||
|                                     activity.config.lastExportPath = file.absolutePath.getParentPath() | ||||
|                                     val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources() | ||||
|                                         val ignoredSources = contactSources.filter { !selectedSources.contains(it) }.map { it.getFullIdentifier() }.toHashSet() | ||||
|                                     val ignoredSources = contactSources | ||||
|                                         .filter { !selectedSources.contains(it) } | ||||
|                                         .map { it.getFullIdentifier() } | ||||
|                                         .toHashSet() | ||||
|                                     callback(file, ignoredSources) | ||||
|                                     dismiss() | ||||
|                                 } | ||||
| @@ -83,4 +97,23 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, val h | ||||
|                 } | ||||
|             } | ||||
|     } | ||||
|  | ||||
|     private fun processDataIfReady(view: View) { | ||||
|         if (!isContactSourcesReady || !isContactsReady) { | ||||
|             return | ||||
|         } | ||||
|  | ||||
|         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||
|         for (source in contactSources) { | ||||
|             val count = contacts.filter { it.source == source.name }.count() | ||||
|             contactSourcesWithCount.add(source.copy(count = count)) | ||||
|         } | ||||
|  | ||||
|         contactSources.clear() | ||||
|         contactSources.addAll(contactSourcesWithCount) | ||||
|  | ||||
|         activity.runOnUiThread { | ||||
|             view.export_contacts_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, activity.getVisibleContactSources()) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -9,26 +9,55 @@ import com.simplemobiletools.contacts.pro.extensions.config | ||||
| import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources | ||||
| import com.simplemobiletools.contacts.pro.helpers.ContactsHelper | ||||
| import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE | ||||
| import com.simplemobiletools.contacts.pro.models.Contact | ||||
| import com.simplemobiletools.contacts.pro.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>() | ||||
|     private var contacts = ArrayList<Contact>() | ||||
|     private var isContactSourcesReady = false | ||||
|     private var isContactsReady = false | ||||
|  | ||||
|     init { | ||||
|         ContactsHelper(activity).getContactSources { | ||||
|             if (it.isEmpty()) { | ||||
|                 return@getContactSources | ||||
|         ContactsHelper(activity).getContactSources { contactSources -> | ||||
|             contactSources.mapTo(this@FilterContactSourcesDialog.contactSources) { it.copy() } | ||||
|             isContactSourcesReady = true | ||||
|             processDataIfReady() | ||||
|         } | ||||
|  | ||||
|             it.mapTo(contactSources) { it.copy() } | ||||
|             val selectedSources = activity.getVisibleContactSources() | ||||
|             activity.runOnUiThread { | ||||
|                 view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) | ||||
|         ContactsHelper(activity).getContacts(getAll = true) { contacts -> | ||||
|             contacts.mapTo(this@FilterContactSourcesDialog.contacts) { it.copy() } | ||||
|             isContactsReady = true | ||||
|             processDataIfReady() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun processDataIfReady() { | ||||
|         if (!isContactSourcesReady) { | ||||
|             return | ||||
|         } | ||||
|  | ||||
|         val contactSourcesWithCount = ArrayList<ContactSource>() | ||||
|         for (contactSource in contactSources) { | ||||
|             val count = if (isContactsReady) { | ||||
|                 contacts.filter { it.source == contactSource.name }.count() | ||||
|             } else { | ||||
|                 -1 | ||||
|             } | ||||
|             contactSourcesWithCount.add(contactSource.copy(count = count)) | ||||
|         } | ||||
|  | ||||
|         contactSources.clear() | ||||
|         contactSources.addAll(contactSourcesWithCount) | ||||
|  | ||||
|         activity.runOnUiThread { | ||||
|             val selectedSources = activity.getVisibleContactSources() | ||||
|             view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, selectedSources) | ||||
|  | ||||
|             if (dialog == null) { | ||||
|                 dialog = AlertDialog.Builder(activity) | ||||
|                     .setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() } | ||||
|                     .setNegativeButton(R.string.cancel, null) | ||||
|   | ||||
| @@ -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