Skip to content

Commit

Permalink
Add text watcher to dynamically change phone number input type
Browse files Browse the repository at this point in the history
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
vinaygopinath committed Oct 28, 2024
1 parent 307d327 commit 5d20725
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.vinaygopinath.launchchat.screens.main
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.view.Menu
import android.view.MenuItem
import android.widget.ArrayAdapter
Expand All @@ -15,6 +16,7 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -32,6 +34,7 @@ import org.vinaygopinath.launchchat.helpers.ClipboardHelper
import org.vinaygopinath.launchchat.helpers.DetailedActivityHelper
import org.vinaygopinath.launchchat.helpers.IntentHelper
import org.vinaygopinath.launchchat.helpers.PhoneNumberHelper
import org.vinaygopinath.launchchat.helpers.TextHelper
import org.vinaygopinath.launchchat.models.Action
import org.vinaygopinath.launchchat.models.Activity
import org.vinaygopinath.launchchat.models.DetailedActivity
Expand Down Expand Up @@ -96,6 +99,15 @@ class MainActivity : AppCompatActivity() {
messageInput = findViewById(R.id.message_input)
historyTitle = findViewById(R.id.history_title)
historyListView = findViewById(R.id.history_list)
phoneNumberInput.addTextChangedListener(
afterTextChanged = {
if (phoneNumberInput.isFocused) {
return@addTextChangedListener
}

updatePhoneNumberInputType()
}
)
with(historyListView) {
val linearLayoutManager = LinearLayoutManager(this@MainActivity)
layoutManager = linearLayoutManager
Expand Down Expand Up @@ -154,7 +166,10 @@ class MainActivity : AppCompatActivity() {
repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
viewModel.uiState.collect { uiState ->
uiState.extractedContent?.let { handleExtractedContent(it) }
uiState.extractedContent?.let {
handleExtractedContent(it)
updatePhoneNumberInputType()
}
}
}
launch {
Expand Down Expand Up @@ -280,4 +295,15 @@ class MainActivity : AppCompatActivity() {
private fun showHistory(activity: Activity) {
viewModel.logActivityFromHistory(activity)
}

private fun updatePhoneNumberInputType() {
val inputText = phoneNumberInput.text.toString()
val newInputType =
if (inputText.isBlank() || TextHelper.doesTextMatchPhoneNumberRegex(inputText)) {
InputType.TYPE_CLASS_PHONE
} else {
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
}
phoneNumberInput.inputType = newInputType
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="phone"
android:inputType="textMultiLine"
android:inputType="phone"
android:minLines="3" />
</com.google.android.material.textfield.TextInputLayout>

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

0 comments on commit 5d20725

Please sign in to comment.