Skip to content

Instantly share code, notes, and snippets.

@lithin
Created April 17, 2019 06:27
Show Gist options
  • Save lithin/948208ffe0e328c01a9719843645f07b to your computer and use it in GitHub Desktop.
Save lithin/948208ffe0e328c01a9719843645f07b to your computer and use it in GitHub Desktop.
Lazy loading native modules
componentWillMount = () => {
const { default: BiometryScanner } = require('react-native-fingerprint-scanner');
BiometryScanner.isSensorAvailable()
.then(biometryType => {
return this.setState({
biometryType,
});
})
.catch(() => {
return this.setState({
biometryType: null,
});
});
};
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new ArrayList<>(Arrays.asList(
new MainReactPackage(),
));
packages.add(new LazyReactPackage() {
@Override
protected List<ModuleSpec> getNativeModules(ReactApplicationContext reactContext) {
return Arrays.asList(
ModuleSpec.nativeModuleSpec(
ReactNativeFingerprintScannerModule.class,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactNativeFingerprintScannerModule(reactContext);
}
}));
}
@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return new ReactModuleInfoProvider() {
@Override
public Map<String, ReactModuleInfo> getReactModuleInfos() {
Map reactModuleInfoMap = new HashMap();
reactModuleInfoMap.put("ReactNativeFingerprintScanner", new ReactModuleInfo("ReactNativeFingerprintScanner", "com.hieuvp.fingerprint.ReactNativeFingerprintScannerModule", false, false, false, false, false));
return reactModuleInfoMap;
}
};
}
});
// packages.add(new TurboReactPackage() {
//
// @Override
// public NativeModule getModule(String name, ReactApplicationContext reactContext) {
// switch (name) {
// case "ReactNativeFingerprintScanner":
// return new ReactNativeFingerprintScannerModule(reactContext);
// default:
// return null;
//
// }
// }
//
// @Override
// public ReactModuleInfoProvider getReactModuleInfoProvider() {
// return new ReactModuleInfoProvider() {
// @Override
// public Map<String, ReactModuleInfo> getReactModuleInfos() {
// Map reactModuleInfoMap = new HashMap();
// reactModuleInfoMap.put("ReactNativeFingerprintScanner", new ReactModuleInfo("ReactNativeFingerprintScanner", "com.hieuvp.fingerprint.ReactNativeFingerprintScannerModule", false, false, false, false, false));
// return reactModuleInfoMap;
// }
// };
// }
// });
return packages;
}
@ReactModule(name="ReactNativeFingerprintScanner")
public class ReactNativeFingerprintScannerModule extends ReactContextBaseJavaModule
implements LifecycleEventListener {
public static final int MAX_AVAILABLE_TIMES = Integer.MAX_VALUE;
private final ReactApplicationContext mReactContext;
private FingerprintIdentify mFingerprintIdentify;
public ReactNativeFingerprintScannerModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactContext = reactContext;
ReactMarker.logMarker("FINGERPRINT_START");
}
@Override
public String getName() {
return "ReactNativeFingerprintScanner";
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment