Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Last active September 13, 2021 09:21
Show Gist options
  • Save rahulsainani/c8df05b83bfe87a54bc350a155117ce0 to your computer and use it in GitHub Desktop.
Save rahulsainani/c8df05b83bfe87a54bc350a155117ce0 to your computer and use it in GitHub Desktop.
class PhoneNumberFormatter @Inject constructor(
private val phoneNumberUtil: PhoneNumberUtil
) {
fun formatToCountryCodeAndNationalNumber(phoneNumberWithCountryCode: String): Pair<Int, Long>? =
try {
// Can pass a default country code as the second param if there is one or an empty string if not
val number = phoneNumberUtil.parse(phoneNumberWithCountryCode, "")
Pair(number.countryCode, number.nationalNumber)
} catch (e: NumberParseException) {
Timber.e(e, "Error getting country code and national number")
null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment