Skip to content

Commit 25df426

Browse files
authored
Merge pull request #72 from PSPDFKit/akshat/dismiss-callback-example
Added dismiss callback and its implementation in the example.
2 parents 301befb + 3bf1f38 commit 25df426

File tree

6 files changed

+137
-11
lines changed

6 files changed

+137
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
///
2+
/// Copyright © 2021 PSPDFKit GmbH. All rights reserved.
3+
///
4+
/// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
5+
/// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
6+
/// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
7+
/// This notice may not be removed from this file.
8+
///
9+
10+
package com.pspdfkit.flutter.pspdfkit;
11+
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
15+
import io.flutter.plugin.common.MethodChannel;
16+
17+
/**
18+
* Internal singleton class used to communicate between activities and the PSPDFKit Flutter plugin.
19+
*/
20+
public class EventDispatcher {
21+
@Nullable
22+
private static EventDispatcher instance;
23+
/**
24+
* A channel for sending events from Java to Flutter. This is set as soon as
25+
* the plugin is registered.
26+
*/
27+
@Nullable
28+
private MethodChannel channel = null;
29+
30+
private EventDispatcher() {
31+
}
32+
33+
@NonNull
34+
public static synchronized EventDispatcher getInstance() {
35+
if (instance == null) {
36+
instance = new EventDispatcher();
37+
}
38+
return instance;
39+
}
40+
41+
public void setChannel(@Nullable MethodChannel channel) {
42+
this.channel = channel;
43+
}
44+
45+
public void notifyActivityOnPause() {
46+
sendEvent("flutterPdfActivityOnPause");
47+
}
48+
49+
private void sendEvent(@NonNull final String method) {
50+
if (channel != null) {
51+
channel.invokeMethod(method, null, null);
52+
}
53+
}
54+
}

android/src/main/java/com/pspdfkit/flutter/pspdfkit/FlutterPdfActivity.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.os.Bundle;
44

55
import androidx.annotation.NonNull;
6+
import androidx.annotation.Nullable;
67

