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 39baf6e6..c96c373e 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 @@ -16,8 +16,8 @@ class FilterContactSourcesAdapter( val activity: SimpleActivity, private val contactSources: List, private val displayContactSources: ArrayList -) : - RecyclerView.Adapter() { +) : RecyclerView.Adapter() { + private val selectedKeys = HashSet() 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) } } 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 f0457a18..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 @@ -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) -> Unit) { - private var contactSources = ArrayList() +class ExportContactsDialog( + val activity: SimpleActivity, val path: String, val hidePath: Boolean, + private val callback: (file: File, ignoredContactSources: HashSet) -> Unit +) { private var ignoreClicks = false private var realPath = if (path.isEmpty()) activity.internalStoragePath else path + private var contactSources = ArrayList() + private var contacts = ArrayList() + private var isContactSourcesReady = false + private var isContactsReady = false init { val view = (activity.layoutInflater.inflate(R.layout.dialog_export_contacts, null) as ViewGroup).apply { @@ -40,47 +46,74 @@ 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) } } AlertDialog.Builder(activity) - .setPositiveButton(R.string.ok, null) - .setNegativeButton(R.string.cancel, null) - .create().apply { - activity.setupDialogStuff(view, this, R.string.export_contacts) { - getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - if (view.export_contacts_list.adapter == null || ignoreClicks) { - return@setOnClickListener - } + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this, R.string.export_contacts) { + getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + if (view.export_contacts_list.adapter == null || ignoreClicks) { + return@setOnClickListener + } - val filename = view.export_contacts_filename.value - when { - filename.isEmpty() -> activity.toast(R.string.empty_name) - filename.isAValidFilename() -> { - val file = File(realPath, "$filename.vcf") - if (!hidePath && file.exists()) { - activity.toast(R.string.name_taken) - return@setOnClickListener - } - - ignoreClicks = true - 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() - callback(file, ignoredSources) - dismiss() - } + val filename = view.export_contacts_filename.value + when { + filename.isEmpty() -> activity.toast(R.string.empty_name) + filename.isAValidFilename() -> { + val file = File(realPath, "$filename.vcf") + if (!hidePath && file.exists()) { + activity.toast(R.string.name_taken) + return@setOnClickListener + } + + ignoreClicks = true + 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() + callback(file, ignoredSources) + dismiss() } - else -> activity.toast(R.string.invalid_name) } + else -> activity.toast(R.string.invalid_name) } } } + } + } + + private fun processDataIfReady(view: View) { + if (!isContactSourcesReady || !isContactsReady) { + return + } + + val contactSourcesWithCount = ArrayList() + 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()) + } } } 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 1090c7d3..6c2351fc 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 @@ -9,32 +9,61 @@ 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() + private var contacts = ArrayList() + 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() + } + + 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() + for (contactSource in contactSources) { + val count = if (isContactsReady) { + contacts.filter { it.source == contactSource.name }.count() + } else { + -1 } + contactSourcesWithCount.add(contactSource.copy(count = count)) + } - it.mapTo(contactSources) { it.copy() } + contactSources.clear() + contactSources.addAll(contactSourcesWithCount) + + activity.runOnUiThread { val selectedSources = activity.getVisibleContactSources() - activity.runOnUiThread { - view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) + 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) - .create().apply { - activity.setupDialogStuff(view, this) - } + .setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() } + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) + } } } } 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