Skip to content

Commit

Permalink
Merge pull request #156 from PSPDFKit/use-uuids-for-removal
Browse files Browse the repository at this point in the history
Use the uuid property to remove an annotation
  • Loading branch information
radazzouz authored Jan 15, 2019
2 parents e8aea9c + c2c2339 commit 0b39ad6
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The [PSPDFKit SDK](https://pspdfkit.com/) is a framework that allows you to view
#### Requirements

- Xcode 10.1
- PSPDFKit 8.1 for iOS or later
- PSPDFKit 8.1.3 for iOS or later
- react-native >= 0.55.4

#### Getting Started
Expand Down
10 changes: 6 additions & 4 deletions ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ @implementation RCTConvert (PSPDFAnnotation)
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations {
NSMutableArray <NSDictionary *> *annotationsJSON = [NSMutableArray new];
for (PSPDFAnnotation *annotation in annotations) {
NSDictionary <NSString *, NSString *> *uuidDict = @{@"uuid" : annotation.uuid};
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
if (annotationData) {
NSDictionary *annotationDictionary = [NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL];
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL] mutableCopy];
[annotationDictionary addEntriesFromDictionary:uuidDict];
if (annotationDictionary) {
[annotationsJSON addObject:annotationDictionary];
}
} else if (annotation.name) {
// We only generate Instant JSON data for attached annotations. When an annotation is deleted, we only send the annotation name.
[annotationsJSON addObject:@{@"name" : annotation.name}];
} else {
// We only generate Instant JSON data for attached annotations. When an annotation is deleted, we only set the annotation uuid.
[annotationsJSON addObject:uuidDict];
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/RCTPSPDFKit/RCTPSPDFKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/// Anotations
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
- (BOOL)addAnnotation:(id)jsonAnnotation;
- (BOOL)removeAnnotation:(id)jsonAnnotation;
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
- (BOOL)addAnnotations:(NSString *)jsonAnnotations;

Expand Down
22 changes: 5 additions & 17 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,31 +205,19 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
return success;
}

- (BOOL)removeAnnotation:(id)jsonAnnotation {
NSData *data;
if ([jsonAnnotation isKindOfClass:NSString.class]) {
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
} else {
NSLog(@"Invalid JSON Annotation.");
return NO;
}

- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
PSPDFDocument *document = self.pdfController.document;
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;

BOOL success = NO;
if (data) {
PSPDFAnnotation *annotationToRemove = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:NULL];
for (PSPDFAnnotation *annotation in [document annotationsForPageAtIndex:annotationToRemove.pageIndex type:annotationToRemove.type]) {

NSArray<PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"];
for (PSPDFAnnotation *annotation in allAnnotations) {
// Remove the annotation if the name matches.
if ([annotation.name isEqualToString:annotationToRemove.name]) {
if ([annotation.uuid isEqualToString:annotationUUID]) {
success = [document removeAnnotations:@[annotation] options:nil];
break;
}
}
}

if (!success) {
NSLog(@"Failed to remove annotation.");
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ @implementation RCTPSPDFKitViewManager
RCT_EXPORT_METHOD(removeAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
BOOL success = [component removeAnnotation:jsonAnnotation];
BOOL success = [component removeAnnotationWithUUID:jsonAnnotation[@"uuid"]];
if (success) {
resolve(@(success));
} else {
Expand Down
4 changes: 2 additions & 2 deletions ios/cocoapods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
### CocoaPods integration

#### Requirements
- Xcode 10
- PSPDFKit 8.0 for iOS or later
- Xcode 10.1
- PSPDFKit 8.1.3 for iOS or later
- react-native >= 0.55.4
- CocoaPods >= 1.5.3

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-pspdfkit",
"version": "1.22.0",
"version": "1.23.0",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down

0 comments on commit 0b39ad6

Please sign in to comment.