Skip to content

Exposed a few additional APIs #89

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
buildscript {
apply from: 'config.gradle'

ext {
kotlinVersion = "1.5.20"
}

repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:$androidGradlePluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

Expand All @@ -39,7 +44,7 @@ android {
namespace "com.pspdfkit.flutter.pspdfkit"

compileSdk androidCompileSdkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ConfigurationAdapter {
private static final String ENABLE_DOCUMENT_EDITOR = "enableDocumentEditor";
private static final String DARK_THEME_RESOURCE = "darkThemeResource";
private static final String DEFAULT_THEME_RESOURCE = "defaultThemeResource";
private static final String RESTORE_LAST_VIEWED_PAGE = "restoreLastViewedPage";

// Thumbnail Options
private static final String SHOW_THUMBNAIL_BAR = "showThumbnailBar";
Expand Down Expand Up @@ -400,6 +401,11 @@ class ConfigurationAdapter {
double value = (double) configurationMap.get(key);
configuration.startZoomScale((float) value);
}

key = getKeyOfType(configurationMap, RESTORE_LAST_VIEWED_PAGE, Boolean.class);
if (key != null) {
configureRestoreLastViewedPage((Boolean) configurationMap.get(key));
}
}
}

Expand Down Expand Up @@ -761,6 +767,10 @@ private void configureMeasurementToolSnappingEnabled(Context context,Boolean aBo
PSPDFKitPreferences.get(context).setMeasurementSnappingEnabled(aBoolean);
}

private void configureRestoreLastViewedPage(boolean restoreLastViewedPage) {
configuration.restoreLastViewedPage(restoreLastViewedPage);
}

private <T> boolean containsKeyOfType(@NonNull HashMap<String, Object> configurationMap,
@NonNull String key,
@NonNull Class<T> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package com.pspdfkit.flutter.pspdfkit
import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.pspdfkit.annotations.AnnotationProvider
import com.pspdfkit.document.PdfDocument
import com.pspdfkit.flutter.pspdfkit.document.FlutterPdfDocument
import com.pspdfkit.flutter.pspdfkit.util.MeasurementHelper
import com.pspdfkit.listeners.DocumentListener
import com.pspdfkit.ui.PdfFragment
import com.pspdfkit.annotations.Annotation
import com.pspdfkit.flutter.pspdfkit.document.FlutterPdfDocument
import com.pspdfkit.ui.special_mode.controller.AnnotationCreationController
import com.pspdfkit.ui.special_mode.manager.AnnotationManager
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel

class FlutterPdfUiFragmentCallbacks(
private val methodChannel: MethodChannel, private val measurementConfigurations:
List<Map<String, Any>>?,
private val binaryMessenger: BinaryMessenger
) : FragmentManager.FragmentLifecycleCallbacks(), DocumentListener {
) : FragmentManager.FragmentLifecycleCallbacks(), DocumentListener,
AnnotationManager.OnAnnotationCreationModeChangeListener {

private var pdfFragment: PdfFragment? = null
private var flutterPdfDocument: FlutterPdfDocument? = null
Expand All @@ -35,6 +40,7 @@ class FlutterPdfUiFragmentCallbacks(
}
pdfFragment = f as PdfFragment
pdfFragment?.addDocumentListener(this)
pdfFragment?.addOnAnnotationCreationModeChangeListener(this)
}
}

Expand Down Expand Up @@ -85,4 +91,16 @@ class FlutterPdfUiFragmentCallbacks(
}
}
}

override fun onEnterAnnotationCreationMode(annotationCreationController: AnnotationCreationController) {

}

override fun onChangeAnnotationCreationMode(annotationCreationController: AnnotationCreationController) {

}

override fun onExitAnnotationCreationMode(annotationCreationController: AnnotationCreationController) {
methodChannel.invokeMethod("onExitAnnotationCreationMode", null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import io.reactivex.rxjava3.subscribers.DisposableSubscriber
import org.json.JSONObject
import java.io.ByteArrayOutputStream
import java.io.File
import com.pspdfkit.preferences.PSPDFKitPreferences
import com.pspdfkit.ui.special_mode.controller.AnnotationTool
import com.pspdfkit.ui.special_mode.controller.AnnotationToolVariant

internal class PSPDFKitView(
val context: Context,
Expand Down Expand Up @@ -597,6 +600,23 @@ internal class PSPDFKitView(
}
})
}
"jumpToPage" -> {
val pageIndex: Int = requireNotNull(call.argument("pageIndex"))
pdfUiFragment.pageIndex = pageIndex
result.success(true)
}

"isShowingTwoPages" -> {
val pageIndex = pdfUiFragment.pdfFragment?.pageIndex ?: -1
result.success(pdfUiFragment.pdfFragment?.getSiblingPageIndex(pageIndex) != -1)
}

