Skip to content

Commit

Permalink
Merge pull request #364 from you-apps/phone-number-formatting
Browse files Browse the repository at this point in the history
feat: format phone numbers using international standards
  • Loading branch information
Bnyro authored Jan 17, 2024
2 parents 81031da + 37125fd commit 79f280c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ dependencies {
// Image parsing
implementation("androidx.exifinterface:exifinterface:1.3.6")

// Phone number formatting
implementation("com.googlecode.libphonenumber:libphonenumber:8.2.0")

// Markdown support for notes
implementation("com.halilibo.compose-richtext:richtext-ui-material3:0.17.0")
implementation("com.halilibo.compose-richtext:richtext-commonmark:0.17.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.bnyro.contacts.ui.models.ContactsModel
import com.bnyro.contacts.ui.screens.MainAppContent
import com.bnyro.contacts.ui.theme.ConnectYouTheme
import com.bnyro.contacts.util.BackupHelper
import com.bnyro.contacts.util.ContactsHelper
import java.net.URLDecoder

class MainActivity : BaseActivity() {
Expand Down Expand Up @@ -106,7 +107,7 @@ class MainActivity : BaseActivity() {
?: return null
val body = intent?.getStringExtra(Intent.EXTRA_TEXT)

return address.replace(ContactsModel.normalizeNumberRegex, "") to body
return ContactsHelper.normalizePhoneNumber(address) to body
}

private fun handleVcfShareAction(contactsModel: ContactsModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.bnyro.contacts.repo.ContactsRepository
import com.bnyro.contacts.repo.DeviceContactsRepository
import com.bnyro.contacts.repo.LocalContactsRepository
import com.bnyro.contacts.ui.models.state.ContactListState
import com.bnyro.contacts.util.ContactsHelper
import com.bnyro.contacts.util.ExportHelper
import com.bnyro.contacts.util.IntentHelper
import com.bnyro.contacts.util.PermissionHelper
Expand Down Expand Up @@ -217,17 +218,15 @@ class ContactsModel(
fun getAvailableGroups() = contacts.map { it.groups }.flatten().distinct()

fun getContactByNumber(number: String): ContactData? {
val normalizedNumber = number.replace(normalizeNumberRegex, "")
val normalizedNumber = ContactsHelper.normalizePhoneNumber(number)
return contacts.firstOrNull {
it.numbers.any { (value, _) ->
value.replace(normalizeNumberRegex, "") == normalizedNumber
ContactsHelper.normalizePhoneNumber(value) == normalizedNumber
}
}
}

companion object {
val normalizeNumberRegex = Regex("[-_ ]")

val Factory = viewModelFactory {
initializer {
val application = this[APPLICATION_KEY] as App
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Message
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.Shortcut
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
Expand Down Expand Up @@ -227,7 +233,9 @@ fun SingleContactScreen(contact: ContactData, onClose: () -> Unit) {

ContactEntryGroup(
label = stringResource(R.string.phone),
entries = contact.numbers,
entries = contact.numbers.map {
ValueWithType(ContactsHelper.normalizePhoneNumber(it.value), it.type)
},
types = ContactsHelper.phoneNumberTypes
) {
IntentHelper.launchAction(context, IntentActionType.DIAL, it.value)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/bnyro/contacts/util/ContactsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bnyro.contacts.util
import android.provider.ContactsContract
import com.bnyro.contacts.R
import com.bnyro.contacts.obj.TranslatedType
import com.google.i18n.phonenumbers.PhoneNumberUtil
import ezvcard.parameter.AddressType
import ezvcard.parameter.EmailType
import ezvcard.parameter.TelephoneType
Expand Down Expand Up @@ -93,4 +94,11 @@ object ContactsHelper {
}
}
}

fun normalizePhoneNumber(number: String): String {
val phoneUtil = PhoneNumberUtil.getInstance()
val phoneNumber = runCatching { phoneUtil.parse(number, null) }
.getOrElse { return number }
return phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)
}
}

0 comments on commit 79f280c

Please sign in to comment.