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

Fix hot reload breaking the PdfView #269

Merged
merged 4 commits into from
Aug 14, 2019
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
21 changes: 21 additions & 0 deletions android/src/main/java/com/pspdfkit/views/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class PdfView extends FrameLayout {

private static final String FILE_SCHEME = "file:///";

/** Key to use when setting the id argument of PdfFragments created by this PdfView. */
private static final String ARG_ROOT_ID = "root_id";

private FragmentManager fragmentManager;
private EventDispatcher eventDispatcher;
private String fragmentTag;
Expand All @@ -102,6 +105,9 @@ public class PdfView extends FrameLayout {
@Nullable
private PdfTextSelectionPopupToolbar textSelectionPopupToolbar;

/** An internal id we generate so we can track if fragments found belong to this specific PdfView instance. */
private int internalId;

public PdfView(@NonNull Context context) {
super(context);
init();
Expand Down Expand Up @@ -158,6 +164,9 @@ public void doFrame(long frameTimeNanos) {

// Set a default configuration.
configuration = new PdfActivityConfiguration.Builder(getContext()).build();

// Generate an id to set on all fragments created by the PdfView.
internalId = View.generateViewId();
}

public void inject(FragmentManager fragmentManager, EventDispatcher eventDispatcher) {
Expand Down Expand Up @@ -239,8 +248,20 @@ public void setMenuItemGroupingRule(@NonNull MenuItemGroupingRule groupingRule)
private void setupFragment() {
if (fragmentTag != null && configuration != null && document != null) {
PdfFragment pdfFragment = (PdfFragment) fragmentManager.findFragmentByTag(fragmentTag);
if (pdfFragment != null &&
(pdfFragment.getArguments() == null ||
pdfFragment.getArguments().getInt(ARG_ROOT_ID) != internalId)) {
// This is an orphaned fragment probably from a reload, get rid of it.
fragmentManager.beginTransaction()
.remove(pdfFragment)
.commitNow();
pdfFragment = null;
}

if (pdfFragment == null) {
pdfFragment = PdfFragment.newInstance(document, this.configuration.getConfiguration());
// We put our internal id so we can track if this fragment belongs to us, used to handle orphaned fragments after hot reloads.
pdfFragment.getArguments().putInt(ARG_ROOT_ID, internalId);
prepareFragment(pdfFragment);
} else {
View fragmentView = pdfFragment.getView();
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.25.3",
"version": "1.25.4",
"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.25.3",
"version": "1.25.4",
"private": true,
"scripts": {
"start": "react-native start",
Expand Down