Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a promise when calling PSPDFKit.present() #348

Merged
merged 4 commits into from
Feb 12, 2020
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
38 changes: 35 additions & 3 deletions android/src/main/java/com/pspdfkit/react/PSPDFKitModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand Down Expand Up @@ -61,6 +63,12 @@ public class PSPDFKitModule extends ReactContextBaseJavaModule implements Applic
@NonNull
private Handler activityResultHandler = new Handler(Looper.getMainLooper());

/**
* The last promise we received when calling present. Used to notify once the document is loaded.
*/
@Nullable
private Promise lastPresentPromise;

public PSPDFKitModule(ReactApplicationContext reactContext) {
super(reactContext);
}
Expand All @@ -83,7 +91,7 @@ public String getName() {
}

@ReactMethod
public void present(@NonNull String document, @NonNull ReadableMap configuration) {
public void present(@NonNull String document, @NonNull ReadableMap configuration, @Nullable Promise promise) {
if (getCurrentActivity() != null) {
if (resumedActivity == null) {
// We register an activity lifecycle callback so we can get notified of the current activity.
Expand All @@ -95,12 +103,13 @@ public void present(@NonNull String document, @NonNull ReadableMap configuration
document = FILE_SCHEME + document;
}

lastPresentPromise = promise;
PdfActivity.showDocument(getCurrentActivity(), Uri.parse(document), configurationAdapter.build());
}
}

@ReactMethod
public void presentImage(@NonNull String imageDocument, @NonNull ReadableMap configuration) {
public void presentImage(@NonNull String imageDocument, @NonNull ReadableMap configuration, @Nullable Promise promise) {
if (getCurrentActivity() != null) {
if (resumedActivity == null) {
// We register an activity lifecycle callback so we can get notified of the current activity.
Expand All @@ -112,18 +121,21 @@ public void presentImage(@NonNull String imageDocument, @NonNull ReadableMap con
imageDocument = FILE_SCHEME + imageDocument;
}

lastPresentPromise = promise;
PdfActivity.showImage(getCurrentActivity(), Uri.parse(imageDocument), configurationAdapter.build());
}
}

@ReactMethod
public void presentInstant(@NonNull String serverUrl, @NonNull String jwt, @NonNull ReadableMap configuration) {
public void presentInstant(@NonNull String serverUrl, @NonNull String jwt, @NonNull ReadableMap configuration, @Nullable Promise promise) {
if (getCurrentActivity() != null) {
if (resumedActivity == null) {
// We register an activity lifecycle callback so we can get notified of the current activity.
getCurrentActivity().getApplication().registerActivityLifecycleCallbacks(this);
}
ConfigurationAdapter configurationAdapter = new ConfigurationAdapter(getCurrentActivity(), configuration);

lastPresentPromise = promise;
InstantPdfActivity.showInstantDocument(getCurrentActivity(), serverUrl, jwt, configurationAdapter.build());
}
}
Expand Down Expand Up @@ -189,6 +201,26 @@ public synchronized void onActivityResumed(Activity activity) {
// Run our queued up task when a PdfActivity is displayed.
onPdfActivityOpenedTask.run();
onPdfActivityOpenedTask = null;

// We notify the called as soon as the document is loaded or loading failed.
if (lastPresentPromise != null) {
PdfActivity pdfActivity = (PdfActivity) resumedActivity;
pdfActivity.getPdfFragment().addDocumentListener(new SimpleDocumentListener() {
@Override
public void onDocumentLoaded(@NonNull PdfDocument document) {
super.onDocumentLoaded(document);
lastPresentPromise.resolve(Boolean.TRUE);
lastPresentPromise = null;
}

@Override
public void onDocumentLoadFailed(@NonNull Throwable exception) {
super.onDocumentLoadFailed(exception);
lastPresentPromise.reject(exception);
lastPresentPromise = null;
}
});
}
}
}

Expand Down
45 changes: 34 additions & 11 deletions ios/RCTPSPDFKit/RCTPSPDFKitManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,59 @@ @implementation RCTPSPDFKitManager

RCT_EXPORT_MODULE(PSPDFKit)

RCT_EXPORT_METHOD(setLicenseKey:(NSString *)licenseKey) {
[PSPDFKitGlobal setLicenseKey:licenseKey];
RCT_REMAP_METHOD(setLicenseKey, setLicenseKey:(NSString *)licenseKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
if (licenseKey.length > 0) {
[PSPDFKitGlobal setLicenseKey:licenseKey];
resolve(@(YES));
} else {
reject(@"error", @"Invalid License Key.", nil);
}
}

RCT_EXPORT_METHOD(present:(PSPDFDocument *)document withConfiguration:(PSPDFConfiguration *)configuration) {
RCT_REMAP_METHOD(present, present:(PSPDFDocument *)document withConfiguration:(PSPDFConfiguration *)configuration resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
PSPDFViewController *pdfViewController = [[PSPDFViewController alloc] initWithDocument:document configuration:configuration];

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

navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
UIViewController *presentingViewController = RCTPresentedViewController();
[presentingViewController presentViewController:navigationController animated:YES completion:nil];

if (presentingViewController) {
[presentingViewController presentViewController:navigationController animated:YES completion:^{
resolve(@(YES));
}];
} else {
reject(@"error", @"Failed to present document.", nil);
}
}

RCT_EXPORT_METHOD(dismiss) {
RCT_REMAP_METHOD(dismiss, resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
UIViewController *presentedViewController = RCTPresentedViewController();
NSAssert([presentedViewController isKindOfClass:UINavigationController.class], @"Presented view controller needs to be a UINavigationController");
UINavigationController *navigationController = (UINavigationController *)presentedViewController;
NSAssert(navigationController.viewControllers.count == 1 && [navigationController.viewControllers.firstObject isKindOfClass:PSPDFViewController.class], @"Presented view controller needs to contain a PSPDFViewController");
[navigationController dismissViewControllerAnimated:true completion:nil];

if (navigationController) {
[navigationController dismissViewControllerAnimated:YES completion:^{
resolve(@(YES));
}];
} else {
reject(@"error", @"Failed to dismiss", nil);
}
}

RCT_EXPORT_METHOD(setPageIndex:(NSUInteger)pageIndex animated:(BOOL)animated) {
RCT_REMAP_METHOD(setPageIndex, setPageIndex:(NSUInteger)pageIndex animated:(BOOL)animated resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
UIViewController *presentedViewController = RCTPresentedViewController();
NSAssert([presentedViewController isKindOfClass:UINavigationController.class], @"Presented view controller needs to be a UINavigationController");
UINavigationController *navigationController = (UINavigationController *)presentedViewController;
NSAssert(navigationController.viewControllers.count == 1 && [navigationController.viewControllers.firstObject isKindOfClass:PSPDFViewController.class], @"Presented view controller needs to contain a PSPDFViewController");
PSPDFViewController *pdfViewController = (PSPDFViewController *)navigationController.viewControllers.firstObject;

[pdfViewController setPageIndex:pageIndex animated:animated];
PSPDFViewController *pdfViewController = (PSPDFViewController *)navigationController.viewControllers.firstObject;
// Validate the the page index is not out of bounds.
if (pageIndex < pdfViewController.document.pageCount) {
[pdfViewController setPageIndex:pageIndex animated:animated];
resolve(@(YES));
} else {
reject(@"error", @"Failed to set page index: The page index is out of bounds", nil);
}
}

- (dispatch_queue_t)methodQueue {
Expand Down
7 changes: 6 additions & 1 deletion samples/Catalog/Catalog.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ const examples = [
name: "Open assets document",
description: "Open document from your project assets folder",
action: () => {
PSPDFKit.present("file:///android_asset/Annual Report.pdf", {});
PSPDFKit.present("file:///android_asset/Annual Report.pdf", {})
.then(loaded => {
console.log("Document was loaded successfully.")
}).catch(error => {
console.log(error);
});
PSPDFKit.setPageIndex(3, false);
}
},
Expand Down
38 changes: 34 additions & 4 deletions samples/Catalog/Catalog.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ const RNFS = require("react-native-fs");
import PSPDFKitView from "react-native-pspdfkit";
const PSPDFKit = NativeModules.PSPDFKit;

PSPDFKit.setLicenseKey("YOUR_LICENSE_KEY_GOES_HERE");
PSPDFKit.setLicenseKey("YOUR_LICENSE_KEY_GOES_HERE").then(result => {
if (result) {
console.log("Successfully set the license key.");
} else {
console.log(error);
}
});

const pspdfkitColor = "#267AD4";
const pspdfkitColorAlpha = "#267AD450";
Expand All @@ -37,7 +43,13 @@ const examples = [
name: "Open document using resource path",
description: "Open document from your resource bundle with relative path.",
action: () => {
PSPDFKit.present("PDFs/Annual Report.pdf", {});
PSPDFKit.present("PDFs/Annual Report.pdf", {}).then(result => {
if (result) {
console.log("Successfully presented document.");
} else {
console.log(error);
}
});
}
},
{
Expand All @@ -58,8 +70,20 @@ const examples = [
}
})
.then(() => {
PSPDFKit.present(path, {});
PSPDFKit.setPageIndex(3, false);
PSPDFKit.present(path, {}).then(result => {
if (result) {
console.log("Successfully presented document.");
} else {
console.log(error);
}
});
PSPDFKit.setPageIndex(3, false).then(result => {
if (result) {
console.log("Successfully set the page index.");
} else {
console.log(error);
}
});
})
.catch(err => {
console.log(err.message, err.code);
Expand All @@ -80,6 +104,12 @@ const examples = [
showPageLabels: false,
showDocumentLabel: true,
inlineSearch: true
}).then(result => {
if (result) {
console.log("Successfully presented document.");
} else {
console.log(error);
}
});
}
},
Expand Down