forked from SubhamTyagi/openinwa
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add text watcher to dynamically change phone number input type
This changes the default inputType of the phone number input field to "phone" and adds a TextWatcher to observe for changes to the input field (either manual user edits or programmatic changes from extracting text out of intent launches or history item clicks). This allows users who share text with Launch Chat to edit the text (using the "text" soft keyboard) before clicking one of the action buttons, while also aiding users who directly launch Launch Chat in typing a phone number (using the "phone" soft keyboard)
- Loading branch information
1 parent
307d327
commit 5d20725
Showing
4 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/org/vinaygopinath/launchchat/helpers/TextHelper.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,10 @@ | ||
package org.vinaygopinath.launchchat.helpers | ||
|
||
object TextHelper { | ||
|
||
private val phoneNumberRegex = Regex("^[+]?[(]?[0-9]{1,4}[)]?[-\\s./0-9]*$") | ||
|
||
fun doesTextMatchPhoneNumberRegex(text: String): Boolean { | ||
return phoneNumberRegex.matches(text) | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
app/src/test/kotlin/org/vinaygopinath/launchchat/helpers/TextHelperPasswordRegexTest.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,52 @@ | ||
package org.vinaygopinath.launchchat.helpers | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
class TextHelperPasswordRegexTest { | ||
|
||
@Test | ||
fun `returns true when text is phone number with no formatting and no country code`() { | ||
val inputText = "111222333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isTrue() | ||
} | ||
|
||
@Test | ||
fun `returns true when text is phone number with no formatting and country code`() { | ||
val inputText = "+1111222333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isTrue() | ||
} | ||
|
||
|
||
@Test | ||
fun `returns true when text is phone number with space formatting`() { | ||
val inputText = "111 222 333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isTrue() | ||
} | ||
|
||
|
||
@Test | ||
fun `returns true when text is phone number with parentheses and space formatting`() { | ||
val inputText = "(111) 222 333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isTrue() | ||
} | ||
|
||
@Test | ||
fun `returns true when text is phone number with hyphen formatting`() { | ||
val inputText = "111-222-333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isTrue() | ||
} | ||
|
||
|
||
@Test | ||
fun `returns false when text includes non-numerical and non-formatting characters`() { | ||
val inputText = "Please contact us on this phone number: +1(111)-222 333" | ||
|
||
assertThat(TextHelper.doesTextMatchPhoneNumberRegex(inputText)).isFalse() | ||
} | ||
} |