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

Add text watcher to dynamically change phone number input type #45

Merged
merged 1 commit into from
Nov 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}