Skip to content

Commit d541e08

Browse files
authored
Create the PDFViewPage when asked to by react native. (#229)
There was a lifetime issue when navigating back and forth between views. Now we create a view when react native wants to. The unloading is still handled in UWP.
1 parent 7ca8638 commit d541e08

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-pspdfkit",
3-
"version": "1.23.13",
3+
"version": "1.23.14",
44
"description": "A React Native module for the PSPDFKit library.",
55
"keywords": [
66
"react native",

samples/Catalog/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Catalog",
3-
"version": "1.23.13",
3+
"version": "1.23.14",
44
"private": true,
55
"scripts": {
66
"start": "node node_modules/react-native/local-cli/cli.js start"

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PDFViewPage.xaml.cs

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public PDFViewPage()
3939
// This is a work aronud to ensure that if the user navigates away from
4040
// the Page and then back, a document will still be shown.
4141
_fileToOpen = null;
42+
43+
args.Complete();
4244
};
4345

4446
PDFView.OnDocumentOpened += (pdfView, document) =>

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitModule.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace ReactNativePSPDFKit
2222
/// </summary>
2323
class PSPDFKitModule : ReactContextNativeModuleBase
2424
{
25-
private readonly PDFViewPage _pdfViewPage;
25+
private readonly PSPDFKitViewManger _viewManager;
2626
private string VERSION_KEY = "versionString";
2727

28-
public PSPDFKitModule(ReactContext reactContext, PDFViewPage pdfViewPage) : base(reactContext)
28+
public PSPDFKitModule(ReactContext reactContext, PSPDFKitViewManger viewManager) : base(reactContext)
2929
{
30-
_pdfViewPage = pdfViewPage;
30+
_viewManager = viewManager;
3131
}
3232

3333
/// <summary>
@@ -98,8 +98,14 @@ private static async Task<StorageFile> PickPDF()
9898
private async Task LoadFileAsync(StorageFile file)
9999
{
100100
if (file == null) return;
101-
102-
await _pdfViewPage.OpenFileAsync(file);
101+
if(_viewManager.PdfViewPage != null)
102+
{
103+
await _viewManager.PdfViewPage.OpenFileAsync(file);
104+
}
105+
else
106+
{
107+
throw new Exception("PdfViewPage: is not ready or unavailable.");
108+
}
103109
}
104110

105111
/// <summary>

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitPackage.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using ReactNative.UIManager;
1313
using System;
1414
using System.Collections.Generic;
15+
using Windows.UI.Xaml;
1516

1617
namespace ReactNativePSPDFKit
1718
{
@@ -23,13 +24,14 @@ namespace ReactNativePSPDFKit
2324
/// </summary>
2425
public class PSPDFKitPackage : IReactPackage
2526
{
26-
private readonly PSPDFKitViewManger _pspdfkitViewManger = new PSPDFKitViewManger();
27+
private readonly PSPDFKitViewManger _pspdfkitViewManger;
2728

2829
/// <summary>
2930
/// Creates a PSPDFKit package with the default settings.
3031
/// </summary>
3132
public PSPDFKitPackage()
3233
{
34+
_pspdfkitViewManger = new PSPDFKitViewManger(null);
3335
}
3436

3537
/// <summary>
@@ -38,7 +40,7 @@ public PSPDFKitPackage()
3840
/// </summary>
3941
public PSPDFKitPackage(Uri cssResource)
4042
{
41-
_pspdfkitViewManger.PdfViewPage.Pdfview.Css = cssResource;
43+
_pspdfkitViewManger = new PSPDFKitViewManger(cssResource);
4244
}
4345

4446
/// <summary>
@@ -49,10 +51,20 @@ public PSPDFKitPackage(Uri cssResource)
4951
/// <returns>The list of native modules.</returns>
5052
public IReadOnlyList<INativeModule> CreateNativeModules(ReactContext reactContext)
5153
{
54+
object pspdfkitLicense;
55+
try
56+
{
57+
pspdfkitLicense = Application.Current.Resources["PSPDFKitLicense"];
58+
}
59+
catch (Exception)
60+
{
61+
throw new Exception("Please ensure you define PSPDFKitLicense. See https://github.com/PSPDFKit/react-native#running-catalog-project-2");
62+
}
63+
5264
return new List<INativeModule>
5365
{
54-
new PSPDFKitModule(reactContext, _pspdfkitViewManger.PdfViewPage),
55-
new LibraryModule(reactContext, _pspdfkitViewManger.PdfViewPage.Pdfview.License)
66+
new PSPDFKitModule(reactContext, _pspdfkitViewManger),
67+
new LibraryModule(reactContext, pspdfkitLicense as string)
5668
};
5769
}
5870

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitViewManager.cs

+20-9
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@ public class PSPDFKitViewManger : SimpleViewManager<PDFViewPage>
2929
private const int COMMAND_SET_TOOLBAR_ITEMS = 7;
3030
private const int COMMAND_REMOVE_ANNOTATION = 8;
3131

32-
internal readonly PDFViewPage PdfViewPage = new PDFViewPage();
32+
private readonly Uri _cssResource = null;
33+
internal PDFViewPage PdfViewPage;
34+
35+
public PSPDFKitViewManger(Uri cssResource)
36+
{
37+
_cssResource = cssResource;
38+
}
3339

3440
protected override PDFViewPage CreateViewInstance(ThemedReactContext reactContext)
3541
{
42+
PdfViewPage = new PDFViewPage();
43+
if(_cssResource != null)
44+
{
45+
PdfViewPage.Pdfview.Css = _cssResource;
46+
}
3647
return PdfViewPage;
3748
}
3849

@@ -93,28 +104,28 @@ public override async void ReceiveCommand(PDFViewPage view, int commandId, JArra
93104
switch (commandId)
94105
{
95106
case COMMAND_ENTER_ANNOTATION_CREATION_MODE:
96-
await PdfViewPage.SetInteractionMode(args[0].Value<int>(), InteractionMode.Note);
107+
await view.SetInteractionMode(args[0].Value<int>(), InteractionMode.Note);
97108
break;
98109
case COMMAND_EXIT_CURRENTLY_ACTIVE_MODE:
99-
await PdfViewPage.SetInteractionMode(args[0].Value<int>(), InteractionMode.None);
110+
await view.SetInteractionMode(args[0].Value<int>(), InteractionMode.None);
100111
break;
101112
case COMMAND_SAVE_CURRENT_DOCUMENT:
102-
await PdfViewPage.ExportCurrentDocument(args[0].Value<int>());
113+
await view.ExportCurrentDocument(args[0].Value<int>());
103114
break;
104115
case COMMAND_GET_ANNOTATIONS:
105-
await PdfViewPage.GetAnnotations(args[0].Value<int>(), args[1].Value<int>());
116+
await view.GetAnnotations(args[0].Value<int>(), args[1].Value<int>());
106117
break;
107118
case COMMAND_ADD_ANNOTATION:
108-
await PdfViewPage.CreateAnnotation(args[0].Value<int>(), args[1].ToString());
119+
await view.CreateAnnotation(args[0].Value<int>(), args[1].ToString());
109120
break;
110121
case COMMAND_REMOVE_ANNOTATION:
111-
await PdfViewPage.RemoveAnnotation(args[0].Value<int>(), args[1].ToString());
122+
await view.RemoveAnnotation(args[0].Value<int>(), args[1].ToString());
112123
break;
113124
case COMMAND_GET_TOOLBAR_ITEMS:
114-
PdfViewPage.GetToolbarItems(args[0].Value<int>());
125+
view.GetToolbarItems(args[0].Value<int>());
115126
break;
116127
case COMMAND_SET_TOOLBAR_ITEMS:
117-
await PdfViewPage.SetToolbarItems(args[0].Value<int>(), args[1].ToString());
128+
await view.SetToolbarItems(args[0].Value<int>(), args[1].ToString());
118129
break;
119130
}
120131
}

0 commit comments

Comments
 (0)