Skip to content

Commit 3c473ce

Browse files
authored
Merge pull request #261 from PSPDFKit/rad/improve-error-handling
Improve error handling for annotation manipulations functions
2 parents 0fb1fce + a58ed2d commit 3c473ce

File tree

9 files changed

+130
-75
lines changed

9 files changed

+130
-75
lines changed

ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@interface RCTConvert (PSPDFAnnotation)
1515

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

1919
@end

ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
@implementation RCTConvert (PSPDFAnnotation)
1313

14-
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations {
14+
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations error:(NSError **)error {
1515
NSMutableArray <NSDictionary *> *annotationsJSON = [NSMutableArray new];
1616
for (PSPDFAnnotation *annotation in annotations) {
1717
NSDictionary <NSString *, NSString *> *uuidDict = @{@"uuid" : annotation.uuid};
18-
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
18+
NSData *annotationData = [annotation generateInstantJSONWithError:error];
1919
if (annotationData) {
20-
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL] mutableCopy];
20+
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:error] mutableCopy];
2121
[annotationDictionary addEntriesFromDictionary:uuidDict];
2222
if (annotationDictionary) {
2323
[annotationsJSON addObject:annotationDictionary];
@@ -27,7 +27,7 @@ @implementation RCTConvert (PSPDFAnnotation)
2727
[annotationsJSON addObject:uuidDict];
2828
}
2929
}
30-
30+
3131
return [annotationsJSON copy];
3232
}
3333

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ NS_ASSUME_NONNULL_BEGIN
3636
- (BOOL)exitCurrentlyActiveMode;
3737

3838
/// Document
39-
- (BOOL)saveCurrentDocument;
39+
- (BOOL)saveCurrentDocumentWithError:(NSError *_Nullable *)error;
4040

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

4848
/// Forms
4949
- (NSDictionary<NSString *, NSString *> *)getFormFieldValue:(NSString *)fullyQualifiedName;

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ - (instancetype)initWithFrame:(CGRect)frame {
2929
_pdfController.delegate = self;
3030
_pdfController.annotationToolbarController.delegate = self;
3131
_closeButton = [[UIBarButtonItem alloc] initWithImage:[PSPDFKit imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonPressed:)];
32-
32+
3333
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationChangedNotification object:nil];
3434
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsAddedNotification object:nil];
3535
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsRemovedNotification object:nil];
3636
}
37-
37+
3838
return self;
3939
}
4040

@@ -48,27 +48,27 @@ - (void)didMoveToWindow {
4848
if (controller == nil || self.window == nil || self.topController != nil) {
4949
return;
5050
}
51-
51+
5252
if (self.pdfController.configuration.useParentNavigationBar || self.hideNavigationBar) {
5353
self.topController = self.pdfController;
54-
54+
5555
} else {
5656
self.topController = [[PSPDFNavigationController alloc] initWithRootViewController:self.pdfController];;
5757
}
58-
58+
5959
UIView *topControllerView = self.topController.view;
6060
topControllerView.translatesAutoresizingMaskIntoConstraints = NO;
61-
61+
6262
[self addSubview:topControllerView];
6363
[controller addChildViewController:self.topController];
6464
[self.topController didMoveToParentViewController:controller];
65-
65+
6666
[NSLayoutConstraint activateConstraints:
6767
@[[topControllerView.topAnchor constraintEqualToAnchor:self.topAnchor],
6868
[topControllerView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
6969
[topControllerView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
7070
[topControllerView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
71-
]];
71+
]];
7272
}
7373

7474
- (void)destroyViewControllerRelationship {
@@ -81,7 +81,7 @@ - (void)destroyViewControllerRelationship {
8181
- (void)closeButtonPressed:(nullable id)sender {
8282
if (self.onCloseButtonPressed) {
8383
self.onCloseButtonPressed(@{});
84-
84+
8585
} else {
8686
// try to be smart and pop if we are not displayed modally.
8787
BOOL shouldDismiss = YES;
@@ -119,8 +119,8 @@ - (BOOL)exitCurrentlyActiveMode {
119119
return [self.pdfController.annotationToolbarController hideToolbarAnimated:YES];
120120
}
121121

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

126126
#pragma mark - PSPDFDocumentDelegate
@@ -161,7 +161,7 @@ - (void)pdfViewController:(PSPDFViewController *)pdfController willBeginDisplayi
161161
}
162162

163163
- (void)pdfViewController:(PSPDFViewController *)pdfController didChangeDocument:(nullable PSPDFDocument *)document {
164-
VALIDATE_DOCUMENT(document)
164+
VALIDATE_DOCUMENT(document)
165165
}
166166

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

181181
#pragma mark - Instant JSON
182182

183-
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type {
183+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
184184
PSPDFDocument *document = self.pdfController.document;
185185
VALIDATE_DOCUMENT(document, nil);
186-
186+
187187
NSArray <PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex:pageIndex type:type];
188-
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations];
188+
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
189189
return @{@"annotations" : annotationsJSON};
190190
}
191191

192-
- (BOOL)addAnnotation:(id)jsonAnnotation {
192+
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error {
193193
NSData *data;
194194
if ([jsonAnnotation isKindOfClass:NSString.class]) {
195195
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
196196
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
197-
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
197+
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:error];
198198
} else {
199199
NSLog(@"Invalid JSON Annotation.");
200200
return NO;
@@ -203,25 +203,25 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
203203
PSPDFDocument *document = self.pdfController.document;
204204
VALIDATE_DOCUMENT(document, NO)
205205
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
206-
206+
207207
BOOL success = NO;
208208
if (data) {
209-
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:NULL];
209+
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:error];
210210
success = [document addAnnotations:@[annotation] options:nil];
211211
}
212-
212+
213213
if (!success) {
214214
NSLog(@"Failed to add annotation.");
215215
}
216-
216+
217217
return success;
218218
}
219219

220220
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
221221
PSPDFDocument *document = self.pdfController.document;
222222
VALIDATE_DOCUMENT(document, NO)
223223
BOOL success = NO;
224-
224+
225225
NSArray<PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"];
226226
for (PSPDFAnnotation *annotation in allAnnotations) {
227227
// Remove the annotation if the uuids match.
@@ -237,22 +237,22 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
237237
return success;
238238
}
239239

240-
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
240+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error {
241241
PSPDFDocument *document = self.pdfController.document;
242242
VALIDATE_DOCUMENT(document, nil)
243-
243+
244244
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
245-
NSData *data = [document generateInstantJSONFromDocumentProvider:documentProvider error:NULL];
246-
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
245+
NSData *data = [document generateInstantJSONFromDocumentProvider:documentProvider error:error];
246+
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:error];
247247
return annotationsJSON;
248248
}
249249

