66// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77// Other code you write will be lost the next time you deploy the project.
88import { Big } from "big.js" ;
9- import { Alert , Linking , NativeModules , Platform } from "react-native" ;
9+ import { NativeFileSystem } from "mendix-native" ;
10+ import { Alert , Linking , Platform } from "react-native" ;
1011import {
1112 CameraOptions ,
1213 ErrorCode ,
@@ -16,7 +17,8 @@ import {
1617 launchImageLibrary
1718} from "react-native-image-picker" ;
1819import { getLocales } from "react-native-localize" ;
19- import { ImagePickerV2Options , ImagePickerV2Response , PictureQuality , PictureSource } from "../../typings/Camera" ;
20+ import RNPermissions from "react-native-permissions" ;
21+ import { PictureQuality , PictureSource } from "../../typings/Camera" ;
2022
2123// BEGIN EXTRA CODE
2224// END EXTRA CODE
@@ -55,10 +57,6 @@ export async function TakePicture(
5557 ) ;
5658 }
5759
58- // V3 dropped the feature of providing an action sheet so users can decide on which action to take, camera or library.
59- const nativeVersionMajor = NativeModules ?. ImagePickerManager ?. showImagePicker ? 2 : 4 ;
60- const RNPermissions = nativeVersionMajor === 4 ? ( await import ( "react-native-permissions" ) ) . default : null ;
61-
6260 try {
6361 const uri = await takePicture ( ) ;
6462
@@ -77,32 +75,14 @@ export async function TakePicture(
7775
7876 function takePicture ( ) : Promise < string | undefined > {
7977 return new Promise ( ( resolve , reject ) => {
80- const options = nativeVersionMajor === 2 ? getOptionsV2 ( ) : getOptions ( ) ;
78+ const options = getOptions ( ) ;
8179 getPictureMethod ( )
8280 . then ( method =>
83- method ( options , ( response : ImagePickerV2Response | ImagePickerResponse ) => {
81+ method ( options , ( response : ImagePickerResponse ) => {
8482 if ( response . didCancel ) {
8583 return resolve ( undefined ) ;
8684 }
8785
88- if ( nativeVersionMajor === 2 ) {
89- const responseV2 = response as ImagePickerV2Response ;
90-
91- if ( responseV2 . error ) {
92- const unhandledError = handleImagePickerV2Error ( responseV2 . error ) ;
93-
94- if ( ! unhandledError ) {
95- return resolve ( undefined ) ;
96- }
97-
98- return reject ( new Error ( responseV2 . error ) ) ;
99- }
100-
101- return resolve ( responseV2 . uri ) ;
102- }
103-
104- response = response as ImagePickerResponse ;
105-
10686 if ( response . errorCode ) {
10787 handleImagePickerV4Error ( response . errorCode , response . errorMessage ) ;
10888
@@ -118,7 +98,7 @@ export async function TakePicture(
11898
11999 async function safeRemove ( filePath : string ) : Promise < void > {
120100 try {
121- await NativeModules . MxFileSystem . remove ( filePath ) ;
101+ await NativeFileSystem . remove ( filePath ) ;
122102 } catch ( error ) {
123103 console . warn ( `Failed to remove file at ${ filePath } . Error: ${ error } ` ) ;
124104 // ignore error
@@ -127,7 +107,7 @@ export async function TakePicture(
127107
128108 function storeFile ( imageObject : mendix . lib . MxObject , uri : string ) : Promise < boolean > {
129109 return new Promise ( ( resolve , reject ) => {
130- NativeModules . MxFileSystem . read ( uri . replace ( "file://" , "" ) )
110+ NativeFileSystem . read ( uri . replace ( "file://" , "" ) )
131111 . then ( ( nativeBlob : unknown ) => {
132112 const blob = new Blob ( ) ;
133113 Object . assign ( blob , { data : nativeBlob } ) ;
@@ -169,13 +149,10 @@ export async function TakePicture(
169149 }
170150
171151 async function getPictureMethod ( ) : Promise <
172- (
173- options : ImagePickerV2Options | CameraOptions | ImageLibraryOptions ,
174- callback : ( response : ImagePickerV2Response | ImagePickerResponse ) => void
175- ) => void
152+ ( options : CameraOptions | ImageLibraryOptions , callback : ( response : ImagePickerResponse ) => void ) => void
176153 > {
177154 async function handleCameraRequest ( ) : Promise < typeof launchCamera > {
178- if ( Platform . OS === "android" && nativeVersionMajor === 4 ) {
155+ if ( Platform . OS === "android" ) {
179156 await checkAndMaybeRequestAndroidPermission ( ) ;
180157 }
181158
@@ -225,38 +202,6 @@ export async function TakePicture(
225202 }
226203 }
227204
228- function getOptionsV2 ( ) : ImagePickerV2Options {
229- const { maxWidth, maxHeight } = getPictureQuality ( ) ;
230- const [ language ] = getLocales ( ) . map ( local => local . languageCode ) ;
231- const isDutch = language === "nl" ;
232-
233- return {
234- mediaType : "photo" as const ,
235- maxWidth,
236- maxHeight,
237- noData : true ,
238- title : isDutch ? "Foto toevoegen" : "Select a photo" ,
239- cancelButtonTitle : isDutch ? "Annuleren" : "Cancel" ,
240- takePhotoButtonTitle : isDutch ? "Foto maken" : "Take photo" ,
241- chooseFromLibraryButtonTitle : isDutch ? "Kies uit bibliotheek" : "Choose from library" ,
242- permissionDenied : {
243- title : isDutch
244- ? "Deze app heeft geen toegang tot uw camera of foto bibliotheek"
245- : "This app does not have access to your camera or photo library" ,
246- text : isDutch
247- ? "Ga naar Instellingen > Privacy om toegang tot uw camera en bestanden te verlenen."
248- : "To enable access, tap Settings > Privacy and turn on Camera and Photos/Storage." ,
249- reTryTitle : isDutch ? "Instellingen" : "Settings" ,
250- okTitle : isDutch ? "Annuleren" : "Cancel"
251- } ,
252- storageOptions : {
253- skipBackup : true ,
254- cameraRoll : false ,
255- privateDirectory : true
256- }
257- } ;
258- }
259-
260205 function getOptions ( ) : CameraOptions | ImageLibraryOptions {
261206 const { maxWidth, maxHeight } = getPictureQuality ( ) ;
262207 return {
@@ -293,37 +238,6 @@ export async function TakePicture(
293238 }
294239 }
295240
296- function handleImagePickerV2Error ( error : string ) : string | undefined {
297- const ERRORS = {
298- AndroidPermissionDenied : "Permissions weren't granted" ,
299- iOSPhotoLibraryPermissionDenied : "Photo library permissions not granted" ,
300- iOSCameraPermissionDenied : "Camera permissions not granted"
301- } ;
302-
303- switch ( error ) {
304- case ERRORS . iOSPhotoLibraryPermissionDenied :
305- showAlert (
306- "This app does not have access to your photo library" ,
307- "To enable access, tap Settings and turn on Photos."
308- ) ;
309- return ;
310-
311- case ERRORS . iOSCameraPermissionDenied :
312- showAlert (
313- "This app does not have access to your camera" ,
314- "To enable access, tap Settings and turn on Camera."
315- ) ;
316- return ;
317-
318- case ERRORS . AndroidPermissionDenied :
319- // Ignore this error because the image picker plugin already shows an alert in this case.
320- return ;
321-
322- default :
323- return error ;
324- }
325- }
326-
327241 function showAlert ( title : string , message : string ) : void {
328242 Alert . alert (
329243 title ,
0 commit comments