diff --git a/README.md b/README.md index 581fe09..aafd50a 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ RNQRGenerator.generate({ bottom: 20, right: 20, }, + fileName: 'My Profile', // (optional), name of the image file to store in FileSystem. correctionLevel: 'L', // default is 'H', also available 'M' and 'Q'. }) .then(response => { diff --git a/android/src/main/java/com/gevorg/reactlibrary/RNQrGeneratorModule.java b/android/src/main/java/com/gevorg/reactlibrary/RNQrGeneratorModule.java index b235209..eaa022a 100644 --- a/android/src/main/java/com/gevorg/reactlibrary/RNQrGeneratorModule.java +++ b/android/src/main/java/com/gevorg/reactlibrary/RNQrGeneratorModule.java @@ -60,6 +60,7 @@ public String getName() { @ReactMethod public void generate(final ReadableMap options, @Nullable Callback failureCallback, @Nullable Callback successCallback) { String value = options.hasKey("value") ? options.getString("value") : ""; + String fileName = options.hasKey("fileName") ? options.getString("fileName") : null; String correctionLevel = options.hasKey("correctionLevel") ? options.getString("correctionLevel") : "H"; Double width = options.hasKey("width") ? options.getDouble("width") : 100; Double height = options.hasKey("height") ? options.getDouble("height") : 100; @@ -98,7 +99,7 @@ public void generate(final ReadableMap options, @Nullable Callback failureCallba try { File cacheDirectory = this.reactContext.getCacheDir(); - File imageFile = new File(getOutputFilePath(cacheDirectory, ".png")); + File imageFile = new File(getOutputFilePath(cacheDirectory, fileName, ".png")); imageFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(imageFile); @@ -233,10 +234,10 @@ public static File ensureDirExists(File dir) throws IOException { return dir; } - public static String getOutputFilePath(File directory, String extension) throws IOException { + public static String getOutputFilePath(File directory, String fileName, String extension) throws IOException { ensureDirExists(directory); - String filename = UUID.randomUUID().toString(); - return directory + File.separator + filename + extension; + String name = (fileName != null) ? fileName : UUID.randomUUID().toString(); + return directory + File.separator + name + extension; } public Bitmap getBitmapFromSource(String path, String base64) throws IOException { diff --git a/index.js b/index.js index 9c8f6c7..411ef57 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ export type QRCodeGenerateOptions = { height?: number, base64?: boolean, padding?: Padding, + fileName?: string, correctionLevel: 'L' | 'M' | 'Q' | 'H', }; diff --git a/ios/RNQrGenerator.m b/ios/RNQrGenerator.m index 1daa15e..1c73f41 100644 --- a/ios/RNQrGenerator.m +++ b/ios/RNQrGenerator.m @@ -23,6 +23,7 @@ - (dispatch_queue_t)methodQueue NSString *qrData = [RCTConvert NSString:options[@"value"]]; NSString *level = [RCTConvert NSString:options[@"correctionLevel"]]; + NSString *fileName = [RCTConvert NSString:options[@"fileName"]]; level = [self getCorrectionLevel:level]; float width = [RCTConvert float:options[@"width"]]; float height = [RCTConvert float:options[@"height"]]; @@ -88,7 +89,7 @@ - (dispatch_queue_t)methodQueue NSData *qrData = UIImagePNGRepresentation(image); NSString *directory = [[self cacheDirectoryPath] stringByAppendingPathComponent:@"QRCode"]; - NSString *path = [self generatePathInDirectory:directory withExtension:@".png"]; + NSString *path = [self generatePathInDirectory:directory fileName:fileName withExtension:@".png"]; response[@"uri"] = [self writeImage:qrData toPath:path]; response[@"width"] = @(image.size.width); @@ -162,9 +163,10 @@ - (dispatch_queue_t)methodQueue } } -- (NSString *)generatePathInDirectory:(NSString *)directory withExtension:(NSString *)extension +- (NSString *)generatePathInDirectory:(NSString *)directory fileName:(NSString *)name withExtension:(NSString *)extension { - NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingString:extension]; + NSString *fileName = name ? name : [[NSUUID UUID] UUIDString]; + fileName = [fileName stringByAppendingString:extension]; [self ensureDirExistsWithPath:directory]; return [directory stringByAppendingPathComponent:fileName]; } diff --git a/package.json b/package.json index 5b8aa7c..e6511ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rn-qr-generator", - "version": "1.1.2", + "version": "1.1.3", "description": "React native QR Code generator", "main": "index.js", "types": "typings/index.d.ts", diff --git a/typings/index.d.ts b/typings/index.d.ts index 38c98a9..83033fc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -18,6 +18,7 @@ export interface QRCodeGenerateOptions { height?: number; base64?: boolean; padding?: Padding; + fileName?: string, correctionLevel: 'L' | 'M' | 'Q' | 'H', };