Merge pull request #413 from Naveen3Singh/vcard_preview_2

Properly extract name from vcard
This commit is contained in:
Tibor Kaputa 2022-09-01 12:10:04 +02:00 committed by GitHub
commit 40bb318146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 16 deletions

View File

@ -367,7 +367,7 @@ class ThreadAdapter(
thread_mesage_attachments_holder.addView(vCardView)
parseVCardFromUri(context, uri) { vCards ->
val title = vCards.firstOrNull()?.formattedName?.value
val title = vCards.firstOrNull()?.parseNameFromVCard()
val imageIcon = if (title != null) {
SimpleContactsHelper(context).getContactLetterIcon(title)
} else {

View File

@ -13,3 +13,20 @@ fun parseVCardFromUri(context: Context, uri: Uri, callback: (vCards: List<VCard>
callback(vCards)
}
}
fun VCard?.parseNameFromVCard(): String? {
if (this == null) return null
var fullName = formattedName?.value
if (fullName.isNullOrEmpty()) {
val structured = structuredName ?: return null
val nameComponents = arrayListOf<String?>().apply {
addAll(structured.prefixes)
add(structured.given)
addAll(structured.additionalNames)
add(structured.family)
addAll(structured.suffixes)
}
fullName = nameComponents.filter { !it.isNullOrEmpty() }.joinToString(separator = " ")
}
return fullName
}

View File

@ -5,6 +5,7 @@ import com.simplemobiletools.commons.extensions.normalizePhoneNumber
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.extensions.format
import com.simplemobiletools.smsmessenger.helpers.parseNameFromVCard
import ezvcard.VCard
import ezvcard.property.*
@ -15,27 +16,13 @@ private val displayedPropertyClasses = arrayOf(
data class VCardWrapper(val vCard: VCard, val fullName: String?, val properties: List<VCardPropertyWrapper>, var expanded: Boolean = false) {
companion object {
private fun VCard.extractFullName(): String? {
var fullName = formattedName?.value
if (fullName.isNullOrEmpty()) {
val structured = structuredName
val given = structured?.given
val family = structured.family
fullName = if (family != null) {
given?.plus(" ")?.plus(family)
} else {
given
}
}
return fullName
}
fun from(context: Context, vCard: VCard): VCardWrapper {
val properties = vCard.properties
.filter { displayedPropertyClasses.contains(it::class.java) }
.map { VCardPropertyWrapper.from(context, it) }
.distinctBy { it.value }
val fullName = vCard.extractFullName()
val fullName = vCard.parseNameFromVCard()
return VCardWrapper(vCard, fullName, properties)
}