Skip to content

Commit b0d080f

Browse files
authored
fix: setModalInPresenstation & docs (#39)
1 parent d9e06c7 commit b0d080f

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ import { getDocumentAsync } from "expo-document-picker";
119119

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

137137
try {
138138
const result = await launchImageLibraryAsync();
139-
if (result.assets?.[0] === null) {
140-
return
139+
if (result.canceled || !result.assets?.[0]) {
140+
return;
141141
}
142-
await open(result.assets?.[0].uri, { displayName: 'Image' });
142+
await open(result.assets[0].uri, { displayName: 'Image' });
143143
} catch (e) {
144144
// error
145145
}
@@ -159,19 +159,20 @@ try {
159159
160160
### Open a file from Android assets folder
161161
162-
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).
162+
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).
163163
164164
Example (using expo-file-system):
165165
166166
```javascript
167167
import { open } from "react-native-file-viewer-turbo";
168-
import { copyAsync, documentDirectory } from "expo-file-system";
168+
import { File, Paths } from "expo-file-system/next";
169169

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

173-
await copyAsync({ from: file, to: dest })
174-
await open(dest, { displayName: 'My Document' })
174+
sourceFile.copy(destFile);
175+
await open(destFile.uri, { displayName: 'My Document' });
175176
```
176177
177178
### Download and open a file (using [expo-file-system](https://docs.expo.dev/versions/latest/sdk/filesystem))
@@ -183,8 +184,7 @@ Example (using expo-file-system):
183184
184185
```javascript
185186
import { open } from "react-native-file-viewer-turbo";
186-
import { downloadAsync, documentDirectory } from "expo-file-system";
187-
import { Platform } from "react-native";
187+
import { File, Paths } from "expo-file-system/next";
188188

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

200200
const extension = getUrlExtension(url);
201201

202-
// Feel free to change main path according to your requirements.
203-
const localFile = `${documentDirectory}/temporaryfile.${extension}`;
202+
try {
203+
const destination = new File(Paths.document, `temporaryfile.${extension}`);
204+
205+
// Delete existing file if exists
206+
if (destination.exists) {
207+
destination.delete();
208+
}
204209

205-
const options = {
206-
fromUrl: url,
207-
toFile: localFile,
208-
};
210+
await File.downloadFileAsync(url, destination);
209211

210-
try {
211-
const result = await downloadAsync(url, localFile);
212-
await open(result.uri, { displayName: "Downloaded PDF" });
212+
await open(destination.uri, { displayName: "Downloaded PDF" });
213213
} catch (e) {
214214
// error
215215
}

ios/FileViewerTurbo.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,19 @@ - (void)dismissView:(id)sender {
140140
File *file = [[File alloc] initWithPath:path title:displayName];
141141

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

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

148+
controller.delegate = self;
149+
149150
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
150151

152+
if (@available(iOS 13.0, *)) {
153+
[navigationController setModalInPresentation: true];
154+
}
155+
151156
if (doneButtonTitle) {
152157
buttonItem = [[UIBarButtonItem alloc] initWithTitle:doneButtonTitle style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)];
153158
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-file-viewer-turbo",
3-
"version": "0.7.2",
3+
"version": "0.7.3",
44
"description": "Native file viewer for react-native - now with TurboModules support",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

0 commit comments

Comments
 (0)