Skip to content

Commit

Permalink
Merge branch 'master' into kingst/await_ready
Browse files Browse the repository at this point in the history
  • Loading branch information
kingst-stripe authored Oct 13, 2021
2 parents 8400c8d + 7f7a919 commit 002b311
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.facebook.react:react-native:+' // From node_modules

implementation "com.getbouncer:cardverify-ui:2.1.0011"
implementation "com.getbouncer:cardverify-ui:2.1.0015"
}

def configureReactNativePom(def pom) {
Expand Down
57 changes: 53 additions & 4 deletions android/src/main/java/com/getbouncer/RNCardVerifyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.getbouncer.cardverify.ui.network.CardVerifyActivity;
Expand All @@ -18,6 +19,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class RNCardVerifyModule extends ReactContextBaseJavaModule {
private static final int SCAN_REQUEST_CODE = 51235;

Expand All @@ -32,11 +36,13 @@ public class RNCardVerifyModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;

private Promise scanPromise;
private Promise modelsDownloadPromise;
private boolean modelsDownloaded = false;

@Override
public void initialize() {
if (!deferModelDownloads) {
downloadModels();
downloadModels(null);
}
}

Expand Down Expand Up @@ -306,14 +312,57 @@ public void isSupportedAsync(Promise promise) {
}

@ReactMethod
public void downloadModels() {
public void awaitReady(@NotNull final Promise promise) {
// Force this to the UI thread to avoid a race condition with [handleModelsDownloaded]
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
if (modelsDownloaded) {
promise.resolve(true);
} else {
modelsDownloadPromise = promise;
}
}
});
}

@ReactMethod
public void downloadModels(@Nullable final Promise promise) {
if (useLocalVerificationOnly) {
com.getbouncer.cardverify.ui.local.CardVerifyActivity.warmUp(this.reactContext.getApplicationContext(), apiKey, enableExpiryExtraction || enableNameExtraction, false);
com.getbouncer.cardverify.ui.local.CardVerifyActivity.prepareScan(this.reactContext.getApplicationContext(), apiKey, enableExpiryExtraction || enableNameExtraction, false, new Function0<Unit>() {
@Override
public Unit invoke() {
handleModelsDownloaded(promise);
return Unit.INSTANCE;
}
});
} else {
CardVerifyActivity.warmUp(this.reactContext.getApplicationContext(), apiKey, enableExpiryExtraction || enableNameExtraction, false);
CardVerifyActivity.prepareScan(this.reactContext.getApplicationContext(), apiKey, enableExpiryExtraction || enableNameExtraction, false, new Function0<Unit>() {
@Override
public Unit invoke() {
handleModelsDownloaded(promise);
return Unit.INSTANCE;
}
});
}
}

private void handleModelsDownloaded(@Nullable final Promise promise) {
// Force this to the UI thread to avoid a race condition with [awaitReady]
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
modelsDownloaded = true;
if (promise != null) {
promise.resolve(true);
}
if (modelsDownloadPromise != null) {
modelsDownloadPromise.resolve(true);
}
}
});
}

@ReactMethod
public void scan(@Nullable String requiredCardIin, @Nullable String requiredCardLastFour, Boolean skipVerificationOnDownloadFailure, @NotNull Promise promise) {
scanPromise = promise;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public void onCreate() {
RNCardVerifyModule.apiKey = "<your_api_key_here>";
RNCardVerifyModule.enableNameExtraction = true;
RNCardVerifyModule.enableExpiryExtraction = true;
RNCardVerifyModule.deferModelDownloads = false;
RNCardVerifyModule.deferModelDownloads = true;
}
}

0 comments on commit 002b311

Please sign in to comment.