mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-02-17 03:51:03 +01:00
show contact count at contact sources
This commit is contained in:
parent
a1cadedeaf
commit
bae7af1474
@ -14,25 +14,27 @@ import kotlinx.android.synthetic.main.item_filter_contact_source.view.*
|
|||||||
|
|
||||||
class FilterContactSourcesAdapter(
|
class FilterContactSourcesAdapter(
|
||||||
val activity: SimpleActivity,
|
val activity: SimpleActivity,
|
||||||
private val contactSources: List<ContactSource>,
|
private val data: List<ContactSourceModel>,
|
||||||
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 {
|
||||||
contactSources.forEachIndexed { index, contactSource ->
|
data.forEachIndexed { index, model ->
|
||||||
if (displayContactSources.contains(contactSource.name)) {
|
if (displayContactSources.contains(model.contactSource.name)) {
|
||||||
selectedKeys.add(contactSource.hashCode())
|
selectedKeys.add(model.hashCode())
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) {
|
if (model.contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) {
|
||||||
selectedKeys.add(contactSource.hashCode())
|
selectedKeys.add(model.hashCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toggleItemSelection(select: Boolean, contactSource: ContactSource, position: Int) {
|
private fun toggleItemSelection(select: Boolean, contactSource: ContactSourceModel, position: Int) {
|
||||||
if (select) {
|
if (select) {
|
||||||
selectedKeys.add(contactSource.hashCode())
|
selectedKeys.add(contactSource.hashCode())
|
||||||
} else {
|
} else {
|
||||||
@ -42,7 +44,7 @@ class FilterContactSourcesAdapter(
|
|||||||
notifyItemChanged(position)
|
notifyItemChanged(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSelectedContactSources() = contactSources.filter { selectedKeys.contains(it.hashCode()) }
|
fun getSelectedContactSources() = data.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)
|
||||||
@ -50,26 +52,27 @@ class FilterContactSourcesAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
val contactSource = contactSources[position]
|
val model = data[position]
|
||||||
holder.bindView(contactSource)
|
holder.bindView(model)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemCount() = contactSources.size
|
override fun getItemCount() = data.size
|
||||||
|
|
||||||
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
fun bindView(contactSource: ContactSource): View {
|
fun bindView(model: ContactSourceModel): View {
|
||||||
val isSelected = selectedKeys.contains(contactSource.hashCode())
|
val isSelected = selectedKeys.contains(model.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())
|
||||||
filter_contact_source_checkbox.text = contactSource.publicName
|
val displayName = "${model.contactSource.publicName} (${model.count})"
|
||||||
filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) }
|
filter_contact_source_checkbox.text = displayName
|
||||||
|
filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, model) }
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemView
|
return itemView
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun viewClicked(select: Boolean, contactSource: ContactSource) {
|
private fun viewClicked(select: Boolean, contactSource: ContactSourceModel) {
|
||||||
toggleItemSelection(select, contactSource, adapterPosition)
|
toggleItemSelection(select, contactSource, adapterPosition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,47 +46,71 @@ 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AlertDialog.Builder(activity)
|
AlertDialog.Builder(activity)
|
||||||
.setPositiveButton(R.string.ok, null)
|
.setPositiveButton(R.string.ok, null)
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.create().apply {
|
.create().apply {
|
||||||
activity.setupDialogStuff(view, this, R.string.export_contacts) {
|
activity.setupDialogStuff(view, this, R.string.export_contacts) {
|
||||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||||
if (view.export_contacts_list.adapter == null || ignoreClicks) {
|
if (view.export_contacts_list.adapter == null || ignoreClicks) {
|
||||||
return@setOnClickListener
|
return@setOnClickListener
|
||||||
}
|
}
|
||||||
|
|
||||||
val filename = view.export_contacts_filename.value
|
val filename = view.export_contacts_filename.value
|
||||||
when {
|
when {
|
||||||
filename.isEmpty() -> activity.toast(R.string.empty_name)
|
filename.isEmpty() -> activity.toast(R.string.empty_name)
|
||||||
filename.isAValidFilename() -> {
|
filename.isAValidFilename() -> {
|
||||||
val file = File(realPath, "$filename.vcf")
|
val file = File(realPath, "$filename.vcf")
|
||||||
if (!hidePath && file.exists()) {
|
if (!hidePath && file.exists()) {
|
||||||
activity.toast(R.string.name_taken)
|
activity.toast(R.string.name_taken)
|
||||||
return@setOnClickListener
|
return@setOnClickListener
|
||||||
}
|
}
|
||||||
|
|
||||||
ignoreClicks = true
|
ignoreClicks = true
|
||||||
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
|
||||||
callback(file, ignoredSources)
|
.filter { !selectedSources.map { source -> source.contactSource }.contains(it) }
|
||||||
dismiss()
|
.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 adapterData = ArrayList<FilterContactSourcesAdapter.ContactSourceModel>()
|
||||||
|
for (source in contactSources) {
|
||||||
|
val count = contacts.filter { it.source == source.name }.count()
|
||||||
|
adapterData.add(FilterContactSourcesAdapter.ContactSourceModel(source, count))
|
||||||
|
}
|
||||||
|
|
||||||
|
activity.runOnUiThread {
|
||||||
|
view.export_contacts_list.adapter = FilterContactSourcesAdapter(activity, adapterData, activity.getVisibleContactSources())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,45 +5,68 @@ 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
|
||||||
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.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.contacts) { it.copy() }
|
||||||
activity.runOnUiThread {
|
isContactsReady = true
|
||||||
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources)
|
processDataIfReady()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dialog = AlertDialog.Builder(activity)
|
private fun processDataIfReady() {
|
||||||
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() }
|
if (!isContactSourcesReady || !isContactsReady) {
|
||||||
.setNegativeButton(R.string.cancel, null)
|
return
|
||||||
.create().apply {
|
}
|
||||||
activity.setupDialogStuff(view, this)
|
|
||||||
}
|
val adapterData = ArrayList<ContactSourceModel>()
|
||||||
}
|
for (source in contactSources) {
|
||||||
|
val count = contacts.filter { it.source == source.name }.count()
|
||||||
|
adapterData.add(ContactSourceModel(source, count))
|
||||||
|
}
|
||||||
|
|
||||||
|
val selectedSources = activity.getVisibleContactSources()
|
||||||
|
activity.runOnUiThread {
|
||||||
|
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, adapterData, selectedSources)
|
||||||
|
|
||||||
|
dialog = AlertDialog.Builder(activity)
|
||||||
|
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() }
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create().apply {
|
||||||
|
activity.setupDialogStuff(view, this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.filter { !selectedContactSources.contains(it) }.map {
|
val ignoredContactSources = contactSources
|
||||||
if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier()
|
.filter { !selectedContactSources.map { it.contactSource }.contains(it) }
|
||||||
}.toHashSet()
|
.map {
|
||||||
|
if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier()
|
||||||
|
}.toHashSet()
|
||||||
|
|
||||||
if (activity.getVisibleContactSources() != ignoredContactSources) {
|
if (activity.getVisibleContactSources() != ignoredContactSources) {
|
||||||
activity.config.ignoredContactSources = ignoredContactSources
|
activity.config.ignoredContactSources = ignoredContactSources
|
||||||
|
Loading…
x
Reference in New Issue
Block a user