Skip to content

Commit

Permalink
update country code format
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Oct 1, 2020
1 parent 9a1f9b2 commit a8d04ec
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 19 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,54 @@ To receive country name or iso2 code from given **country code**
val country = phoneNumberKit.getCountry(90)
```

## Usage with Custom Item Layout

Add your custom item layout resource as a parameter
```kotlin
phoneNumberKit.setupCountryPicker(this, R.layout.my_item_layout)
```
You need to use below view ids in your layout file
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingVertical="12dp"
android:paddingHorizontal="18dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">

<ImageView
android:id="@+id/imageViewFlag"
android:layout_width="22dp"
android:layout_height="22dp" />

<TextView
android:id="@+id/textViewName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"
android:textSize="16sp"
android:textColor="#232425" />

<TextView
android:id="@+id/textViewCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#838383" />

</LinearLayout>
```


## Demo
<table>
<tr>
Expand Down Expand Up @@ -86,7 +134,7 @@ allprojects {
Step 2. Add the dependency
```
dependencies {
implementation 'com.github.ibrahimsn98:PhoneNumberKit:1.3'
implementation 'com.github.ibrahimsn98:PhoneNumberKit:1.4'
}
```

Expand All @@ -102,7 +150,6 @@ dependencies {
- This library is based on Google's lilPhoneNumber library (https://github.com/google/libphonenumber)
- Inspired from PhoneNumberKit Swift library by [marmelloy](https://github.com/marmelroy) (https://github.com/marmelroy/PhoneNumberKit)
- Flag images from [region-flags](https://github.com/behdad/region-flags)
- Original country data from mledoze's [World countries in JSON, CSV and XML](https://github.com/mledoze/countries)

## License
PhoneNumberKit is available under the Apache license. See the [LICENSE](https://github.com/ibrahimsn98/PhoneNumberKit/blob/master/LICENSE) file for more info.
Expand Down
36 changes: 24 additions & 12 deletions lib/src/main/java/me/ibrahimsn/lib/PhoneNumberKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import me.ibrahimsn.lib.Constants.KEY_DIGIT
import me.ibrahimsn.lib.Constants.KEY_SPACE
import me.ibrahimsn.lib.bottomsheet.CountryPickerBottomSheet
import me.ibrahimsn.lib.core.Core
import me.ibrahimsn.lib.util.PhoneNumberTextWatcher
import me.ibrahimsn.lib.util.prependPlus
import me.ibrahimsn.lib.util.*

class PhoneNumberKit(private val context: Context) {

Expand All @@ -34,16 +33,18 @@ class PhoneNumberKit(private val context: Context) {
get() = input?.editText?.text
set(value) {
input?.tag = Constants.VIEW_TAG
input?.editText?.setText("")
input?.editText?.clear()
input?.editText?.append(value)
input?.tag = null
}

private val textWatcher = object: PhoneNumberTextWatcher() {

override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {
if (input?.tag != Constants.VIEW_TAG) {
val parsedNumber = core.parsePhoneNumber(rawInput.toString(), country?.iso2)
val parsedNumber = core.parsePhoneNumber(
rawInput.toString().clearSpaces(),
country?.iso2
)

// Update country flag and mask if detected as a different one
if (country == null || country?.countryCode != parsedNumber?.countryCode) {
Expand All @@ -61,44 +62,54 @@ class PhoneNumberKit(private val context: Context) {

private fun applyFormat() {
rawInput?.let { raw ->
// Clear all of the non-digit characters from the phone number
val pureNumber = raw.filter { i -> i.isDigit() }.toMutableList()

// Add plus to beginning of the number
pureNumber.add(0, CHAR_PLUS)

for (i in format.indices) {
if (pureNumber.size > i) {
// Put required format spaces
if (format[i] == KEY_SPACE && pureNumber[i] != CHAR_SPACE) {
pureNumber.add(i, CHAR_SPACE)
continue
}

// Put required format dashes
if (format[i] == KEY_DASH && pureNumber[i] != CHAR_DASH) {
pureNumber.add(i, CHAR_DASH)
continue
}
}
}

rawInput = pureNumber.joinToString("")
rawInput = pureNumber.toRawString()
}
}

private fun setCountry(country: Country?, isManual: Boolean = false) {
country?.let {
this.country = country

// Clear input if a country code selected manually
if (isManual) {
rawInput = country.countryCode.prependPlus()
hasManualCountry = true
}

// Setup country icon
getFlagIcon(country.iso2)?.let { icon ->
input?.startIconDrawable = icon
}

// Set text length limit according to the example phone number
core.getExampleNumber(country.iso2)?.let { example ->

if (isManual) {
hasManualCountry = true
rawInput = if (country.countryCode != example.countryCode) {
example.countryCode.prependPlus() + country.countryCode
} else {
country.countryCode.prependPlus()
}
}
}

core.formatPhoneNumber(core.getExampleNumber(country.iso2))?.let { number ->
input?.editText?.filters = arrayOf(InputFilter.LengthFilter(number.length))
format = createNumberFormat(number)
Expand All @@ -107,6 +118,7 @@ class PhoneNumberKit(private val context: Context) {
}
}

// Creates a pattern like +90 506 555 55 55 -> +0010001000100100
private fun createNumberFormat(number: String): String {
var format = number.replace("(\\d)".toRegex(), KEY_DIGIT.toString())
format = format.replace("(\\s)".toRegex(), KEY_SPACE.toString())
Expand Down
3 changes: 1 addition & 2 deletions lib/src/main/java/me/ibrahimsn/lib/core/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ class Core {
null
}
}

}
}
21 changes: 18 additions & 3 deletions lib/src/main/java/me/ibrahimsn/lib/util/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package me.ibrahimsn.lib.util

import android.widget.EditText
import me.ibrahimsn.lib.Constants.CHAR_PLUS

fun CharSequence?.prependPlus(): String {
return StringBuilder()
.append("+")
.append(CHAR_PLUS)
.append(this)
.toString()
}

fun Int.prependPlus(): String {
return StringBuilder()
.append("+")
.append(CHAR_PLUS)
.append(this)
.toString()
}

fun CharSequence?.startsWithPlus(): Boolean {
return this?.startsWith("+") == true
return this?.startsWith(CHAR_PLUS) == true
}

fun String?.clearSpaces(): String? {
return this?.replace("\\s+", "")
}

fun <T> Collection<T>.toRawString(): String {
return this.joinToString("")
}

fun EditText.clear() {
this.setText("")
}

0 comments on commit a8d04ec

Please sign in to comment.