250-
- (BOOL)addAnnotations:(id)jsonAnnotations {
250+
- (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
251251
NSData *data;
252252
if ([jsonAnnotations isKindOfClass:NSString.class]) {
253253
data = [jsonAnnotations dataUsingEncoding:NSUTF8StringEncoding];
254254
} else if ([jsonAnnotations isKindOfClass:NSDictionary.class]) {
255-
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotations options:0 error:nil];
255+
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotations options:0 error:error];
256256
} else {
257257
NSLog(@"Invalid JSON Annotations.");
258258
return NO;
@@ -262,11 +262,11 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
262262
PSPDFDocument *document = self.pdfController.document;
263263
VALIDATE_DOCUMENT(document, NO)
264264
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
265-
BOOL success = [document applyInstantJSONFromDataProvider:dataContainerProvider toDocumentProvider:documentProvider lenient:NO error:NULL];
265+
BOOL success = [document applyInstantJSONFromDataProvider:dataContainerProvider toDocumentProvider:documentProvider lenient:NO error:error];
266266
if (!success) {
267267
NSLog(@"Failed to add annotations.");
268268
}
269-
269+
270270
[self.pdfController reloadPageAtIndex:self.pdfController.pageIndex animated:NO];
271271
return success;
272272
}
@@ -278,7 +278,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
278278
NSLog(@"Invalid fully qualified name.");
279279
return nil;
280280
}
281-
281+
282282
PSPDFDocument *document = self.pdfController.document;
283283
VALIDATE_DOCUMENT(document, nil)
284284

@@ -288,7 +288,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
288288
return @{@"value": formFieldValue ?: [NSNull new]};
289289
}
290290
}
291-
291+
292292
return @{@"error": @"Failed to get the form field value."};
293293
}
294294

@@ -297,7 +297,7 @@ - (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
297297
NSLog(@"Invalid fully qualified name.");
298298
return;
299299
}
300-
300+
301301
PSPDFDocument *document = self.pdfController.document;
302302
VALIDATE_DOCUMENT(document)
303303

@@ -338,7 +338,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
338338
}
339339
return;
340340
}
341-
341+
342342
NSString *name = notification.name;
343343
NSString *change;
344344
if ([name isEqualToString:PSPDFAnnotationChangedNotification]) {
@@ -348,8 +348,8 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
348348
} else if ([name isEqualToString:PSPDFAnnotationsRemovedNotification]) {
349349
change = @"removed";
350350
}
351-
352-
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations];
351+
352+
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:NULL];
353353
if (self.onAnnotationsChanged) {
354354
self.onAnnotationsChanged(@{@"change" : change, @"annotations" : annotationsJSON});
355355
}
@@ -365,7 +365,7 @@ - (void)setLeftBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode
365365
[leftItems addObject:barButtonItem];
366366
}
367367
}
368-
368+
369369
if (viewMode.length) {
370370
[self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
371371
} else {
@@ -381,7 +381,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
381381
[rightItems addObject:barButtonItem];
382382
}
383383
}
384-
384+
385385
if (viewMode.length) {
386386
[self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
387387
} else {
@@ -396,7 +396,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
396396
} else {
397397
items = [self.pdfController.navigationItem leftBarButtonItems];
398398
}
399-
399+
400400
return [self buttonItemsStringFromUIBarButtonItems:items];
401401
}
402402

@@ -407,7 +407,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
407407
} else {
408408
items = [self.pdfController.navigationItem rightBarButtonItems];
409409
}
410-
410+
411411
return [self buttonItemsStringFromUIBarButtonItems:items];
412412
}
413413

@@ -435,7 +435,7 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController
435435
@"annotationEditingActive" : @(hasSelectedAnnotations),
436436
@"textSelectionActive" : @(hasSelectedText),
437437
@"formEditingActive" : @(isFormEditingActive)
438-
});
438+
});
439439
}
440440
}
441441

0 commit comments

Comments
 (0)