Skip to content

Commit

Permalink
feat: Adds support for hidden input types (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhudec authored May 2, 2023
1 parent d075af3 commit 98b36f9
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 42 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [ master ]

env:
ORG_GRADLE_PROJECT_com.basistheory.android.example.apiUrl: https://api-dev.basistheory.com
ORG_GRADLE_PROJECT_com.basistheory.android.example.apiUrl: https://api.flock-dev.com
ORG_GRADLE_PROJECT_com.basistheory.android.example.apiKey: ${{ secrets.BASIS_THEORY_API_KEY }}

jobs:
Expand All @@ -24,7 +24,7 @@ jobs:
cache: 'gradle'

- name: Setup Gradle cache
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v2.4.2

- name: Build modules
run: ./gradlew build -x test
Expand All @@ -51,7 +51,7 @@ jobs:
cache: 'gradle'

- name: Setup Gradle cache
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v2.4.2

- name: Setup AVD cache
uses: actions/cache@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ class CollectCustomFormTests {
onView(withText("John Doe")).check(matches(isDisplayed()))
onView(withText("+1(234) 567-8900")).check(matches(isDisplayed()))
onView(withText("ABC-123")).check(matches(isDisplayed()))
onView(withText("secret password 123")).check(matches(isDisplayed()))
onView(withText("1234")).check(matches(isDisplayed()))
}

@Test
fun canTokenize() {
val name = Faker().name().fullName()
val phoneNumber = "2345678900"
val orderNumber = "ABC123"
val password = Faker().zelda().character()
val pin = Faker().number().randomNumber(4, true).toString()

// type values into elements
onView(withId(R.id.name)).perform(scrollTo(), typeText(name))
onView(withId(R.id.phoneNumber)).perform(scrollTo(), typeText(phoneNumber))
onView(withId(R.id.orderNumber)).perform(scrollTo(), typeText(orderNumber))
onView(withId(R.id.password)).perform(scrollTo(), typeText(password))
onView(withId(R.id.pin)).perform(scrollTo(), typeText(pin))

// click tokenize
onView(withId(R.id.tokenize_button)).perform(scrollTo(), click())
Expand All @@ -60,7 +66,9 @@ class CollectCustomFormTests {
allOf(
withSubstring(name),
withSubstring("+1(234) 567-8900"),
withSubstring("ABC-123")
withSubstring("ABC-123"),
withSubstring(password),
withSubstring(pin)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.fragment.app.viewModels
import com.basistheory.android.example.databinding.FragmentCustomFormBinding
import com.basistheory.android.example.util.tokenExpirationTimestamp
import com.basistheory.android.example.viewmodel.ApiViewModel
import com.basistheory.android.model.KeyboardType
import com.basistheory.android.model.InputType
import com.basistheory.android.view.mask.ElementMask

class CustomFormFragment : Fragment() {
Expand All @@ -28,8 +28,8 @@ class CustomFormFragment : Fragment() {
val digitRegex = Regex("""\d""")
val charRegex = Regex("""[A-Za-z]""")

// illustrates that keyboardType can be set programmatically (or in xml)
binding.phoneNumber.keyboardType = KeyboardType.NUMBER
// illustrates that inputType can be set programmatically (or in xml)
binding.phoneNumber.inputType = InputType.NUMBER
binding.phoneNumber.mask = ElementMask(
listOf(
"+",
Expand Down Expand Up @@ -65,6 +65,8 @@ class CustomFormFragment : Fragment() {
binding.name.setText("John Doe")
binding.phoneNumber.setText("2345678900")
binding.orderNumber.setText("ABC123")
binding.password.setText("secret password 123")
binding.pin.setText("1234")
}

private fun tokenize() = viewModel.tokenize(object {
Expand All @@ -73,6 +75,8 @@ class CustomFormFragment : Fragment() {
val name = binding.name
val phoneNumber = binding.phoneNumber
val orderNumber = binding.orderNumber
val password = binding.password
val pin = binding.pin
}
val expires_at = tokenExpirationTimestamp()
}).observe(viewLifecycleOwner) {}
Expand Down
29 changes: 27 additions & 2 deletions example/src/main/res/layout/fragment_custom_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,40 @@
android:background="@drawable/rounded_edit_text"
android:padding="5dp"
bt:hint="Order Number"
bt:keyboardType="text"
bt:inputType="text"
bt:removeDefaultStyles="true"
bt:textColor="@color/gray_800" />

<com.basistheory.android.view.TextElement
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/rounded_edit_text"
android:padding="5dp"
bt:hint="Password"
bt:inputType="textPassword"
bt:removeDefaultStyles="true"
bt:textColor="@color/gray_800" />

<com.basistheory.android.view.TextElement
android:id="@+id/pin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/rounded_edit_text"
android:padding="5dp"
bt:hint="PIN"
bt:inputType="numberPassword"
bt:removeDefaultStyles="true"
bt:textColor="@color/gray_800" />


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal"
android:layout_marginBottom="50dp">

<Button
android:id="@+id/tokenize_button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
android:inputType="number"
android:padding="5dp"
bt:hint="Social Security Number"
bt:keyboardType="number"
bt:inputType="number"
bt:mask="###-##-####"
bt:removeDefaultStyles="true"
bt:textColor="@color/gray_800" />
Expand Down
10 changes: 10 additions & 0 deletions lib/src/main/java/com/basistheory/android/model/InputType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.basistheory.android.model

import android.text.InputType as AndroidInputType

enum class InputType(val androidInputType: Int, val isConcealed: Boolean = false) {
TEXT(AndroidInputType.TYPE_CLASS_TEXT),
NUMBER(AndroidInputType.TYPE_CLASS_NUMBER),
TEXT_PASSWORD(AndroidInputType.TYPE_TEXT_VARIATION_PASSWORD, true),
NUMBER_PASSWORD(AndroidInputType.TYPE_CLASS_NUMBER, true);
}
14 changes: 0 additions & 14 deletions lib/src/main/java/com/basistheory/android/model/KeyboardType.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.basistheory.android.view
import android.content.Context
import android.util.AttributeSet
import com.basistheory.android.model.ElementValueReference
import com.basistheory.android.model.KeyboardType
import com.basistheory.android.model.InputType
import com.basistheory.android.view.mask.ElementMask
import com.basistheory.android.view.validation.FutureDateValidator

Expand All @@ -28,7 +28,7 @@ class CardExpirationDateElement @JvmOverloads constructor(
}

init {
super.keyboardType = KeyboardType.NUMBER
super.inputType = InputType.NUMBER
super.mask = defaultMask
super.validator = FutureDateValidator()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.util.AttributeSet
import com.basistheory.android.event.ChangeEvent
import com.basistheory.android.event.EventDetails
import com.basistheory.android.model.CardMetadata
import com.basistheory.android.model.KeyboardType
import com.basistheory.android.model.InputType
import com.basistheory.android.service.CardBrandEnricher
import com.basistheory.android.view.mask.ElementMask
import com.basistheory.android.view.transform.RegexReplaceElementTransform
Expand All @@ -20,7 +20,7 @@ class CardNumberElement @JvmOverloads constructor(
private val cardBrandEnricher: CardBrandEnricher = CardBrandEnricher()

init {
super.keyboardType = KeyboardType.NUMBER
super.inputType = InputType.NUMBER
super.mask = defaultMask
super.transform = RegexReplaceElementTransform(Regex("""\s"""), "")
super.validator = LuhnValidator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.basistheory.android.view

import android.content.Context
import android.util.AttributeSet
import com.basistheory.android.model.KeyboardType
import com.basistheory.android.model.InputType
import com.basistheory.android.view.mask.ElementMask
import com.basistheory.android.view.validation.RegexValidator

Expand All @@ -24,7 +24,7 @@ class CardVerificationCodeElement @JvmOverloads constructor(
}

init {
super.keyboardType = KeyboardType.NUMBER
super.inputType = InputType.NUMBER
super.mask = defaultMask
super.validator = validatorForLength(defaultMask.validLengths.max())
}
Expand Down
21 changes: 12 additions & 9 deletions lib/src/main/java/com/basistheory/android/view/TextElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import com.basistheory.android.event.ElementEventListeners
import com.basistheory.android.event.FocusEvent
import com.basistheory.android.model.ElementValueReference
import com.basistheory.android.model.InputAction
import com.basistheory.android.model.KeyboardType
import com.basistheory.android.model.InputType
import com.basistheory.android.view.mask.ElementMask
import com.basistheory.android.view.method.FullyHiddenTransformationMethod
import com.basistheory.android.view.transform.ElementTransform
import com.basistheory.android.view.validation.ElementValidator

Expand All @@ -40,6 +41,7 @@ open class TextElement @JvmOverloads constructor(
private var _isValid: Boolean = true
private var _isMaskSatisfied: Boolean = true
private var _isEmpty: Boolean = true
private var _inputType: InputType = InputType.TEXT

internal var inputAction: InputAction = InputAction.INSERT

Expand All @@ -61,11 +63,8 @@ open class TextElement @JvmOverloads constructor(

hint = getString(R.styleable.TextElement_hint)

keyboardType = KeyboardType.fromInt(
getInt(
R.styleable.TextElement_keyboardType,
KeyboardType.TEXT.inputType
)
inputType = InputType.values().elementAt(
getInt(R.styleable.TextElement_inputType, 0)
)

mask = getString(R.styleable.TextElement_mask)?.let { ElementMask(it) }
Expand Down Expand Up @@ -166,10 +165,14 @@ open class TextElement @JvmOverloads constructor(
_editText.hint = value
}

var keyboardType: KeyboardType
get() = KeyboardType.fromInt(_editText.inputType)
var inputType: InputType
get() = _inputType
set(value) {
_editText.inputType = value.inputType
_inputType = value
_editText.inputType = value.androidInputType

if (value.isConcealed)
_editText.transformationMethod = FullyHiddenTransformationMethod()
}

var removeDefaultStyles: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.basistheory.android.view.method

import android.text.method.PasswordTransformationMethod
import android.view.View

/**
* Customizes PasswordTransformationMethod to also hide the current character while typing
*/
internal class FullyHiddenTransformationMethod : PasswordTransformationMethod() {

override fun getTransformation(charSequence: CharSequence, view: View): CharSequence {
return FullyHiddenCharSequence(charSequence)
}

private inner class FullyHiddenCharSequence(private val charSequence: CharSequence) : CharSequence {
private val dot = '\u2022'

override val length: Int
get() = charSequence.length

override fun get(index: Int): Char = dot

override fun subSequence(startIndex: Int, endIndex: Int): CharSequence =
FullyHiddenCharSequence(charSequence.subSequence(startIndex, endIndex))
}
}
8 changes: 5 additions & 3 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
<declare-styleable name="TextElement">
<attr name="editable" format="boolean" />
<attr name="hint" format="string" />
<attr name="keyboardType" format="enum">
<enum name="text" value="1" />
<enum name="number" value="2" />
<attr name="inputType" format="enum">
<enum name="text" value="0" />
<enum name="number" value="1" />
<enum name="textPassword" value="2" />
<enum name="numberPassword" value="3" />
</attr>
<attr name="mask" format="string" />
<attr name="removeDefaultStyles" format="boolean" />
Expand Down

0 comments on commit 98b36f9

Please sign in to comment.