Skip to content

Commit 6c126db

Browse files
committed
Introduce event dispatcher
1 parent 232a24d commit 6c126db

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.pspdfkit.flutter.pspdfkit;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
6+
import io.flutter.plugin.common.MethodChannel;
7+
8+
/**
9+
* Internal singleton class used to communicate between activities and the PSPDFKit Flutter plugin.
10+
*/
11+
public class EventDispatcher {
12+
@Nullable
13+
private static EventDispatcher instance;
14+
/**
15+
* A channel for sending events from Java to Flutter. This is set as soon as
16+
* the plugin is registered.
17+
*/
18+
@Nullable
19+
private MethodChannel channel = null;
20+
21+
private EventDispatcher() {
22+
}
23+
24+
@NonNull
25+
public static synchronized EventDispatcher getInstance() {
26+
if (instance == null) {
27+
instance = new EventDispatcher();
28+
}
29+
return instance;
30+
}
31+
32+
public void setChannel(@Nullable MethodChannel channel) {
33+
this.channel = channel;
34+
}
35+
36+
public void notifyActivityOnPause() {
37+
sendEvent("flutterPdfActivityOnPause");
38+
}
39+
40+
private void sendEvent(@NonNull final String method) {
41+
if (channel != null) {
42+
channel.invokeMethod(method, null, null);
43+
}
44+
}
45+
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
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;
910

1011
import java.util.concurrent.atomic.AtomicReference;
1112

12-
import io.flutter.plugin.common.MethodChannel;
13-
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1413
import io.flutter.plugin.common.MethodChannel.Result;
1514

1615
/**
@@ -19,18 +18,13 @@
1918
*/
2019
public class FlutterPdfActivity extends PdfActivity {
2120

22-
private static FlutterPdfActivity currentActivity;
23-
private static MethodChannel channel;
24-
private static AtomicReference<Result> loadedDocumentResult = new AtomicReference<>();
21+
@Nullable private static FlutterPdfActivity currentActivity;
22+
@NonNull private static final AtomicReference<Result> loadedDocumentResult = new AtomicReference<>();
2523

2624
public static void setLoadedDocumentResult(Result result) {
2725
loadedDocumentResult.set(result);
2826
}
2927

30-
public static void setMethodChannel(MethodChannel methodChannel) {
31-
channel = methodChannel;
32-
}
33-
3428
@Override
3529
protected void onCreate(Bundle bundle) {
3630
super.onCreate(bundle);
@@ -39,8 +33,9 @@ protected void onCreate(Bundle bundle) {
3933

4034
@Override
4135
protected void onPause() {
36+
// Notify the Flutter PSPDFKit plugin that the activity is going to enter the onPause state.
37+
EventDispatcher.getInstance().notifyActivityOnPause();
4238
super.onPause();
43-
channel.invokeMethod("flutterPdfActivityOnPause", null, null);
4439
}
4540

4641
@Override
@@ -79,6 +74,7 @@ private void releaseActivity() {
7974
currentActivity = null;
8075
}
8176

77+
@Nullable
8278
public static FlutterPdfActivity getCurrentActivity() {
8379
return currentActivity;
8480
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +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;
66-
private static MethodChannel channel;
65+
@NonNull private final Context context;
66+
@NonNull private final Registrar registrar;
6767
/** Atomic reference that prevents sending twice the permission result and throwing exception. */
68-
private AtomicReference<Result> permissionRequestResult;
68+
@NonNull private final AtomicReference<Result> permissionRequestResult;
6969

7070
private PspdfkitPlugin(Registrar registrar) {
7171
this.context = registrar.activeContext();
@@ -77,20 +77,18 @@ private PspdfkitPlugin(Registrar registrar) {
7777
* Plugin registration.
7878
*/
7979
public static void registerWith(Registrar registrar) {
80-
channel = new MethodChannel(registrar.messenger(), "pspdfkit");
80+
MethodChannel channel = new MethodChannel(registrar.messenger(), "pspdfkit");
8181
PspdfkitPlugin pspdfkitPlugin = new PspdfkitPlugin(registrar);
8282
channel.setMethodCallHandler(pspdfkitPlugin);
83+
eventDispatcher.setChannel(channel);
8384
registrar.addRequestPermissionsResultListener(pspdfkitPlugin);
8485
}
8586

8687
@Override
8788
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
8889
String fullyQualifiedName;
89-
FlutterPdfActivity flutterPdfActivity;
9090
PdfDocument document;
9191

92-
FlutterPdfActivity.setMethodChannel(channel);
93-
9492
switch (call.method) {
9593
case "frameworkVersion":
9694
result.success("Android " + PSPDFKit.VERSION);

0 commit comments

Comments
 (0)