Skip to content

Commit

Permalink
Merge pull request #322 from PSPDFKit/reinhard/pdfui-fragment
Browse files Browse the repository at this point in the history
Make PdfView use PdfUiFragment instead of PdfFragment
  • Loading branch information
Reinhard Hafenscher authored Jan 30, 2020
2 parents 8609d16 + 29b6eb9 commit c0c3dd9
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 480 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pspdfkit.views;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.pspdfkit.configuration.activity.PdfActivityConfiguration;
import com.pspdfkit.ui.PdfUiFragment;

/** This {@link PdfUiFragment} reports when the {@link PdfActivityConfiguration} was changed. */
public class ConfigurationChangeReportingPdfUiFragment extends PdfUiFragment {

@Nullable private OnConfigurationChangedListener onConfigurationChangedListener;

public void setOnConfigurationChangedListener(@Nullable OnConfigurationChangedListener listener) {
this.onConfigurationChangedListener = listener;
}

@Override
public void performApplyConfiguration(@NonNull PdfActivityConfiguration configuration) {
super.performApplyConfiguration(configuration);

if (this.onConfigurationChangedListener != null) {
onConfigurationChangedListener.onConfigurationChanged();
}
}

/** Listener that is notified when the user changes the configuration (scroll direction etc.) via the UI. */
public interface OnConfigurationChangedListener {

/** Called when the configuration changed, reset your {@link com.pspdfkit.ui.PdfFragment} listeners in here. */
void onConfigurationChanged();
}
}
487 changes: 209 additions & 278 deletions android/src/main/java/com/pspdfkit/views/PdfView.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.pspdfkit.views;

import android.graphics.PointF;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.MotionEvent;

import com.facebook.react.uimanager.events.EventDispatcher;
import com.pspdfkit.annotations.Annotation;
Expand Down Expand Up @@ -86,8 +87,8 @@ public boolean onDocumentClick() {
}