"enterAnnotationCreationMode" -> {
val authorName: String = requireNotNull(call.argument("authorName"))
PSPDFKitPreferences.get(context).setAnnotationCreator(authorName)
pdfUiFragment.pdfFragment?.enterAnnotationCreationMode(AnnotationTool.INK, AnnotationToolVariant.fromPreset(AnnotationToolVariant.Preset.PEN))
result.success(true)
}
else -> result.notImplemented()
}
}
Expand Down
10 changes: 9 additions & 1 deletion ios/Classes/PspdfPlatformView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@import PSPDFKit;
@import PSPDFKitUI;

@interface PspdfPlatformView() <PSPDFViewControllerDelegate>
@interface PspdfPlatformView() <PSPDFViewControllerDelegate, PSPDFFlexibleToolbarDelegate>
@property int64_t platformViewId;
@property (nonatomic) FlutterMethodChannel *channel;
@property (nonatomic) FlutterMethodChannel *broadcastChannel;
Expand Down Expand Up @@ -70,6 +70,7 @@ - (instancetype)initWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId argum
_pdfViewController.appearanceModeManager.appearanceMode = [PspdfkitFlutterConverter appearanceMode:configurationDictionary];
_pdfViewController.pageIndex = [PspdfkitFlutterConverter pageIndex:configurationDictionary];
_pdfViewController.delegate = self;
_pdfViewController.annotationToolbarController.toolbar.toolbarDelegate = self;

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(documentDidFinishRendering) name:PSPDFDocumentViewControllerDidConfigureSpreadViewNotification object:nil];

Expand Down Expand Up @@ -169,4 +170,11 @@ - (void)pdfViewControllerDidDismiss:(PSPDFViewController *)pdfController {
[self cleanup];
}

# pragma mark - PSPDFFlexibleToolbarDelegate

- (void)flexibleToolbarDidHide:(PSPDFFlexibleToolbar *)toolbar {
// Handle annotation toolbar being hidden.
[_channel invokeMethod:@"onExitAnnotationCreationMode" arguments: nil];
}

