Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatted strings #42

Merged
merged 2 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ repositories {
}

dependencies {
implementation 'com.alexstyl:contactstore:0.11.0'
implementation 'com.alexstyl:contactstore:0.11.1'

// optional dependency for tests
testImplementation 'com.alexstyl:contactstore-test:0.11.0'
testImplementation 'com.alexstyl:contactstore-test:0.11.1'
}
```

Expand Down
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)
}
}