Skip to content

Commit

Permalink
Add functions to help with formatting of phone numbers and addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstyl committed Feb 1, 2022
1 parent 4f12325 commit 8987af1
Showing 1 changed file with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.alexstyl.contactstore

import android.os.Build
import android.telephony.PhoneNumberUtils
import java.util.Locale

/**
* Formats the phone number according to the device selected [Locale][java.util.Locale].
*
* Returns the formatted phone number or the original phone number if the phone number is considered
* invalid in terms of formatting.
*/
public val PhoneNumber.formattedNumber: String
get() {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val country = Locale.getDefault().country
PhoneNumberUtils.formatNumber(raw, country)
.orEmpty()
.ifBlank { raw }
} else {
@Suppress("DEPRECATION")
PhoneNumberUtils.formatNumber(raw)
.orEmpty()
.ifBlank { raw }
}
}

/**
* The full postal address returned in a single lined [String], separated by commas.
* i.e:
*
* _94 Byrd Lane, Arch, NM, New Mexico, 88130_
*/
public val PostalAddress.unstructuredPostalAddress: String
get() {
return buildString {
appendIfPresent(street)
appendIfPresent(poBox)
appendIfPresent(neighborhood)
appendIfPresent(city)
appendIfPresent(region)
appendIfPresent(postCode)
appendIfPresent(country)
}
}

private fun StringBuilder.appendIfPresent(thing: String) {
if (thing.isNotEmpty()) {
if (isNotEmpty()) {
append(", ")
}
append(thing)
}
}

/**
* The formatted version of the address, split by new lines.
*
* i.e:
*
* _94 Byrd Lane,_
*
* _Arch,_
*
* _NM,_
*
* _New Mexico,_
*
* _88130_
*/
public val PostalAddress.formattedPostalAddress: String
get() {
return buildString {
appendLineIfPresent(street)
appendLineIfPresent(poBox)
appendLineIfPresent(neighborhood)
appendLineIfPresent(city)
appendLineIfPresent(region)
appendLineIfPresent(postCode)
appendLineIfPresent(country)
}
}

private fun StringBuilder.appendLineIfPresent(thing: String) {
if (thing.isNotEmpty()) {
if (isNotEmpty()) {
appendLine(", ")
}
append(thing)
}
}

0 comments on commit 8987af1

Please sign in to comment.