Skip to content

Commit 1fa8fde

Browse files
authored
Add a promise to Present File. (#172)
Fix : https://github.com/PSPDFKit/react-native/issues/170 There was previously no way of knowing if a present operation was successful or not. Now we have a promise. This may alleviate issues like https://github.com/PSPDFKit/react-native/issues/158
1 parent a54a279 commit 1fa8fde

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

samples/Catalog/Catalog.windows.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ var RNFS = require("react-native-fs");
2626

2727
import { YellowBox } from "react-native";
2828
YellowBox.ignoreWarnings([
29-
"Warning: isMounted(...) is deprecated", // React Native bug that hopefully will be fixed soon: https://github.com/facebook/react-native/issues/18868
30-
"Warning: Invalid argument supplied to oneOf" // React native windows bug.
31-
]);
29+
"Warning: isMounted(...) is deprecated", // React Native bug that hopefully will be fixed soon: https://github.com/facebook/react-native/issues/18868
30+
"Warning: Invalid argument supplied to oneOf" // React native windows bug.
31+
]);
3232

3333

3434
const complexSearchConfiguration = {
@@ -66,7 +66,9 @@ var examples = [
6666
// Present can only take files loaded in the Visual studio Project's Assets. Please use RNFS.
6767
// See https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions
6868
var path = RNFS.MainBundlePath + "\\Assets\\pdf\\Business Report.pdf";
69-
PSPDFKit.Present(path);
69+
PSPDFKit.Present(path)
70+
.then(() => { alert("File Opened successfully"); })
71+
.catch(error => { alert(error); });
7072
}
7173
},
7274
{
@@ -95,8 +97,8 @@ var examples = [
9597
await PSPDFKitLibrary.EnqueueDocumentsInFolderPicker("MyLibrary");
9698
alert(
9799
'Searching Library for "' +
98-
simpleSearch.searchString +
99-
'". Please wait.'
100+
simpleSearch.searchString +
101+
'". Please wait.'
100102
);
101103
PSPDFKitLibrary.SearchLibrary("MyLibrary", simpleSearch)
102104
.then(result => {
@@ -116,8 +118,8 @@ var examples = [
116118
await PSPDFKitLibrary.EnqueueDocumentsInFolder("AssetsLibrary", path);
117119
alert(
118120
'Searching Library for "' +
119-
complexSearchConfiguration.searchString +
120-
'". Please wait.'
121+
complexSearchConfiguration.searchString +
122+
'". Please wait.'
121123
);
122124
PSPDFKitLibrary.SearchLibrary("AssetsLibrary", complexSearchConfiguration)
123125
.then(result => {
@@ -163,18 +165,18 @@ class CatalogScreen extends Component<{}> {
163165
}
164166

165167
_renderRow = ({ item, separators }) => {
166-
return (
167-
<TouchableHighlight
168-
onPress={() => {
169-
item.action(this);
170-
}}
171-
onShowUnderlay={separators.highlight}
172-
onHideUnderlay={separators.unhighlight}>
173-
<View style={styles.rowContent}>
174-
<Text style={styles.name}>{item.name}</Text>
175-
<Text style={styles.description}>{item.description}</Text>
176-
</View>
177-
</TouchableHighlight>
168+
return (
169+
<TouchableHighlight
170+
onPress={() => {
171+
item.action(this);
172+
}}
173+
onShowUnderlay={separators.highlight}
174+
onHideUnderlay={separators.unhighlight}>
175+
<View style={styles.rowContent}>
176+
<Text style={styles.name}>{item.name}</Text>
177+
<Text style={styles.description}>{item.description}</Text>
178+
</View>
179+
</TouchableHighlight>
178180
)
179181
};
180182
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/LibraryModule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public async void EnqueueDocumentsInFolder(string libraryName, string path, IPro
8989

9090
try
9191
{
92-
var storageFolder = await StorageFolder.GetFolderFromPathAsync(path).AsTask().ConfigureAwait(false); ;
92+
var storageFolder = await StorageFolder.GetFolderFromPathAsync(path).AsTask().ConfigureAwait(false);
9393
// Queue up the PDFs in the folder for indexing.
94-
await EnqueueDocuments(_libraries[libraryName], storageFolder).ConfigureAwait(false); ;
94+
await EnqueueDocuments(_libraries[libraryName], storageFolder).ConfigureAwait(false);
9595

9696
promise.Resolve(null);
9797
}
@@ -131,7 +131,7 @@ await CoreApplication.MainView.Dispatcher.AwaitableRunAsync(async () => {
131131
}).ConfigureAwait(false); ;
132132

133133
// Queue up the PDFs in the folder for indexing.
134-
await EnqueueDocuments(_libraries[libraryName], folder).ConfigureAwait(false); ;
134+
await EnqueueDocuments(_libraries[libraryName], folder).ConfigureAwait(false);
135135

136136
promise.Resolve(null);
137137
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitModule.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.Reflection;
1515
using System.Threading.Tasks;
1616
using Windows.Storage;
17-
using Windows.UI.Popups;
1817

1918
namespace ReactNativePSPDFKit
2019
{
@@ -51,9 +50,13 @@ public void OpenFilePicker()
5150
/// Open a file from a path presented from javascript.
5251
/// Files loaded in the Visual studio Project's Assets.
5352
/// See https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions
53+
///
54+
/// Promise will resolve is the file opened correctly. Errors will also be propagated
55+
/// to the promise.
56+
///
5457
/// </summary>
5558
[ReactMethod]
56-
public void Present(string assetPath)
59+
public void Present(string assetPath, IPromise promise)
5760
{
5861
DispatcherHelpers.RunOnDispatcher(async () =>
5962
{
@@ -62,11 +65,12 @@ public void Present(string assetPath)
6265
var file = await StorageFile.GetFileFromPathAsync(assetPath);
6366

6467
await LoadFileAsync(file);
68+
69+
promise.Resolve(null);
6570
}
6671
catch (Exception e)
6772
{
68-
var dialog = new MessageDialog("Unable to open the file specified.");
69-
await dialog.ShowAsync();
73+
promise.Reject(e);
7074
}
7175
});
7276
}
@@ -75,7 +79,7 @@ public void Present(string assetPath)
7579
/// Opens the native file picker.
7680
/// </summary>
7781
/// <returns>The file chosen in the file picker.</returns>
78-
private static async Task<Windows.Storage.StorageFile> PickPDF()
82+
private static async Task<StorageFile> PickPDF()
7983
{
8084
var picker = new Windows.Storage.Pickers.FileOpenPicker
8185
{
@@ -91,7 +95,7 @@ public void Present(string assetPath)
9195
/// Call to the PDFView to open a file. Fails if file is null.
9296
/// </summary>
9397
/// <param name="file">File to open</param>
94-
private async Task LoadFileAsync(Windows.Storage.StorageFile file)
98+
private async Task LoadFileAsync(StorageFile file)
9599
{
96100
if (file == null) return;
97101

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitViewManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
using System;
1111
using System.Collections.Generic;
12-
using System.Threading.Tasks;
1312
using Windows.Data.Json;
1413
using ReactNative.UIManager;
1514
using ReactNative.UIManager.Annotations;
1615
using Windows.Storage;
1716
using Newtonsoft.Json.Linq;
18-
using PSPDFKit.Pdf;
1917
using PSPDFKit.Pdf.Annotation;
2018
using PSPDFKit.UI;
2119
using ReactNativePSPDFKit.Events;
@@ -44,7 +42,7 @@ protected override PDFViewPage CreateViewInstance(ThemedReactContext reactContex
4442
public override string Name => "RCTPSPDFKitView";
4543

4644
[ReactProp("document")]
47-
public async void SetDocumentAsync(PDFViewPage view, String document)
45+
public async void SetDocumentAsync(PDFViewPage view, string document)
4846
{
4947
var storagefile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(document));
5048
await view.SetDefaultDocument(storagefile);

0 commit comments

Comments
 (0)