Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ import { getDocumentAsync } from "expo-document-picker";

try {
const result = await getDocumentAsync({ type: 'application/pdf' });
if (result.assets?.[0] === null) {
return
if (result.canceled || !result.assets?.[0]) {
return;
}
await open(result.assets?.[0].uri, { displayName: 'My PDF Document' });
await open(result.assets[0].uri, { displayName: 'My PDF Document' });
} catch (e) {
// error
}
Expand All @@ -136,10 +136,10 @@ import { launchImageLibraryAsync } from "expo-image-picker";

try {
const result = await launchImageLibraryAsync();
if (result.assets?.[0] === null) {
return
if (result.canceled || !result.assets?.[0]) {
return;
}
await open(result.assets?.[0].uri, { displayName: 'Image' });
await open(result.assets[0].uri, { displayName: 'Image' });
} catch (e) {
// error
}
Expand All @@ -159,19 +159,20 @@ try {

### Open a file from Android assets folder

Since the library works only with absolute paths and Android assets folder doesn't have any absolute path, the file needs to be copied first. Use [copyAsync](https://docs.expo.dev/versions/latest/sdk/filesystem/#filesystemcopyasyncoptions) of [expo-file-system](https://docs.expo.dev/versions/latest/sdk/filesystem).
Since the library works only with absolute paths and Android assets folder doesn't have any absolute path, the file needs to be copied first. Use [expo-file-system](https://docs.expo.dev/versions/latest/sdk/filesystem).

Example (using expo-file-system):

```javascript
import { open } from "react-native-file-viewer-turbo";
import { copyAsync, documentDirectory } from "expo-file-system";
import { File, Paths } from "expo-file-system/next";

const file = "file-to-open.doc"; // this is your file name
const dest = `${documentDirectory}/${file}`;
const fileName = "file-to-open.doc";
const sourceFile = new File(Paths.cache, fileName);
const destFile = new File(Paths.document, fileName);

await copyAsync({ from: file, to: dest })
await open(dest, { displayName: 'My Document' })
sourceFile.copy(destFile);
await open(destFile.uri, { displayName: 'My Document' });
```

### Download and open a file (using [expo-file-system](https://docs.expo.dev/versions/latest/sdk/filesystem))
Expand All @@ -183,8 +184,7 @@ Example (using expo-file-system):

```javascript
import { open } from "react-native-file-viewer-turbo";
import { downloadAsync, documentDirectory } from "expo-file-system";
import { Platform } from "react-native";
import { File, Paths } from "expo-file-system/next";

const url =
"https://github.com/Vadko/react-native-file-viewer-turbo/raw/main/docs/sample.pdf";
Expand All @@ -199,17 +199,17 @@ function getUrlExtension(url: string) {

const extension = getUrlExtension(url);

// Feel free to change main path according to your requirements.
const localFile = `${documentDirectory}/temporaryfile.${extension}`;
try {
const destination = new File(Paths.document, `temporaryfile.${extension}`);

// Delete existing file if exists
if (destination.exists) {
destination.delete();
}

const options = {
fromUrl: url,
toFile: localFile,
};
await File.downloadFileAsync(url, destination);

try {
const result = await downloadAsync(url, localFile);
await open(result.uri, { displayName: "Downloaded PDF" });
await open(destination.uri, { displayName: "Downloaded PDF" });
} catch (e) {
// error
}
Expand Down
7 changes: 6 additions & 1 deletion ios/FileViewerTurbo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,19 @@ - (void)dismissView:(id)sender {
File *file = [[File alloc] initWithPath:path title:displayName];

QLPreviewController *controller = [[CustomQLViewController alloc] initWithFile:file identifier:invocationId];
controller.delegate = self;

if (@available(iOS 13.0, *)) {
[controller setModalInPresentation: true];
}

controller.delegate = self;

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];

if (@available(iOS 13.0, *)) {
[navigationController setModalInPresentation: true];
}

if (doneButtonTitle) {
buttonItem = [[UIBarButtonItem alloc] initWithTitle:doneButtonTitle style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)];
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-file-viewer-turbo",
"version": "0.7.2",
"version": "0.7.3",
"description": "Native file viewer for react-native - now with TurboModules support",
"source": "./src/index.tsx",
"main": "./lib/commonjs/index.js",
Expand Down
Loading