-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to help with formatting of phone numbers and addresses
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
library/src/main/java/com/alexstyl/contactstore/FormattedStrings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |