diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/VCardViewerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/VCardViewerActivity.kt index 3b1bc15c..b8f2296b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/VCardViewerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/VCardViewerActivity.kt @@ -76,6 +76,6 @@ class VCardViewerActivity : SimpleActivity() { } private fun prepareData(vCards: List): List { - return vCards.map { VCardWrapper(it) } + return vCards.map { vCard -> VCardWrapper.from(this, vCard) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/VCardViewerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/VCardViewerAdapter.kt index bbeeb1bf..2b08b175 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/VCardViewerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/VCardViewerAdapter.kt @@ -20,7 +20,7 @@ import kotlinx.android.synthetic.main.item_vcard_contact.view.* import kotlinx.android.synthetic.main.item_vcard_contact_property.view.* class VCardViewerAdapter( - private val activity: SimpleActivity, private var items: MutableList, private val itemClick: (Any) -> Unit + activity: SimpleActivity, private var items: MutableList, private val itemClick: (Any) -> Unit ) : RecyclerView.Adapter() { private var fontSize = activity.getTextSize() @@ -53,7 +53,7 @@ class VCardViewerAdapter( } private fun setupVCardView(view: View, item: VCardWrapper) { - val name = item.getFullName() + val name = item.fullName view.apply { item_contact_name.apply { text = name @@ -121,7 +121,7 @@ class VCardViewerAdapter( } private fun expandOrCollapseRow(view: View, item: VCardWrapper) { - val properties = item.getVCardProperties(context = activity) + val properties = item.properties if (item.expanded) { collapseRow(view, properties, item) } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/VCard.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/VCard.kt index 23609674..64f67cc9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/VCard.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/VCard.kt @@ -12,28 +12,33 @@ private val displayedPropertyClasses = arrayOf( Telephone::class.java, Email::class.java, Organization::class.java, Birthday::class.java, Anniversary::class.java, Note::class.java ) -data class VCardWrapper(val vCard: VCard, var expanded: Boolean = false) { +data class VCardWrapper(val vCard: VCard, val fullName: String?, val properties: List, var expanded: Boolean = false) { - fun getFullName(): String? { - var formattedName = vCard.formattedName?.value - if (formattedName.isNullOrEmpty()) { - val structured = vCard.structuredName - val given = structured?.given - val family = structured.family - formattedName = if (family != null) { - given?.plus(" ")?.plus(family) - } else { - given + 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 } - return formattedName - } - fun getVCardProperties(context: Context): List { - return vCard.properties - .filter { displayedPropertyClasses.contains(it::class.java) } - .map { VCardPropertyWrapper.from(context, it) } - .distinctBy { it.value } + 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() + + return VCardWrapper(vCard, fullName, properties, expanded = false) + } } }