Skip to content

Commit 70b548d

Browse files
author
irgendeinich
committed
Implement getAllAnnotations() on Android
1 parent caa59b8 commit 70b548d

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

android/src/main/java/com/pspdfkit/react/ReactPdfViewManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pspdfkit.react;
22

33
import android.app.Activity;
4+
45
import androidx.annotation.NonNull;
56
import androidx.fragment.app.FragmentActivity;
67

@@ -51,6 +52,7 @@ public class ReactPdfViewManager extends ViewGroupManager<PdfView> {
5152
public static final int COMMAND_GET_FORM_FIELD_VALUE = 8;
5253
public static final int COMMAND_SET_FORM_FIELD_VALUE = 9;
5354
public static final int COMMAND_REMOVE_ANNOTATION = 10;
55+
public static final int COMMAND_GET_ALL_ANNOTATIONS = 11;
5456

5557
private CompositeDisposable annotationDisposables = new CompositeDisposable();
5658

@@ -100,6 +102,7 @@ public Map<String, Integer> getCommandsMap() {
100102
commandMap.put("getFormFieldValue", COMMAND_GET_FORM_FIELD_VALUE);
101103
commandMap.put("setFormFieldValue", COMMAND_SET_FORM_FIELD_VALUE);
102104
commandMap.put("removeAnnotation", COMMAND_REMOVE_ANNOTATION);
105+
commandMap.put("getAllAnnotations", COMMAND_GET_ALL_ANNOTATIONS);
103106
return commandMap;
104107
}
105108

@@ -185,6 +188,19 @@ public void accept(List<Annotation> annotations) {
185188
annotationDisposables.add(annotationDisposable);
186189
}
187190
break;
191+
case COMMAND_GET_ALL_ANNOTATIONS:
192+
if (args != null && args.size() == 2) {
193+
final int requestId = args.getInt(0);
194+
annotationDisposables.add(root.getAllAnnotations(args.getString(1))
195+
.subscribeOn(Schedulers.io())
196+
.observeOn(AndroidSchedulers.mainThread())
197+
.subscribe(annotations -> {
198+
root.getEventDispatcher().dispatchEvent(new PdfViewDataReturnedEvent(root.getId(), requestId, annotations));
199+
}, throwable -> {
200+
root.getEventDispatcher().dispatchEvent(new PdfViewDataReturnedEvent(root.getId(), requestId, throwable));
201+
}));
202+
}
203+
break;
188204
case COMMAND_ADD_ANNOTATION:
189205
if (args != null && args.size() == 2) {
190206
final int requestId = args.getInt(0);

android/src/main/java/com/pspdfkit/views/PdfView.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import android.content.Context;
44
import android.net.Uri;
5-
import androidx.annotation.NonNull;
6-
import androidx.annotation.Nullable;
7-
import androidx.fragment.app.FragmentManager;
8-
95
import android.util.AttributeSet;
106
import android.util.Pair;
117
import android.view.Choreographer;
128
import android.view.Gravity;
139
import android.view.View;
1410
import android.widget.FrameLayout;
1511

12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
import androidx.fragment.app.FragmentManager;
15+
1616
import com.facebook.react.bridge.ReadableMap;
1717
import com.facebook.react.uimanager.events.EventDispatcher;
1818
import com.pspdfkit.annotations.Annotation;
@@ -30,7 +30,6 @@
3030
import com.pspdfkit.forms.TextFormElement;
3131
import com.pspdfkit.listeners.OnPreparePopupToolbarListener;
3232
import com.pspdfkit.listeners.SimpleDocumentListener;
33-
import com.pspdfkit.react.R;
3433
import com.pspdfkit.react.events.PdfViewDataReturnedEvent;
3534
import com.pspdfkit.react.events.PdfViewDocumentLoadFailedEvent;
3635
import com.pspdfkit.react.events.PdfViewDocumentSaveFailedEvent;
@@ -449,11 +448,17 @@ public PdfDocument apply(PdfFragment pdfFragment) {
449448
}).flatMap(new Function<PdfDocument, ObservableSource<Annotation>>() {
450449
@Override
451450
public ObservableSource<Annotation> apply(PdfDocument pdfDocument) {
452-
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfType(getTypeFromString(type), pageIndex, 1);
451+
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type), pageIndex, 1);
453452
}
454453
}).toList();
455454
}
456455

456+
public Single<List<Annotation>> getAllAnnotations(@Nullable final String type) {
457+
return fragmentGetter.take(1).map(PdfFragment::getDocument)
458+
.flatMap(pdfDocument -> pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type)))
459+
.toList();
460+
}
461+
457462
private EnumSet<AnnotationType> getTypeFromString(@Nullable String type) {
458463
if (type == null) {
459464
return EnumSet.allOf(AnnotationType.class);

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,22 @@ class PSPDFKitView extends React.Component {
286286
*/
287287
getAllAnnotations = function(type) {
288288
if (Platform.OS === "android") {
289-
//TODO: Implement Android here.
289+
let requestId = this._nextRequestId++;
290+
let requestMap = this._requestMap;
291+
292+
// We create a promise here that will be resolved once onDataReturned is called.
293+
let promise = new Promise(function(resolve, reject) {
294+
requestMap[requestId] = { resolve: resolve, reject: reject };
295+
});
296+
297+
UIManager.dispatchViewManagerCommand(
298+
findNodeHandle(this.refs.pdfView),
299+
this._getViewManagerConfig("RCTPSPDFKitView").Commands
300+
.getAllAnnotations,
301+
[requestId, type]
302+
);
303+
304+
return promise;
290305
} else if (Platform.OS === "ios") {
291306
return NativeModules.PSPDFKitViewManager.getAllAnnotations(
292307
type,

samples/Catalog/Catalog.android.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,8 @@ class PdfViewInstantJsonScreen extends Component<{}> {
588588
<View>
589589
<Button
590590
onPress={() => {
591-
// This gets all annotations on the first page.
592-
this.refs.pdfView.getAnnotations(0, null).then(annotations => {
591+
// This gets all annotations in the document.
592+
this.refs.pdfView.getAllAnnotations().then(annotations => {
593593
alert(JSON.stringify(annotations));
594594
});
595595
}}

0 commit comments

Comments
 (0)