From 6d6f2d1fb026629e7c391c14b2868918a77f5667 Mon Sep 17 00:00:00 2001 From: jameswilddev Date: Mon, 22 Jul 2024 15:40:41 +0100 Subject: [PATCH] Picture quality is now customizable. --- react-native/services/PictureHelper/index.tsx | 11 ++-- react-native/services/PictureHelper/readme.md | 2 +- react-native/services/PictureHelper/unit.tsx | 52 +++++++++---------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/react-native/services/PictureHelper/index.tsx b/react-native/services/PictureHelper/index.tsx index d319acde..897cba56 100644 --- a/react-native/services/PictureHelper/index.tsx +++ b/react-native/services/PictureHelper/index.tsx @@ -12,10 +12,13 @@ export class PictureHelper implements PictureHelperInterface { * @param fileStore The file store in which pictures are to be stored. * @param permissionHelper The permission helper which will be used when * acquiring pictures. + * @param quality The compression quality to use, where 0 is minimum + * and 1 is maximum. */ constructor ( private readonly fileStore: FileStoreInterface, - private readonly permissionHelper: PermissionHelper + private readonly permissionHelper: PermissionHelper, + private readonly quality: number ) {} async takePicture ( @@ -30,7 +33,7 @@ export class PictureHelper implements PictureHelperInterface { async () => { const result = await ImagePicker.launchCameraAsync({ allowsEditing: true, - quality: 0.7, + quality: this.quality, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -70,7 +73,7 @@ export class PictureHelper implements PictureHelperInterface { async () => { const result = await ImagePicker.launchImageLibraryAsync({ allowsEditing: true, - quality: 0.7, + quality: this.quality, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -103,7 +106,7 @@ export class PictureHelper implements PictureHelperInterface { async () => { const result = await ImagePicker.launchImageLibraryAsync({ allowsEditing: true, - quality: 0.7, + quality: this.quality, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, diff --git a/react-native/services/PictureHelper/readme.md b/react-native/services/PictureHelper/readme.md index 3b28caab..7dab339e 100644 --- a/react-native/services/PictureHelper/readme.md +++ b/react-native/services/PictureHelper/readme.md @@ -7,7 +7,7 @@ Provides helpers for working with pictures. ```tsx import type { PictureHelper } from "react-native-app-helpers"; -const pictureHelper = new PictureHelper(fileStore, permissionHelper); +const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.7); // Do NOT save to media library. await permissionHelper.takePicture( diff --git a/react-native/services/PictureHelper/unit.tsx b/react-native/services/PictureHelper/unit.tsx index 4721be93..ed887b22 100644 --- a/react-native/services/PictureHelper/unit.tsx +++ b/react-native/services/PictureHelper/unit.tsx @@ -27,7 +27,7 @@ test('take picture without saving to media library permission denied', async () const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( false, @@ -78,7 +78,7 @@ test('take picture saving to media library permission denied', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( true, @@ -133,7 +133,7 @@ test('take picture without saving to media library cancelled', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( false, @@ -149,7 +149,7 @@ test('take picture without saving to media library cancelled', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -196,7 +196,7 @@ test('take picture without saving to media library none selected', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( false, @@ -212,7 +212,7 @@ test('take picture without saving to media library none selected', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -258,7 +258,7 @@ test('take picture saving to media library cancelled', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( true, @@ -274,7 +274,7 @@ test('take picture saving to media library cancelled', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -321,7 +321,7 @@ test('take picture saving to media library none selected', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( true, @@ -337,7 +337,7 @@ test('take picture saving to media library none selected', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -384,7 +384,7 @@ test('take picture without saving to media library successful', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( false, @@ -403,7 +403,7 @@ test('take picture without saving to media library successful', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -460,7 +460,7 @@ test('take picture saving to media library successful', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.takePicture( true, @@ -479,7 +479,7 @@ test('take picture saving to media library successful', async () => { expect(ImagePicker.launchCameraAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchCameraAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -527,7 +527,7 @@ test('select one picture from media library permission denied', async () => { const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectOnePictureFromMediaLibrary( onPermissionDenied, @@ -581,7 +581,7 @@ test('select one picture from media library cancelled', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectOnePictureFromMediaLibrary( onPermissionDenied, @@ -597,7 +597,7 @@ test('select one picture from media library cancelled', async () => { expect(ImagePicker.launchImageLibraryAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchImageLibraryAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -644,7 +644,7 @@ test('select one picture from media library no assets', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectOnePictureFromMediaLibrary( onPermissionDenied, @@ -660,7 +660,7 @@ test('select one picture from media library no assets', async () => { expect(ImagePicker.launchImageLibraryAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchImageLibraryAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -707,7 +707,7 @@ test('select one picture from media library successful', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectOnePictureFromMediaLibrary( onPermissionDenied, @@ -726,7 +726,7 @@ test('select one picture from media library successful', async () => { expect(ImagePicker.launchImageLibraryAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchImageLibraryAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -769,7 +769,7 @@ test('select multiple pictures from media library permission denied', async () = const onCancel = jest.fn().mockResolvedValue(undefined) const onSuccess = jest.fn() - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectMultiplePicturesFromMediaLibrary( onPermissionDenied, @@ -823,7 +823,7 @@ test('select multiple pictures from media library cancelled', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectMultiplePicturesFromMediaLibrary( onPermissionDenied, @@ -839,7 +839,7 @@ test('select multiple pictures from media library cancelled', async () => { expect(ImagePicker.launchImageLibraryAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchImageLibraryAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false, @@ -900,7 +900,7 @@ test('select multiple pictures from media library successful', async () => { const onCancel = jest.fn() const onSuccess = jest.fn().mockResolvedValue(undefined) - const pictureHelper = new PictureHelper(fileStore, permissionHelper) + const pictureHelper = new PictureHelper(fileStore, permissionHelper, 0.35) await pictureHelper.selectMultiplePicturesFromMediaLibrary( onPermissionDenied, @@ -921,7 +921,7 @@ test('select multiple pictures from media library successful', async () => { expect(ImagePicker.launchImageLibraryAsync).toHaveBeenCalledTimes(1) expect(ImagePicker.launchImageLibraryAsync).toBeCalledWith({ allowsEditing: true, - quality: 0.7, + quality: 0.35, mediaTypes: ImagePicker.MediaTypeOptions.Images, exif: false, base64: false,