Skip to content

Commit

Permalink
Merge pull request #261 from PSPDFKit/rad/improve-error-handling
Browse files Browse the repository at this point in the history
Improve error handling for annotation manipulations functions
  • Loading branch information
radazzouz authored Jul 25, 2019
2 parents 0fb1fce + a58ed2d commit 3c473ce
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 75 deletions.
2 changes: 1 addition & 1 deletion ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@interface RCTConvert (PSPDFAnnotation)

+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations;
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations error:(NSError **)error;
+ (PSPDFAnnotationType)annotationTypeFromInstantJSONType:(NSString *)type;

@end
8 changes: 4 additions & 4 deletions ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

@implementation RCTConvert (PSPDFAnnotation)

+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations {
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations error:(NSError **)error {
NSMutableArray <NSDictionary *> *annotationsJSON = [NSMutableArray new];
for (PSPDFAnnotation *annotation in annotations) {
NSDictionary <NSString *, NSString *> *uuidDict = @{@"uuid" : annotation.uuid};
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
NSData *annotationData = [annotation generateInstantJSONWithError:error];
if (annotationData) {
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL] mutableCopy];
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:error] mutableCopy];
[annotationDictionary addEntriesFromDictionary:uuidDict];
if (annotationDictionary) {
[annotationsJSON addObject:annotationDictionary];
Expand All @@ -27,7 +27,7 @@ @implementation RCTConvert (PSPDFAnnotation)
[annotationsJSON addObject:uuidDict];
}
}

return [annotationsJSON copy];
}

Expand Down
10 changes: 5 additions & 5 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)exitCurrentlyActiveMode;

/// Document
- (BOOL)saveCurrentDocument;
- (BOOL)saveCurrentDocumentWithError:(NSError *_Nullable *)error;

/// Anotations
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
- (BOOL)addAnnotation:(id)jsonAnnotation;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error;
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error;
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
- (BOOL)addAnnotations:(NSString *)jsonAnnotations;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error;
- (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error;

/// Forms
- (NSDictionary<NSString *, NSString *> *)getFormFieldValue:(NSString *)fullyQualifiedName;
Expand Down
82 changes: 41 additions & 41 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ - (instancetype)initWithFrame:(CGRect)frame {
_pdfController.delegate = self;
_pdfController.annotationToolbarController.delegate = self;
_closeButton = [[UIBarButtonItem alloc] initWithImage:[PSPDFKit imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonPressed:)];

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationChangedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsAddedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsRemovedNotification object:nil];
}

return self;
}

Expand All @@ -48,27 +48,27 @@ - (void)didMoveToWindow {
if (controller == nil || self.window == nil || self.topController != nil) {
return;
}

if (self.pdfController.configuration.useParentNavigationBar || self.hideNavigationBar) {
self.topController = self.pdfController;

} else {
self.topController = [[PSPDFNavigationController alloc] initWithRootViewController:self.pdfController];;
}

UIView *topControllerView = self.topController.view;
topControllerView.translatesAutoresizingMaskIntoConstraints = NO;

[self addSubview:topControllerView];
[controller addChildViewController:self.topController];
[self.topController didMoveToParentViewController:controller];

[NSLayoutConstraint activateConstraints:
@[[topControllerView.topAnchor constraintEqualToAnchor:self.topAnchor],
[topControllerView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[topControllerView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[topControllerView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
]];
]];
}

- (void)destroyViewControllerRelationship {
Expand All @@ -81,7 +81,7 @@ - (void)destroyViewControllerRelationship {
- (void)closeButtonPressed:(nullable id)sender {
if (self.onCloseButtonPressed) {
self.onCloseButtonPressed(@{});

} else {
// try to be smart and pop if we are not displayed modally.
BOOL shouldDismiss = YES;
Expand Down Expand Up @@ -119,8 +119,8 @@ - (BOOL)exitCurrentlyActiveMode {
return [self.pdfController.annotationToolbarController hideToolbarAnimated:YES];
}

- (BOOL)saveCurrentDocument {
return [self.pdfController.document saveWithOptions:nil error:NULL];
- (BOOL)saveCurrentDocumentWithError:(NSError *_Nullable *)error {
return [self.pdfController.document saveWithOptions:nil error:error];
}

#pragma mark - PSPDFDocumentDelegate
Expand Down Expand Up @@ -161,7 +161,7 @@ - (void)pdfViewController:(PSPDFViewController *)pdfController willBeginDisplayi
}

- (void)pdfViewController:(PSPDFViewController *)pdfController didChangeDocument:(nullable PSPDFDocument *)document {
VALIDATE_DOCUMENT(document)
VALIDATE_DOCUMENT(document)
}

#pragma mark - PSPDFFlexibleToolbarContainerDelegate
Expand All @@ -180,21 +180,21 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain

#pragma mark - Instant JSON

- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type {
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil);

NSArray <PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex:pageIndex type:type];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
return @{@"annotations" : annotationsJSON};
}

- (BOOL)addAnnotation:(id)jsonAnnotation {
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error {
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];
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:error];
} else {
NSLog(@"Invalid JSON Annotation.");
return NO;
Expand All @@ -203,25 +203,25 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;

BOOL success = NO;
if (data) {
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:NULL];
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:error];
success = [document addAnnotations:@[annotation] options:nil];
}

if (!success) {
NSLog(@"Failed to add annotation.");
}

return success;
}

- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
BOOL success = NO;

NSArray<PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"];
for (PSPDFAnnotation *annotation in allAnnotations) {
// Remove the annotation if the uuids match.
Expand All @@ -237,22 +237,22 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
return success;
}

- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)

PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
NSData *data = [document generateInstantJSONFromDocumentProvider:documentProvider error:NULL];
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
NSData *data = [document generateInstantJSONFromDocumentProvider:documentProvider error:error];
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:error];
return annotationsJSON;
}

- (BOOL)addAnnotations:(id)jsonAnnotations {
- (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
NSData *data;
if ([jsonAnnotations isKindOfClass:NSString.class]) {
data = [jsonAnnotations dataUsingEncoding:NSUTF8StringEncoding];
} else if ([jsonAnnotations isKindOfClass:NSDictionary.class]) {
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotations options:0 error:nil];
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotations options:0 error:error];
} else {
NSLog(@"Invalid JSON Annotations.");
return NO;
Expand All @@ -262,11 +262,11 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
BOOL success = [document applyInstantJSONFromDataProvider:dataContainerProvider toDocumentProvider:documentProvider lenient:NO error:NULL];
BOOL success = [document applyInstantJSONFromDataProvider:dataContainerProvider toDocumentProvider:documentProvider lenient:NO error:error];
if (!success) {
NSLog(@"Failed to add annotations.");
}

[self.pdfController reloadPageAtIndex:self.pdfController.pageIndex animated:NO];
return success;
}
Expand All @@ -278,7 +278,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
NSLog(@"Invalid fully qualified name.");
return nil;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)

Expand All @@ -288,7 +288,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
return @{@"value": formFieldValue ?: [NSNull new]};
}
}

return @{@"error": @"Failed to get the form field value."};
}

Expand All @@ -297,7 +297,7 @@ - (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
NSLog(@"Invalid fully qualified name.");
return;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document)

Expand Down Expand Up @@ -338,7 +338,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
}
return;
}

NSString *name = notification.name;
NSString *change;
if ([name isEqualToString:PSPDFAnnotationChangedNotification]) {
Expand All @@ -348,8 +348,8 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
} else if ([name isEqualToString:PSPDFAnnotationsRemovedNotification]) {
change = @"removed";
}

NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:NULL];
if (self.onAnnotationsChanged) {
self.onAnnotationsChanged(@{@"change" : change, @"annotations" : annotationsJSON});
}
Expand All @@ -365,7 +365,7 @@ - (void)setLeftBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode
[leftItems addObject:barButtonItem];
}
}

if (viewMode.length) {
[self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
Expand All @@ -381,7 +381,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
[rightItems addObject:barButtonItem];
}
}

if (viewMode.length) {
[self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
Expand All @@ -396,7 +396,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
} else {
items = [self.pdfController.navigationItem leftBarButtonItems];
}

return [self buttonItemsStringFromUIBarButtonItems:items];
}

Expand All @@ -407,7 +407,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
} else {
items = [self.pdfController.navigationItem rightBarButtonItems];
}

return [self buttonItemsStringFromUIBarButtonItems:items];
}

Expand Down Expand Up @@ -435,7 +435,7 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController
@"annotationEditingActive" : @(hasSelectedAnnotations),
@"textSelectionActive" : @(hasSelectedText),
@"formEditingActive" : @(isFormEditingActive)
});
});
}
}

Expand Down
Loading

0 comments on commit 3c473ce

Please sign in to comment.