Skip to content

Commit 6ce0179

Browse files
authored
Merge pull request #264 from PSPDFKit/rad/getAllAnnotations
Implement `getAllAnnotations()`
2 parents 17e895f + 5ee5f40 commit 6ce0179

File tree

11 files changed

+109
-11
lines changed

11 files changed

+109
-11
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,40 @@ class PSPDFKitView extends React.Component {
276276
}
277277
};
278278

279+
/**
280+
* Gets all annotations of the given type.
281+
*
282+
* @param type The type of annotations to get (See here for types https://pspdfkit.com/guides/server/current/api/json-format/) or null to get all annotations.
283+
*
284+
* Returns a promise resolving an array with the following structure:
285+
* {'annotations' : [instantJson]}
286+
*/
287+
getAllAnnotations = function(type) {
288+
if (Platform.OS === "android") {
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;
305+
} else if (Platform.OS === "ios") {
306+
return NativeModules.PSPDFKitViewManager.getAllAnnotations(
307+
type,
308+
findNodeHandle(this.refs.pdfView)
309+
);
310+
}
311+
};
312+
279313
/**
280314
* Applies the passed in document instant json.
281315
*

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error;
4444
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
4545
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error;
46+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error;
4647
- (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error;
4748

4849
/// Forms

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
247247
return annotationsJSON;
248248
}
249249

250+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
251+
PSPDFDocument *document = self.pdfController.document;
252+
VALIDATE_DOCUMENT(document, nil)
253+
254+
NSArray<PSPDFAnnotation *> *annotations = [[document allAnnotationsOfType:type].allValues valueForKeyPath:@"@unionOfArrays.self"];
255+
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
256+
return @{@"annotations" : annotationsJSON};
257+
}
258+
250259
- (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
251260
NSData *data;
252261
if ([jsonAnnotations isKindOfClass:NSString.class]) {

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ @implementation RCTPSPDFKitViewManager
195195
});
196196
}
197197

198+
RCT_EXPORT_METHOD(getAllAnnotations:(NSString *)type reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199+
dispatch_async(dispatch_get_main_queue(), ^{
200+
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
201+
NSError *error;
202+
NSDictionary *annotations = [component getAllAnnotations:[RCTConvert annotationTypeFromInstantJSONType:type] error:&error];
203+
if (annotations) {
204+
resolve(annotations);
205+
} else {
206+
reject(@"error", @"Failed to get all annotations.", error);
207+
}
208+
});
209+
}
210+
198211
RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199212
dispatch_async(dispatch_get_main_queue(), ^{
200213
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-pspdfkit",
3-
"version": "1.25.0",
3+
"version": "1.25.1",
44
"description": "A React Native module for the PSPDFKit library.",
55
"keywords": [
66
"react native",

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
}}

samples/Catalog/Catalog.ios.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,26 @@ class ProgrammaticAnnotations extends Component {
783783
title="getAllUnsavedAnnotations"
784784
/>
785785
</View>
786+
<View>
787+
<Button
788+
onPress={async () => {
789+
// Get all annotations annotations from the document.
790+
await this.refs.pdfView
791+
.getAllAnnotations()
792+
.then(result => {
793+
if (result) {
794+
alert(JSON.stringify(result));
795+
} else {
796+
alert("Failed to get all annotations.");
797+
}
798+
})
799+
.catch(error => {
800+
alert(JSON.stringify(error));
801+
});
802+
}}
803+
title="getAllAnnotations"
804+
/>
805+
</View>
786806
</View>
787807
</View>
788808
);

0 commit comments

Comments
 (0)