mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-04-19 12:17:19 +02:00
Merge pull request #817 from pavelpoley/task/contact-count
Show contact count at contact sources
This commit is contained in:
commit
7d02e2c81c
@ -16,8 +16,8 @@ class FilterContactSourcesAdapter(
|
|||||||
val activity: SimpleActivity,
|
val activity: SimpleActivity,
|
||||||
private val contactSources: List<ContactSource>,
|
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>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -62,7 +62,9 @@ class FilterContactSourcesAdapter(
|
|||||||
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())
|
||||||
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) }
|
filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.simplemobiletools.contacts.pro.dialogs
|
package com.simplemobiletools.contacts.pro.dialogs
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
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.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
|
||||||
|
import com.simplemobiletools.contacts.pro.models.Contact
|
||||||
import com.simplemobiletools.contacts.pro.models.ContactSource
|
import com.simplemobiletools.contacts.pro.models.ContactSource
|
||||||
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
|
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class ExportContactsDialog(val activity: SimpleActivity, val path: String, val hidePath: Boolean,
|
class ExportContactsDialog(
|
||||||
private val callback: (file: File, ignoredContactSources: HashSet<String>) -> Unit) {
|
val activity: SimpleActivity, val path: String, val hidePath: Boolean,
|
||||||
private var contactSources = ArrayList<ContactSource>()
|
private val callback: (file: File, ignoredContactSources: HashSet<String>) -> Unit
|
||||||
|
) {
|
||||||
private var ignoreClicks = false
|
private var ignoreClicks = false
|
||||||
private var realPath = if (path.isEmpty()) activity.internalStoragePath else path
|
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 {
|
init {
|
||||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_contacts, null) as ViewGroup).apply {
|
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 {
|
ContactsHelper(activity).getContactSources { contactSources ->
|
||||||
it.mapTo(contactSources) { it.copy() }
|
contactSources.mapTo(this@ExportContactsDialog.contactSources) { it.copy() }
|
||||||
activity.runOnUiThread {
|
isContactSourcesReady = true
|
||||||
export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.getVisibleContactSources())
|
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 {
|
ensureBackgroundThread {
|
||||||
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.filter { !selectedSources.contains(it) }.map { it.getFullIdentifier() }.toHashSet()
|
val ignoredSources = contactSources
|
||||||
|
.filter { !selectedSources.contains(it) }
|
||||||
|
.map { it.getFullIdentifier() }
|
||||||
|
.toHashSet()
|
||||||
callback(file, ignoredSources)
|
callback(file, ignoredSources)
|
||||||
dismiss()
|
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.extensions.getVisibleContactSources
|
||||||
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||||
import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE
|
import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE
|
||||||
|
import com.simplemobiletools.contacts.pro.models.Contact
|
||||||
import com.simplemobiletools.contacts.pro.models.ContactSource
|
import com.simplemobiletools.contacts.pro.models.ContactSource
|
||||||
import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.*
|
import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.*
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class FilterContactSourcesDialog(val activity: SimpleActivity, private val callback: () -> Unit) {
|
class FilterContactSourcesDialog(val activity: SimpleActivity, private val callback: () -> Unit) {
|
||||||
private var dialog: AlertDialog? = null
|
private var dialog: AlertDialog? = null
|
||||||
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null)
|
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null)
|
||||||
private var contactSources = ArrayList<ContactSource>()
|
private var contactSources = ArrayList<ContactSource>()
|
||||||
|
private var contacts = ArrayList<Contact>()
|
||||||
|
private var isContactSourcesReady = false
|
||||||
|
private var isContactsReady = false
|
||||||
|
|
||||||
init {
|
init {
|
||||||
ContactsHelper(activity).getContactSources {
|
ContactsHelper(activity).getContactSources { contactSources ->
|
||||||
if (it.isEmpty()) {
|
contactSources.mapTo(this@FilterContactSourcesDialog.contactSources) { it.copy() }
|
||||||
return@getContactSources
|
isContactSourcesReady = true
|
||||||
|
processDataIfReady()
|
||||||
}
|
}
|
||||||
|
|
||||||
it.mapTo(contactSources) { it.copy() }
|
ContactsHelper(activity).getContacts(getAll = true) { contacts ->
|
||||||
val selectedSources = activity.getVisibleContactSources()
|
contacts.mapTo(this@FilterContactSourcesDialog.contacts) { it.copy() }
|
||||||
activity.runOnUiThread {
|
isContactsReady = true
|
||||||
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources)
|
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)
|
dialog = AlertDialog.Builder(activity)
|
||||||
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() }
|
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() }
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user