mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-04-11 08:41:00 +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,
|
||||
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,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<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,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<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()
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user