@Override
public void onPageChanged(@NonNull PdfDocument pdfDocument, int i) {

public void onPageChanged(@NonNull PdfDocument pdfDocument, int pageIndex) {
parent.updateState(pageIndex);
}

@Override
Expand Down
175 changes: 51 additions & 124 deletions android/src/main/java/com/pspdfkit/views/PdfViewModeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,196 +2,123 @@

import androidx.annotation.NonNull;

import com.pspdfkit.react.R;
import com.pspdfkit.ui.forms.FormEditingBar;
import com.pspdfkit.ui.inspector.PropertyInspectorCoordinatorLayout;
import com.pspdfkit.ui.inspector.annotation.DefaultAnnotationCreationInspectorController;
import com.pspdfkit.ui.inspector.annotation.DefaultAnnotationEditingInspectorController;
import com.pspdfkit.ui.inspector.forms.FormEditingInspectorController;
import com.pspdfkit.ui.special_mode.controller.AnnotationCreationController;
import com.pspdfkit.ui.special_mode.controller.AnnotationEditingController;
import com.pspdfkit.ui.special_mode.controller.FormEditingController;
import com.pspdfkit.ui.special_mode.controller.TextSelectionController;
import com.pspdfkit.ui.special_mode.manager.AnnotationManager;
import com.pspdfkit.ui.special_mode.manager.FormManager;
import com.pspdfkit.ui.special_mode.manager.TextSelectionManager;
import com.pspdfkit.ui.toolbar.AnnotationCreationToolbar;
import com.pspdfkit.ui.toolbar.AnnotationEditingToolbar;
import com.pspdfkit.ui.toolbar.ContextualToolbar;
import com.pspdfkit.ui.toolbar.ToolbarCoordinatorLayout;
import com.pspdfkit.ui.toolbar.grouping.MenuItemGroupingRule;
import com.pspdfkit.ui.toolbar.grouping.presets.AnnotationCreationToolbarGroupingRule;

import javax.annotation.Nullable;

/**
* Keeps track of the currently active mode and handles updating the toolbar states.
*/
class PdfViewModeController implements AnnotationManager.OnAnnotationCreationModeChangeListener,
AnnotationManager.OnAnnotationEditingModeChangeListener,
FormManager.OnFormElementEditingModeChangeListener,
TextSelectionManager.OnTextSelectionModeChangeListener {
class PdfViewModeController implements
TextSelectionManager.OnTextSelectionModeChangeListener,
ToolbarCoordinatorLayout.OnContextualToolbarLifecycleListener, FormEditingBar.OnFormEditingBarLifecycleListener {

private final PdfView parent;
private final ToolbarCoordinatorLayout toolbarCoordinatorLayout;
private final AnnotationCreationToolbar annotationCreationToolbar;
private final AnnotationEditingToolbar annotationEditingToolbar;
private final FormEditingBar formEditingBar;

private boolean annotationCreationActive = false;
private boolean annotationEditingActive = false;
private boolean textSelectionActive = false;
private boolean formEditingActive = false;

private final DefaultAnnotationCreationInspectorController annotationCreationInspectorController;
private final DefaultAnnotationEditingInspectorController annotationEditingInspectorController;
private final FormEditingInspectorController formEditingInspectorController;
@Nullable
private MenuItemGroupingRule itemGroupingRule;

PdfViewModeController(PdfView parent,
PropertyInspectorCoordinatorLayout inspectorCoordinatorLayout,
ToolbarCoordinatorLayout toolbarCoordinatorLayout,
FormEditingBar formEditingBar) {
PdfViewModeController(@NonNull PdfView parent) {
this.parent = parent;
this.toolbarCoordinatorLayout = toolbarCoordinatorLayout;
this.annotationCreationToolbar = new AnnotationCreationToolbar(parent.getContext());
this.annotationEditingToolbar = new AnnotationEditingToolbar(parent.getContext());
this.formEditingBar = formEditingBar;

this.annotationCreationInspectorController = new DefaultAnnotationCreationInspectorController(parent.getContext(), inspectorCoordinatorLayout);
this.annotationEditingInspectorController = new DefaultAnnotationEditingInspectorController(parent.getContext(), inspectorCoordinatorLayout);
this.formEditingInspectorController = new FormEditingInspectorController(parent.getContext(), inspectorCoordinatorLayout);
}

/**
* Sets the menu item grouping rule that will be used for the annotation creation toolbar.
*/
public void setMenuItemGroupingRule(@Nullable MenuItemGroupingRule groupingRule) {
this.annotationCreationToolbar.setMenuItemGroupingRule(groupingRule);
this.itemGroupingRule = groupingRule;
}

@Override
public void onEnterAnnotationCreationMode(@NonNull AnnotationCreationController controller) {
annotationCreationActive = true;

// When entering the annotation creation mode we bind the creation inspector to the provided controller.
// Controller handles request for toggling annotation inspector.
annotationCreationInspectorController.bindAnnotationCreationController(controller);

// When entering the annotation creation mode we bind the toolbar to the provided controller, and
// issue the coordinator layout to animate the toolbar in place.
// Whenever the user presses an action, the toolbar forwards this command to the controller.
annotationCreationToolbar.bindController(controller);
toolbarCoordinatorLayout.displayContextualToolbar(annotationCreationToolbar, true);
parent.manuallyLayoutChildren();
parent.updateState();
public void onEnterTextSelectionMode(@NonNull TextSelectionController controller) {
textSelectionActive = true;
}

@Override
public void onChangeAnnotationCreationMode(@NonNull AnnotationCreationController controller) {
// Nothing to be done here, if toolbar is bound to the controller it will pick up the changes.
public void onExitTextSelectionMode(@NonNull TextSelectionController controller) {
textSelectionActive = false;
}

@Override
public void onExitAnnotationCreationMode(@NonNull AnnotationCreationController controller) {
annotationCreationActive = false;

// Once we're done with editing, unbind the controller from the toolbar, and remove it from the
// toolbar coordinator layout (with animation in this case).
toolbarCoordinatorLayout.removeContextualToolbar(true);
annotationCreationToolbar.unbindController();

// Also unbind the annotation creation controller from the inspector controller.
annotationCreationInspectorController.unbindAnnotationCreationController();
parent.manuallyLayoutChildren();
parent.updateState();
boolean isAnnotationCreationActive() {
return annotationCreationActive;
}

@Override
public void onEnterAnnotationEditingMode(@NonNull AnnotationEditingController controller) {
annotationEditingActive = true;
boolean isAnnotationEditingActive() {
return annotationEditingActive;
}

annotationEditingInspectorController.bindAnnotationEditingController(controller);
boolean isTextSelectionActive() {
return textSelectionActive;
}

annotationEditingToolbar.bindController(controller);
toolbarCoordinatorLayout.displayContextualToolbar(annotationEditingToolbar, true);
parent.manuallyLayoutChildren();
parent.updateState();
boolean isFormEditingActive() {
return formEditingActive;
}

@Override
public void onChangeAnnotationEditingMode(@NonNull AnnotationEditingController controller) {
// Nothing to be done here, if toolbar is bound to the controller it will pick up the changes.
public void onPrepareContextualToolbar(@NonNull ContextualToolbar contextualToolbar) {
if (contextualToolbar instanceof AnnotationCreationToolbar) {
if (itemGroupingRule != null) {
contextualToolbar.setMenuItemGroupingRule(itemGroupingRule);
} else {
contextualToolbar.setMenuItemGroupingRule(new AnnotationCreationToolbarGroupingRule(parent.getContext()));
}
}
}

@Override
public void onExitAnnotationEditingMode(@NonNull AnnotationEditingController controller) {
annotationEditingActive = false;

toolbarCoordinatorLayout.removeContextualToolbar(true);
annotationEditingToolbar.unbindController();
public void onDisplayContextualToolbar(@NonNull ContextualToolbar contextualToolbar) {
if (contextualToolbar instanceof AnnotationCreationToolbar) {
annotationCreationActive = true;
}
if (contextualToolbar instanceof AnnotationEditingToolbar) {
annotationEditingActive = true;
}

annotationEditingInspectorController.unbindAnnotationEditingController();
parent.manuallyLayoutChildren();
parent.updateState();
}

@Override
public void onEnterTextSelectionMode(@NonNull TextSelectionController controller) {
textSelectionActive = true;
public void onRemoveContextualToolbar(@NonNull ContextualToolbar contextualToolbar) {
if (contextualToolbar instanceof AnnotationCreationToolbar) {
annotationCreationActive = false;
}
if (contextualToolbar instanceof AnnotationEditingToolbar) {
annotationEditingActive = false;
}

parent.updateState();
}

@Override
public void onExitTextSelectionMode(@NonNull TextSelectionController controller) {
textSelectionActive = false;
public void onPrepareFormEditingBar(@NonNull FormEditingBar formEditingBar) {
// Not required.
}

@Override
public void onEnterFormElementEditingMode(@NonNull FormEditingController controller) {
public void onDisplayFormEditingBar(@NonNull FormEditingBar formEditingBar) {
formEditingActive = true;

formEditingInspectorController.bindFormEditingController(controller);
formEditingBar.bindController(controller);
parent.manuallyLayoutChildren();
parent.updateState();
}

@Override
public void onChangeFormElementEditingMode(@NonNull FormEditingController controller) {

}

@Override
public void onExitFormElementEditingMode(@NonNull FormEditingController controller) {
public void onRemoveFormEditingBar(@NonNull FormEditingBar formEditingBar) {
formEditingActive = false;

formEditingInspectorController.unbindFormEditingController();
formEditingBar.unbindController();
parent.manuallyLayoutChildren();
parent.updateState();
}

void resetToolbars() {
toolbarCoordinatorLayout.removeContextualToolbar(false);
annotationCreationToolbar.unbindController();
annotationCreationActive = false;
annotationCreationInspectorController.unbindAnnotationCreationController();
annotationEditingToolbar.unbindController();
annotationEditingInspectorController.unbindAnnotationEditingController();
formEditingInspectorController.unbindFormEditingController();
formEditingBar.unbindController();
}

boolean isAnnotationCreationActive() {
return annotationCreationActive;
}

boolean isAnnotationEditingActive() {
return annotationEditingActive;
}

boolean isTextSelectionActive() {
return textSelectionActive;
}

boolean isFormEditingActive() {
return formEditingActive;
}
}
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.26.8",
"version": "1.27.0",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down
Loading

0 comments on commit c0c3dd9

Please sign in to comment.