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

listening to change events for android #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Text,
View,
TouchableWithoutFeedback,
NativeEventEmitter,
} from 'react-native';

interface SsnTextElementProps {
Expand All @@ -32,8 +33,21 @@ const tokenize = () => SsnTextElementModule.tokenize() as Promise<string>;

const dismissSsnKeyboard = () => SsnTextElementModule.dismissKeyboard() as void;

const eventEmitter = new NativeEventEmitter(NativeModules.SsnTextElementModule);

function App(): JSX.Element {
const [text, setText] = useState<string>();
const [isValid, setIsValid] = useState<boolean>();
const [isComplete, setIsComplete] = useState<boolean>();
const [isMaskSatisified, setIsMaskSatisfied] = useState<boolean>();
const [isEmpty, setIsEmpty] = useState<boolean>(true);

eventEmitter.addListener('change_event', params => {
setIsValid(params.isValid);
setIsComplete(params.isComplete);
setIsMaskSatisfied(params.isMaskSatisfied);
setIsEmpty(params.isEmpty);
});

return (
<SafeAreaView style={styles.view}>
Expand All @@ -54,6 +68,18 @@ function App(): JSX.Element {
}}>
<Text style={styles.tokenizeText}>{'Tokenize'}</Text>
</Pressable>
<Text style={styles.elementEventText}>{`SSN is ${
isValid ? 'valid' : 'invalid'
}`}</Text>
<Text style={styles.elementEventText}>{`SSN is ${
isComplete ? 'complete' : 'incomplete'
}`}</Text>
<Text style={styles.elementEventText}>{`SSN mask is ${
isMaskSatisified ? 'satisfied' : 'unsatisfied'
}`}</Text>
<Text style={styles.elementEventText}>{`SSN is ${
isEmpty ? 'empty' : 'not empty'
}`}</Text>
<Text style={styles.tokenText}>{text}</Text>
</View>
</TouchableWithoutFeedback>
Expand Down Expand Up @@ -95,6 +121,11 @@ const styles = StyleSheet.create({
fontWeight: 'bold',
color: 'white',
},
elementEventText: {
fontWeight: 'bold',
marginVertical: 5,
marginLeft: 10,
},
tokenText: {
marginTop: 25,
},
Expand Down
29 changes: 20 additions & 9 deletions android/app/src/main/java/com/awesomeproject/SsnTextElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import com.basistheory.android.model.KeyboardType
import com.basistheory.android.service.BasisTheoryElements
import com.basistheory.android.view.TextElement
import com.basistheory.android.view.mask.ElementMask
import com.basistheory.android.view.validation.RegexValidator
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Arguments;
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.google.gson.GsonBuilder
import kotlinx.coroutines.*

class SsnTextElement(private val context: ReactApplicationContext) : SimpleViewManager<TextElement?>() {
class SsnTextElement(private val reactAppContext: ReactApplicationContext) : SimpleViewManager<TextElement?>() {
private val apiKey = "your_api_key"
private val bt = BasisTheoryElements.builder().apiKey(apiKey).build()
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
Expand All @@ -30,12 +33,20 @@ class SsnTextElement(private val context: ReactApplicationContext) : SimpleViewM
ssnTextElement.hint = "Enter SSN"
ssnTextElement.keyboardType = KeyboardType.NUMBER
ssnTextElement.mask = ElementMask("###-##-####")
ssnTextElement.validator = RegexValidator(regex = Regex("^\\d{3}-\\d{2}-\\d{4}$"))
ssnTextElement.background =
AppCompatResources.getDrawable(context, R.drawable.rounded_edit_text)
ssnTextElement.textColor = Color.BLACK

ssnTextElement.addChangeEventListener {
println(it)
ssnTextElement.addChangeEventListener { it ->
val params = Arguments.createMap();
params.putBoolean("isValid", it.isValid)
params.putBoolean("isComplete", it.isComplete)
params.putBoolean("isMaskSatisfied", it.isMaskSatisfied)
params.putBoolean("isEmpty", it.isEmpty)

reactAppContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("change_event", params)
}

ssnTextElement.addBlurEventListener {
Expand All @@ -45,6 +56,10 @@ class SsnTextElement(private val context: ReactApplicationContext) : SimpleViewM
return ssnTextElement
}

override fun getName(): String {
return "SsnTextElement"
}

@RequiresApi(Build.VERSION_CODES.O)
@ReactMethod
fun tokenize(promise: Promise) {
Expand All @@ -68,11 +83,7 @@ class SsnTextElement(private val context: ReactApplicationContext) : SimpleViewM

@ReactMethod
fun dismissKeyboard() {
val inputMethodManager = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager?.hideSoftInputFromWindow(ssnTextElement.windowToken, 0)
}

override fun getName(): String {
return "SsnTextElement"
val inputMethodManager = reactAppContext.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(ssnTextElement.windowToken, 0)
}
}