Skip to content

Commit a1e1c4f

Browse files
authored
Merge pull request #19 from Vadko/21.03-custom-done-button-title
feat: done button title for iOS
2 parents 2c39201 + 113fea6 commit a1e1c4f

5 files changed

Lines changed: 47 additions & 36 deletions

File tree

README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This library requires React Native 0.76.3 or newer. It is compatible with Expo S
1717
npx expo install react-native-file-viewer-turbo
1818
```
1919

20-
Add plugin to your `app.json` or `app.config.js` (it will modify AndroidManifest.xml as described below in extra step for Android section):
20+
Add plugin to your `app.json` or `app.config.js` with preferred `mimeTypes` (it will modify AndroidManifest.xml as described below in extra step for Android section):
2121

2222
```json
2323
{
@@ -26,7 +26,7 @@ Add plugin to your `app.json` or `app.config.js` (it will modify AndroidManifest
2626
"react-native-file-viewer-turbo",
2727
{
2828
"mimeTypes": [
29-
"*/*" // or any other mimeType you want to specifically include
29+
"*/*"
3030
]
3131
}
3232
]
@@ -69,6 +69,25 @@ For example, if you know upfront that your app is supposed to open PDF files, th
6969
</manifest>
7070
```
7171

72+
## API
73+
74+
### `open(filepath: string, options?: Options): Promise<void>`
75+
76+
| Parameter | Type | Description |
77+
| ---------------------- | ------ |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
78+
| **filepath** | string | The absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use [expo-file-system constants](https://docs.expo.dev/versions/latest/sdk/filesystem/#constants) to determine the absolute path correctly. |
79+
| **options** (optional) | Object | Some options to customize the behaviour. See below. |
80+
81+
#### Options
82+
83+
| Parameter | Type | Platform | Description |
84+
|------------------------------------|---------|--------------|----------------------------------------------------------------------------------------------------|
85+
| **displayName** (optional) | string | iOS | Customize the QuickLook title |
86+
| **doneButtonTitle** (optional) | string | iOS | Customize UINavigationController Done button title |
87+
| **onDismiss** (optional) | function | iOS, Android | Callback invoked when the viewer is being dismissed |
88+
| **showOpenWithDialog** (optional) | boolean | Android | If there is more than one app that can open the file, show an _Open With_ dialogue box |
89+
| **showAppsSuggestions** (optional) | boolean | Android | If there is not an installed app that can open the file, open the Play Store with suggested apps |
90+
7291
**IMPORTANT**: Try to be as granular as possible when defining your own queries. This might affect your Play Store approval, as mentioned in [Package visibility filtering on Android](https://developer.android.com/training/package-visibility).
7392

7493
> If you publish your app on Google Play, your app's use of this permission is subject to approval based on an upcoming policy.
@@ -191,26 +210,6 @@ try {
191210
}
192211
```
193212
194-
## API
195-
196-
### `open(filepath: string, options?: Object): Promise<void>`
197-
198-
| Parameter | Type | Description |
199-
| ---------------------- | ------ |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
200-
| **filepath** | string | The absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use [expo-file-system constants](https://docs.expo.dev/versions/latest/sdk/filesystem/#constants) to determine the absolute path correctly. |
201-
| **options** (optional) | Object | Some options to customize the behaviour. See below. |
202-
203-
#### Options
204-
205-
| Parameter | Type | Description |
206-
| ---------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------- |
207-
| **displayName** (optional) | string | Customize the QuickLook title (iOS only). |
208-
| **onDismiss** (optional) | function | Callback invoked when the viewer is being dismissed (iOS and Android only). |
209-
| **showOpenWithDialog** (optional) | boolean | If there is more than one app that can open the file, show an _Open With_ dialogue box (Android only). |
210-
| **showAppsSuggestions** (optional) | boolean | If there is not an installed app that can open the file, open the Play Store with suggested apps (Android only). |
211-
212-
213-
214213
## Contributing
215214
216215
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

example/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export default function App() {
2929
toFile: localFile,
3030
};
3131
downloadFile(options).promise.then(() =>
32-
open(localFile, { onDismiss: () => console.log('dismissed!') })
32+
open(localFile, {
33+
onDismiss: () => console.log('dismissed!'),
34+
doneButtonTitle: 'Custom done',
35+
})
3336
.then(console.log)
3437
.catch(console.error)
3538
);

ios/FileViewerTurbo.mm

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,35 @@ - (void)dismissView:(id)sender {
164164
options:(NSDictionary *)options
165165
resolve:(RCTPromiseResolveBlock)resolve
166166
reject:(RCTPromiseRejectBlock)reject) {
167-
167+
168168
NSString *displayName = options[@"displayName"];
169-
File *file = [[File alloc] initWithPath:path title:displayName];
169+
NSString *doneButtonTitle = options[@"doneButtonTitle"];
170170

171+
File *file = [[File alloc] initWithPath:path title:displayName];
172+
171173
QLPreviewController *controller = [[CustomQLViewController alloc] initWithFile:file identifier:invocationId];
172174
controller.delegate = self;
173-
175+
174176
if (@available(iOS 13.0, *)) {
175177
[controller setModalInPresentation: true];
176178
}
177-
179+
178180
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
179-
controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)];
180181

181-
if ([QLPreviewController canPreviewItem:file]) {
182-
[[FileViewerTurbo topViewController] presentViewController:navigationController animated:YES completion:^{
183-
resolve(nil);
184-
}];
185-
} else {
186-
reject(@"FileViewerTurbo:open", @"File not supported", nil);
187-
}
182+
if (doneButtonTitle) {
183+
controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:doneButtonTitle style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)];
184+
} else {
185+
controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)];
186+
187+
}
188+
189+
if ([QLPreviewController canPreviewItem:file]) {
190+
[[FileViewerTurbo topViewController] presentViewController:navigationController animated:YES completion:^{
191+
resolve(nil);
192+
}];
193+
} else {
194+
reject(@"FileViewerTurbo:open", @"File not supported", nil);
195+
}
188196
};
189197

190198
#ifdef RCT_NEW_ARCH_ENABLED

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.3.0",
3+
"version": "0.4.0",
44
"description": "Native file viewer for react-native - now with TurboModules support",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

src/NativeFileViewerTurbo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes';
44

55
export type Options = {
66
displayName?: string;
7+
doneButtonTitle?: string;
78
showOpenWithDialog?: boolean;
89
showAppsSuggestions?: boolean;
910
};

0 commit comments

Comments
 (0)