Skip to content

Commit de188dc

Browse files
authored
Merge pull request #386 from samuel-rl/update-example-file-picker
docs: drop rn document picker for expo one in the example
2 parents 2fbffa4 + d8c8879 commit de188dc

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

example/app.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"ACCESS_FINE_LOCATION",
2121
"ACCESS_COARSE_LOCATION"
2222
]
23-
}
23+
},
24+
"plugins": [
25+
[
26+
"expo-document-picker"
27+
]
28+
]
2429
}
2530
}

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
"dependencies": {
1414
"@playerdata/react-native-mcu-manager": "workspace:*",
1515
"expo": "51.0.36",
16+
"expo-document-picker": "^12.0.2",
1617
"expo-splash-screen": "0.27.6",
1718
"expo-status-bar": "1.12.1",
1819
"lodash": "4.17.21",
1920
"react": "18.3.1",
2021
"react-native": "0.74.5",
2122
"react-native-ble-plx": "3.2.1",
22-
"react-native-document-picker": "9.3.1",
2323
"react-native-get-random-values": "1.11.0",
2424
"react-native-toast-message": "2.2.1"
2525
},

example/src/useFilePicker.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1+
import * as DocumentPicker from 'expo-document-picker';
12
import { useState } from 'react';
2-
import { Platform } from 'react-native';
3-
import DocumentPicker from 'react-native-document-picker';
4-
import type { DocumentPickerResponse } from 'react-native-document-picker';
53

64
export interface SelectedFile {
75
uri: string;
@@ -17,37 +15,20 @@ const useFilePicker = (): {
1715
const [filePickerError, setError] = useState<string | null>(null);
1816

1917
const pickFile = async () => {
20-
let result: DocumentPickerResponse | null = null;
21-
let fileDelimiter: string | null = null;
2218
try {
23-
if (Platform.OS === 'ios') {
24-
fileDelimiter = '%2F';
25-
result = await DocumentPicker.pickSingle({
26-
allowMultiSelection: false,
27-
type: ['public.data'],
28-
copyTo: 'cachesDirectory',
29-
});
30-
}
31-
if (Platform.OS === 'android') {
32-
fileDelimiter = '/';
33-
result = await DocumentPicker.pickSingle({
34-
allowMultiSelection: false,
35-
type: ['*/*'],
36-
});
37-
}
38-
39-
if (result == null || fileDelimiter == null) {
40-
throw new Error('Failed to pick a file, is your OS supported?');
41-
}
19+
const pickedFile = await DocumentPicker.getDocumentAsync();
4220

43-
const uri = result.fileCopyUri ? result.fileCopyUri : result.uri;
44-
setSelectedFile({ uri, name: uri.split(fileDelimiter).slice(-1)[0] });
45-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46-
} catch (err: any) {
47-
if (!DocumentPicker.isCancel(err)) {
48-
setSelectedFile(null);
49-
setError(err.message);
21+
const assets = pickedFile.assets;
22+
if (!assets) {
23+
return;
5024
}
25+
const file = assets[0];
26+
setSelectedFile({
27+
uri: file.uri,
28+
name: file.name || '',
29+
});
30+
} catch (error) {
31+
setError(JSON.stringify(error));
5132
}
5233
};
5334

pnpm-lock.yaml

Lines changed: 12 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)