@end
36 changes: 35 additions & 1 deletion ios/Classes/PspdfkitFlutterHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,41 @@ + (void)processMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
result(nil);
} else if ([@"getZoomScale" isEqualToString:call.method]) {
result(FlutterMethodNotImplemented);
}else {
} else if ([@"jumpToPage" isEqualToString:call.method]) {
@try {
PSPDFPageIndex pageIndex = [call.arguments[@"pageIndex"] longLongValue];
[pdfViewController setPageIndex:pageIndex animated:YES];
result(@(YES));
} @catch (NSException *exception) {
result([FlutterError errorWithCode:@"" message:exception.reason details:nil]);
}
} else if ([@"isShowingTwoPages" isEqualToString:call.method]) {
BOOL showingTwoPages = pdfViewController.documentViewController.layout.spreadMode != PSPDFDocumentViewLayoutSpreadModeSingle;
result(@(showingTwoPages));
} else if([@"enterAnnotationCreationMode" isEqualToString:call.method]){
@try {
NSString *authorName = call.arguments[@"authorName"];
PSPDFUsernameHelper.defaultAnnotationUsername = authorName;

PSPDFDocument *document = pdfViewController.document;
if (!document || !document.isValid) {
result([FlutterError errorWithCode:@"" message:@"PDF document not found or is invalid." details:nil]);
return;
}
document.defaultAnnotationUsername = authorName;

[pdfViewController.annotationToolbarController updateHostView:nil container:nil viewController:pdfViewController];

[pdfViewController.annotationToolbarController showToolbarAnimated:YES completion:^(BOOL finished) {
if (finished) {
[pdfViewController.annotationStateManager setState:PSPDFAnnotationStringInk variant:PSPDFAnnotationVariantStringInkPen];
}
}];
result(@(YES));
} @catch (NSException *exception) {
result([FlutterError errorWithCode:@"" message:exception.reason details:nil]);
}
} else {
result(FlutterMethodNotImplemented);
}
}
Expand Down
108 changes: 58 additions & 50 deletions lib/src/pdf_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,55 +174,62 @@ class PdfConfiguration {
/// Sets the minimum zoom scale. Defaults to null.
final double? minimumZoomScale;

PdfConfiguration(
{this.scrollDirection,
this.pageTransition,
this.enableTextSelection,
this.disableAutosave,
this.pageLayoutMode,
this.spreadFitting,
this.showPageLabels,
this.startPage,
this.documentLabelEnabled,
this.firstPageAlwaysSingle,
this.invertColors,
this.password,
this.androidGrayScale,
this.inlineSearch,
this.toolbarTitle,
this.showActionNavigationButtons,
this.userInterfaceViewMode,
this.immersiveMode,
this.appearanceMode,
this.settingsMenuItems,
this.androidShowSearchAction,
this.androidShowOutlineAction,
this.androidShowBookmarksAction,
this.androidEnableDocumentEditor,
this.androidShowShareAction,
this.androidShowPrintAction,
this.androidShowDocumentInfoView,
this.androidShowAnnotationListAction,
this.androidDarkThemeResource,
this.androidDefaultThemeResource,
this.iOSLeftBarButtonItems,
this.iOSRightBarButtonItems,
this.iOSAllowToolbarTitleChange,
this.showThumbnailBar,
this.androidShowThumbnailGridAction,
this.enableAnnotationEditing,
this.enableInstantComments,
this.webConfiguration,
this.editableAnnotationTypes,
this.toolbarMenuItems,
this.enableMeasurementTools,
this.measurementSnappingEnabled,
this.enableMagnifier,
this.measurementValueConfigurations,
this.annotationToolsGrouping,
this.defaultZoomScale,
this.maximumZoomScale,
this.minimumZoomScale});
/// Enables or disables restoration of the last viewed page when reopening a document.
/// If true, the last viewed page will be restored when opening the document a second time.
/// If false, the document will be opened at page index 0. Defaults to true.
final bool restoreLastViewedPage;

PdfConfiguration({
this.scrollDirection,
this.pageTransition,
this.enableTextSelection,
this.disableAutosave,
this.pageLayoutMode,
this.spreadFitting,
this.showPageLabels,
this.startPage,
this.documentLabelEnabled,
this.firstPageAlwaysSingle,
this.invertColors,
this.password,
this.androidGrayScale,
this.inlineSearch,
this.toolbarTitle,
this.showActionNavigationButtons,
this.userInterfaceViewMode,
this.immersiveMode,
this.appearanceMode,
this.settingsMenuItems,
this.androidShowSearchAction,
this.androidShowOutlineAction,
this.androidShowBookmarksAction,
this.androidEnableDocumentEditor,
this.androidShowShareAction,
this.androidShowPrintAction,
this.androidShowDocumentInfoView,
this.androidShowAnnotationListAction,
this.androidDarkThemeResource,
this.androidDefaultThemeResource,
this.iOSLeftBarButtonItems,
this.iOSRightBarButtonItems,
this.iOSAllowToolbarTitleChange,
this.showThumbnailBar,
this.androidShowThumbnailGridAction,
this.enableAnnotationEditing,
this.enableInstantComments,
this.webConfiguration,
this.editableAnnotationTypes,
this.toolbarMenuItems,
this.enableMeasurementTools,
this.measurementSnappingEnabled,
this.enableMagnifier,
this.measurementValueConfigurations,
this.annotationToolsGrouping,
this.defaultZoomScale,
this.maximumZoomScale,
this.minimumZoomScale,
this.restoreLastViewedPage = true,
});

/// Returns a [Map] representation of the [PdfConfiguration] object.
/// This is used to pass the configuration to the platform side.
Expand Down Expand Up @@ -273,7 +280,8 @@ class PdfConfiguration {
'toolbarItemGrouping': convertAnnotationToolsGrouping(),
'defaultZoomScale': defaultZoomScale,
'maximumZoomScale': maximumZoomScale,
'minimumZoomScale': minimumZoomScale
'minimumZoomScale': minimumZoomScale,
'restoreLastViewedPage': restoreLastViewedPage,
}..removeWhere((key, value) => value == null);
}

Expand Down
11 changes: 8 additions & 3 deletions lib/src/widgets/pspdfkit_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
library pspdfkit_widget;

import 'dart:async';
import 'package:flutter/services.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:pspdfkit_flutter/pspdfkit.dart';

import 'pspdfkit_widget_controller_native.dart';

class PspdfkitWidget extends StatefulWidget {
Expand All @@ -25,6 +27,7 @@ class PspdfkitWidget extends StatefulWidget {
final PdfDocumentLoadedCallback? onPdfDocumentLoaded;
final PdfDocumentLoadFailedCallback? onPdfDocumentError;
final PageChangedCallback? onPageChanged;
final VoidCallback? onExitAnnotationCreationMode;

const PspdfkitWidget({
Key? key,
Expand All @@ -34,6 +37,7 @@ class PspdfkitWidget extends StatefulWidget {
this.onPdfDocumentLoaded,
this.onPdfDocumentError,
this.onPageChanged,
this.onExitAnnotationCreationMode,
}) : super(key: key);

@override
Expand Down Expand Up @@ -118,6 +122,7 @@ class _PspdfkitWidgetState extends State<PspdfkitWidget> {
onPageChanged: widget.onPageChanged,
onPdfDocumentLoadFailed: widget.onPdfDocumentError,
onPdfDocumentLoaded: widget.onPdfDocumentLoaded,
onExitAnnotationCreationMode: widget.onExitAnnotationCreationMode,
);
widget.onPspdfkitWidgetCreated?.call(controller);
}
Expand Down
Loading