Skip to content

Commit

Permalink
refactor: simplify data types for exporting vcards
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 18, 2023
1 parent 999a8d8 commit 557391e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 58 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/bnyro/contacts/obj/TranslatedType.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bnyro.contacts.obj

import androidx.annotation.StringRes
import ezvcard.parameter.VCardParameter

data class TranslatedType(
val id: Int,
@StringRes val title: Int
@StringRes val title: Int,
val vcardType: VCardParameter? = null
)
28 changes: 16 additions & 12 deletions app/src/main/java/com/bnyro/contacts/util/ContactsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package com.bnyro.contacts.util
import android.provider.ContactsContract
import com.bnyro.contacts.R
import com.bnyro.contacts.obj.TranslatedType
import ezvcard.parameter.AddressType
import ezvcard.parameter.EmailType
import ezvcard.parameter.TelephoneType

object ContactsHelper {

val emailTypes = listOf(
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_HOME, R.string.home),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_WORK, R.string.work),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE, R.string.mobile),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_HOME, R.string.home, EmailType.HOME),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_WORK, R.string.work, EmailType.WORK),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE, R.string.mobile, EmailType.PREF),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM, R.string.custom),
TranslatedType(ContactsContract.CommonDataKinds.Email.TYPE_OTHER, R.string.other)
)

val phoneNumberTypes = listOf(
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_HOME, R.string.home),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, R.string.mobile),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_WORK, R.string.work),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_CAR, R.string.car),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME, R.string.fax_home),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK, R.string.fax_work),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_HOME, R.string.home, TelephoneType.HOME),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, R.string.mobile, TelephoneType.CELL),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_WORK, R.string.work, TelephoneType.WORK),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_CAR, R.string.car, TelephoneType.CAR),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME, R.string.fax_home, TelephoneType.FAX),
TranslatedType(ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK, R.string.fax_work, TelephoneType.FAX),
TranslatedType(
ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT,
R.string.assistant
Expand All @@ -32,11 +34,13 @@ object ContactsHelper {
val addressTypes = listOf(
TranslatedType(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME,
R.string.home
R.string.home,
AddressType.HOME
),
TranslatedType(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK,
R.string.work
R.string.work,
AddressType.WORK
),
TranslatedType(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM,
Expand Down
64 changes: 19 additions & 45 deletions app/src/main/java/com/bnyro/contacts/util/VcardHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package com.bnyro.contacts.util

import android.graphics.BitmapFactory
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds.Email
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal
import com.bnyro.contacts.obj.ContactData
import com.bnyro.contacts.obj.ValueWithType
import ezvcard.Ezvcard
Expand All @@ -23,29 +20,6 @@ import ezvcard.property.StructuredName
import java.util.Date

object VcardHelper {
private val addressTypes = listOf(
Pair(StructuredPostal.TYPE_HOME, AddressType.HOME),
Pair(StructuredPostal.TYPE_WORK, AddressType.WORK),
Pair(StructuredPostal.TYPE_OTHER, null)
)

private val emailTypes = listOf(
Pair(Email.TYPE_HOME, EmailType.HOME),
Pair(Email.TYPE_WORK, EmailType.WORK),
Pair(Email.TYPE_MOBILE, EmailType.PREF),
Pair(Email.TYPE_OTHER, null)
)

private val phoneNumberTypes = listOf(
Pair(Phone.TYPE_MOBILE, TelephoneType.CELL),
Pair(Phone.TYPE_HOME, TelephoneType.HOME),
Pair(Phone.TYPE_WORK, TelephoneType.WORK),
Pair(Phone.TYPE_MAIN, TelephoneType.PREF),
Pair(Phone.TYPE_FAX_HOME, TelephoneType.FAX),
Pair(Phone.TYPE_FAX_WORK, TelephoneType.FAX),
Pair(Phone.TYPE_OTHER, null)
)

fun exportVcard(contacts: List<ContactData>): String {
val vCards = contacts.map { createVcardContact(it) }

Expand All @@ -66,31 +40,31 @@ object VcardHelper {
setOrganization(it)
}
contact.numbers.forEachIndexed { index, number ->
val type = phoneNumberTypes.firstOrNull {
it.first == number.type
}?.second ?: TelephoneType.HOME
val type = ContactsHelper.phoneNumberTypes.firstOrNull {
it.id == number.type
}?.vcardType as? TelephoneType ?: TelephoneType.HOME
runCatching {
addTelephoneNumber(number.value, type).also {
if (index == 0) it.types.add(TelephoneType.PREF)
}
}
}
contact.emails.forEach { email ->
val type = emailTypes.firstOrNull {
it.first == email.type
}?.second ?: EmailType.HOME
val type = ContactsHelper.emailTypes.firstOrNull {
it.id == email.type
}?.vcardType as? EmailType ?: EmailType.HOME
runCatching {
addEmail(email.value, type)
}
}
contact.addresses.forEach { address ->
val addressType = addressTypes.firstOrNull {
it.first == address.type
}?.second?.value ?: AddressType.HOME.value
val addressType = ContactsHelper.addressTypes.firstOrNull {
it.id == address.type
}?.vcardType as? AddressType ?: AddressType.HOME
runCatching {
val newAddress = Address().apply {
streetAddress = address.value
parameters.addType(addressType)
parameters.addType(addressType?.value)
}
addAddress(newAddress)
}
Expand Down Expand Up @@ -140,17 +114,17 @@ object VcardHelper {
}.map { number ->
ValueWithType(
number.text,
phoneNumberTypes.firstOrNull { pair ->
pair.second == number.types.firstOrNull()
}?.first
ContactsHelper.phoneNumberTypes.firstOrNull { type ->
type.vcardType == number.types.firstOrNull()
}?.id
)
},
emails = it.emails.orEmpty().map { email ->
ValueWithType(
email.value,
emailTypes.firstOrNull { pair ->
pair.second == email.types.firstOrNull()
}?.first
ContactsHelper.emailTypes.firstOrNull { type ->
type.vcardType == email.types.firstOrNull()
}?.id
)
},
addresses = it.addresses.orEmpty().map { address ->
Expand All @@ -162,9 +136,9 @@ object VcardHelper {
address.postalCode,
address.country
).filter { entry -> entry.isNotBlank() }.joinToString(" ").trim(),
addressTypes.firstOrNull { pair ->
pair.second == address.types.firstOrNull()
}?.first
ContactsHelper.addressTypes.firstOrNull { type ->
type.vcardType == address.types.firstOrNull()
}?.id
)
},
notes = it.notes.orEmpty().map { note ->
Expand Down

0 comments on commit 557391e

Please sign in to comment.