78
import com.pspdfkit.document.PdfDocument;
89
import com.pspdfkit.ui.PdfActivity;
@@ -17,8 +18,8 @@
1718
*/
1819
public class FlutterPdfActivity extends PdfActivity {
1920

20-
private static FlutterPdfActivity currentActivity;
21-
private static AtomicReference<Result> loadedDocumentResult = new AtomicReference<>();
21+
@Nullable private static FlutterPdfActivity currentActivity;
22+
@NonNull private static final AtomicReference<Result> loadedDocumentResult = new AtomicReference<>();
2223

2324
public static void setLoadedDocumentResult(Result result) {
2425
loadedDocumentResult.set(result);
@@ -30,6 +31,13 @@ protected void onCreate(Bundle bundle) {
3031
bindActivity();
3132
}
3233

34+
@Override
35+
protected void onPause() {
36+
// Notify the Flutter PSPDFKit plugin that the activity is going to enter the onPause state.
37+
EventDispatcher.getInstance().notifyActivityOnPause();
38+
super.onPause();
39+
}
40+
3341
@Override
3442
protected void onDestroy() {
3543
super.onDestroy();
@@ -66,6 +74,7 @@ private void releaseActivity() {
6674
currentActivity = null;
6775
}
6876

77+
@Nullable
6978
public static FlutterPdfActivity getCurrentActivity() {
7079
return currentActivity;
7180
}

android/src/main/java/com/pspdfkit/flutter/pspdfkit/PspdfkitPlugin.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@
5959
* PSPDFKit plugin to load PDF and image documents.
6060
*/
6161
public class PspdfkitPlugin implements MethodCallHandler, PluginRegistry.RequestPermissionsResultListener {
62+
@NonNull private static final EventDispatcher eventDispatcher = EventDispatcher.getInstance();
6263
private static final String LOG_TAG = "PSPDFKitPlugin";
6364
private static final String FILE_SCHEME = "file:///";
64-
private final Context context;
65-
private final Registrar registrar;
65+
@NonNull private final Context context;
66+
@NonNull private final Registrar registrar;
6667
/** Atomic reference that prevents sending twice the permission result and throwing exception. */
67-
private AtomicReference<Result> permissionRequestResult;
68+
@NonNull private final AtomicReference<Result> permissionRequestResult;
6869

6970
private PspdfkitPlugin(Registrar registrar) {
7071
this.context = registrar.activeContext();
@@ -79,13 +80,13 @@ public static void registerWith(Registrar registrar) {
7980
final MethodChannel channel = new MethodChannel(registrar.messenger(), "pspdfkit");
8081
PspdfkitPlugin pspdfkitPlugin = new PspdfkitPlugin(registrar);
8182
channel.setMethodCallHandler(pspdfkitPlugin);
83+
eventDispatcher.setChannel(channel);
8284
registrar.addRequestPermissionsResultListener(pspdfkitPlugin);
8385
}
8486

8587
@Override
8688
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
8789
String fullyQualifiedName;
88-
FlutterPdfActivity flutterPdfActivity;
8990
PdfDocument document;
9091

9192
switch (call.method) {

example/lib/main.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,24 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
268268
Pspdfkit.setLicenseKey("LICENSE_KEY_GOES_HERE");
269269
}
270270

271+
void flutterPdfActivityOnPauseHandler() {
272+
print("flutterPdfActivityOnPauseHandler");
273+
}
274+
275+
void pdfViewControllerWillDismissHandler() {
276+
print("pdfViewControllerWillDismissHandler");
277+
}
278+
279+
void pdfViewControllerDidDismissHandler() {
280+
print("pdfViewControllerDidDismissHandler");
281+
}
282+
271283
@override
272284
Widget build(BuildContext context) {
285+
Pspdfkit.flutterPdfActivityOnPause = () => flutterPdfActivityOnPauseHandler();
286+
Pspdfkit.pdfViewControllerWillDismiss = () => pdfViewControllerWillDismissHandler();
287+
Pspdfkit.pdfViewControllerDidDismiss = () => pdfViewControllerDidDismissHandler();
288+
273289
currentTheme = MediaQuery.of(context).platformBrightness == Brightness.light ? lightTheme : darkTheme;
274290
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
275291
if (isIOS) {

ios/Classes/PspdfkitPlugin.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
@import PSPDFKit;
1212
@import PSPDFKitUI;
1313

14-
@interface PspdfkitPlugin()
14+
static FlutterMethodChannel *channel;
15+
16+
@interface PspdfkitPlugin() <PSPDFViewControllerDelegate>
1517
@property (nonatomic) PSPDFViewController *pdfViewController;
1618
@end
1719

1820
@implementation PspdfkitPlugin
1921
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
20-
FlutterMethodChannel* channel = [FlutterMethodChannel
21-
methodChannelWithName:@"pspdfkit"
22-
binaryMessenger:[registrar messenger]];
22+
channel = [FlutterMethodChannel
23+
methodChannelWithName:@"pspdfkit"
24+
binaryMessenger:[registrar messenger]];
2325
PspdfkitPlugin* instance = [[PspdfkitPlugin alloc] init];
2426
[registrar addMethodCallDelegate:instance channel:channel];
2527
}
@@ -88,6 +90,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
8890
self.pdfViewController = [[PSPDFViewController alloc] initWithDocument:document configuration:psPdfConfiguration];
8991
self.pdfViewController.appearanceModeManager.appearanceMode = [self appearanceMode:configurationDictionary];
9092
self.pdfViewController.pageIndex = [self pageIndex:configurationDictionary];
93+
self.pdfViewController.delegate = self;
9194

9295
if ((id)configurationDictionary != NSNull.null) {
9396
[self setLeftBarButtonItems:configurationDictionary[@"leftBarButtonItems"]];
@@ -175,6 +178,15 @@ - (PSPDFConfiguration *)configuration:(NSDictionary *)dictionary isImageDocument
175178
}];
176179
}
177180

181+
#pragma mark - PSPDFViewControllerDelegate
182+
183+
- (void)pdfViewControllerWillDismiss:(PSPDFViewController *)pdfController {
184+
[channel invokeMethod:@"pdfViewControllerWillDismiss" arguments:nil];
185+
}
186+
187+
- (void)pdfViewControllerDidDismiss:(PSPDFViewController *)pdfController {
188+
[channel invokeMethod:@"pdfViewControllerDidDismiss" arguments:nil];
189+
}
178190

179191
#pragma mark - Customize the Toolbar
180192

lib/pspdfkit.dart

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
library pspdfkit;
1010

1111
import 'dart:async';
12+
import 'dart:ui';
1213

1314
import 'package:flutter/services.dart';
1415

@@ -17,7 +18,15 @@ part 'configuration_options.dart';
1718

1819
/// PSPDFKit plugin to load PDF and image documents on both platform iOS and Android.
1920
class Pspdfkit {
20-
static const MethodChannel _channel = const MethodChannel('pspdfkit');
21+
static MethodChannel _privateChannel = null;
22+
23+
static MethodChannel get _channel {
24+
if (_privateChannel == null) {
25+
_privateChannel = const MethodChannel('pspdfkit');
26+
_privateChannel.setMethodCallHandler(_platformCallHandler);
27+
}
28+
return _privateChannel;
29+
}
2130

2231
/// Gets the PSPDFKit framework version.
2332
static Future<String> get frameworkVersion async =>
@@ -93,4 +102,29 @@ class Pspdfkit {
93102
return AndroidPermissionStatus.notDetermined;
94103
}
95104
}
105+
106+
static VoidCallback flutterPdfActivityOnPause;
107+
static VoidCallback pdfViewControllerWillDismiss;
108+
static VoidCallback pdfViewControllerDidDismiss;
109+
110+
static Future<void> _platformCallHandler(MethodCall call) {
111+
try {
112+
switch (call.method) {
113+
case 'flutterPdfActivityOnPause':
114+
flutterPdfActivityOnPause();
115+
break;
116+
case 'pdfViewControllerWillDismiss':
117+
pdfViewControllerWillDismiss();
118+
break;
119+
case 'pdfViewControllerDidDismiss':
120+
pdfViewControllerDidDismiss();
121+
break;
122+
default:
123+
print('Unknowm method ${call.method} ');
124+
}
125+
} catch (e) {
126+
print(e);
127+
}
128+
return Future.value();
129+
}
96130
}

0 commit comments

Comments
 (0)