Skip to content

Commit

Permalink
Fix programmatically initial text not formatted
Browse files Browse the repository at this point in the history
Now when we start watching to sync the number with the flag, we first check if there’s already a valid number in the input. If there is, we ignore the initial country code and go with whatever is there — this case will happen when a differnet input set programmatically (e.g. databinding) differs from the initial input.
  • Loading branch information
tfcporciuncula committed Jan 25, 2021
1 parent 72ce24c commit d44ce08
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PhonemojiTest {
verifyInitialRegionCodeAttribute()
verifyInitialCountryCodeAttribute()
verifyFlagText()
verifyInitialInputIsFormatted()
verifyTextInputLayoutIconPresence(scenario)
}

Expand Down Expand Up @@ -87,6 +88,11 @@ class PhonemojiTest {
onView(withId(R.id.flagTextView)).check(matches(withText("🇧🇷")))
}

private fun verifyInitialInputIsFormatted() {
onView(withId(R.id.editTextWithInitialInput)).check(matches(withText("+49 176")))
onView(withId(R.id.flagTextViewForEditTextWithInitialInput)).check(matches(withText("🇩🇪")))
}

private fun verifyTextInputLayoutIconPresence(scenario: ActivityScenario<TestActivity>) {
scenario.onActivity {
assertThat(it.findViewById<TextInputLayout>(R.id.textInputLayout).startIconDrawable).isNotNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.tfcporciuncula.phonemoji.tests

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.tfcporciuncula.phonemoji.PhonemojiTextInputEditText

class TestActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.test_activity)
findViewById<PhonemojiTextInputEditText>(R.id.editTextWithInitialInput).setInternationalPhoneNumber("49176")
}
}
25 changes: 25 additions & 0 deletions phonemoji-tests/src/main/res/layout/test_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,29 @@

</com.tfcporciuncula.phonemoji.PhonemojiTextInputLayout>

<com.tfcporciuncula.phonemoji.PhonemojiTextInputLayout
android:id="@+id/textInputLayoutWithInitialInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="32dp"
>

<com.tfcporciuncula.phonemoji.PhonemojiTextInputEditText
android:id="@+id/editTextWithInitialInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="32dp"
app:phonemoji_initialCountryCode="41"
/>

</com.tfcporciuncula.phonemoji.PhonemojiTextInputLayout>

<com.tfcporciuncula.phonemoji.PhonemojiFlagTextView
android:id="@+id/flagTextViewForEditTextWithInitialInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="32dp"
app:phonemoji_flagFor="@id/editTextWithInitialInput"
/>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ object PhonemojiHelper {
*/
fun watchPhoneNumber(editText: PhonemojiTextInputEditText, onCountryChanged: (String) -> Unit) {
var currentCountryCode = editText.initialCountryCode
runCatching {
currentCountryCode = phoneNumberUtil.parse(editText.text, null).countryCode
}

onCountryChanged(regionCodeToEmoji(phoneNumberUtil.getRegionCodeForCountryCode(currentCountryCode)))

editText.addTextChangedListener(
Expand Down

0 comments on commit d44ce08

Please sign in to comment.