Skip to content

Commit

Permalink
Fix issue where adding images in PdfViewComponent didn't work (#375)
Browse files Browse the repository at this point in the history
This commit fixes an issue where adding images didn't work because the activity result was not delivered to the GalleryImagePickerFragment and CameraImagePickerFragment. It also bumps the version to 1.29.8
  • Loading branch information
Reinhard Hafenscher authored Aug 13, 2020
1 parent a63f88f commit 603a07f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
41 changes: 25 additions & 16 deletions android/src/main/java/com/pspdfkit/react/PSPDFKitModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,25 +300,34 @@ public void onActivityResult(Activity activity, final int requestCode, final int
// Forward the result to all our fragments.
FragmentActivity fragmentActivity = (FragmentActivity) activity;
for (final Fragment fragment : fragmentActivity.getSupportFragmentManager().getFragments()) {
if (fragment instanceof PdfFragment ||
fragment instanceof GalleryImagePickerFragment ||
fragment instanceof CameraImagePickerFragment) {
// When starting an intent from a fragment its request code is shifted to make it unique,
// we undo it here manually since react by default eats all activity results.
int requestIndex = requestCode >> REQUEST_CODE_TO_INDEX;
if (requestIndex != 0) {
// We need to wait until the next frame with delivering the result to the fragment,
// otherwise the app will crash since the fragment won't be ready.
activityResultHandler.post(new Runnable() {
@Override
public void run() {
fragment.onActivityResult(requestCode & MASKED_REQUEST_CODE_TO_REAL_CODE, resultCode, data);
}
});
handleFragment(fragment, requestCode, resultCode, data);
}
}
}

private void handleFragment(@NonNull final Fragment fragment, final int requestCode, final int resultCode, @NonNull final Intent data) {
if (fragment instanceof PdfFragment ||
fragment instanceof GalleryImagePickerFragment ||
fragment instanceof CameraImagePickerFragment) {
// When starting an intent from a fragment its request code is shifted to make it unique,
// we undo it here manually since react by default eats all activity results.
int requestIndex = requestCode >> REQUEST_CODE_TO_INDEX;
if (requestIndex != 0) {
// We need to wait until the next frame with delivering the result to the fragment,
// otherwise the app will crash since the fragment won't be ready.
activityResultHandler.post(new Runnable() {
@Override
public void run() {
fragment.onActivityResult(requestCode & MASKED_REQUEST_CODE_TO_REAL_CODE, resultCode, data);
}
}
});
}
}

// Also send this to all child fragments so we ensure the result is handled.
for (final Fragment childFragment : fragment.getChildFragmentManager().getFragments()) {
handleFragment(childFragment, requestCode, resultCode, data);
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-pspdfkit",
"version": "1.29.7",
"version": "1.29.8",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down
2 changes: 1 addition & 1 deletion samples/Catalog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Catalog",
"version": "1.29.7",
"version": "1.29.8",
"private": true,
"scripts": {
"start": "react-native start",
Expand Down
2 changes: 1 addition & 1 deletion samples/NativeCatalog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NativeCatalog",
"version": "1.29.7",
"version": "1.29.8",
"private": true,
"scripts": {
"android": "react-native run-android",
Expand Down

0 comments on commit 603a07f

Please sign in to comment.