1111#import < React/RCTUtils.h>
1212#import " RCTConvert+PSPDFAnnotation.h"
1313
14+ #define VALIDATE_DOCUMENT (document, ...) { if (!document.isValid ) { NSLog (@" Document is invalid." ); return __VA_ARGS__; }}
15+
1416@interface RCTPSPDFKitView ()<PSPDFDocumentDelegate, PSPDFViewControllerDelegate, PSPDFFlexibleToolbarContainerDelegate>
1517
1618@property (nonatomic , nullable ) UIViewController *topController;
@@ -173,7 +175,10 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain
173175#pragma mark - Instant JSON
174176
175177- (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAnnotations : (PSPDFPageIndex)pageIndex type : (PSPDFAnnotationType)type {
176- NSArray <PSPDFAnnotation *> *annotations = [self .pdfController.document annotationsForPageAtIndex: pageIndex type: type];
178+ PSPDFDocument *document = self.pdfController .document ;
179+ VALIDATE_DOCUMENT (document, nil );
180+
181+ NSArray <PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex: pageIndex type: type];
177182 NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations: annotations];
178183 return @{@" annotations" : annotationsJSON};
179184}
@@ -190,10 +195,7 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
190195 }
191196
192197 PSPDFDocument *document = self.pdfController .document ;
193- if (!document.isValid ) {
194- NSLog (@" Document is invalid." );
195- return NO ;
196- }
198+ VALIDATE_DOCUMENT (document, NO )
197199 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
198200
199201 BOOL success = NO ;
@@ -211,16 +213,12 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
211213
212214- (BOOL )removeAnnotationWithUUID : (NSString *)annotationUUID {
213215 PSPDFDocument *document = self.pdfController .document ;
214- if (!document.isValid ) {
215- NSLog (@" Document is invalid." );
216- return NO ;
217- }
218-
216+ VALIDATE_DOCUMENT (document, NO )
219217 BOOL success = NO ;
220218
221219 NSArray <PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType: PSPDFAnnotationTypeAll].allValues valueForKeyPath: @" @unionOfArrays.self" ];
222220 for (PSPDFAnnotation *annotation in allAnnotations) {
223- // Remove the annotation if the name matches .
221+ // Remove the annotation if the uuids match .
224222 if ([annotation.uuid isEqualToString: annotationUUID]) {
225223 success = [document removeAnnotations: @[annotation] options: nil ];
226224 break ;
@@ -234,8 +232,11 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
234232}
235233
236234- (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
237- PSPDFDocumentProvider *documentProvider = self.pdfController .document .documentProviders .firstObject ;
238- NSData *data = [self .pdfController.document generateInstantJSONFromDocumentProvider: documentProvider error: NULL ];
235+ PSPDFDocument *document = self.pdfController .document ;
236+ VALIDATE_DOCUMENT (document, nil )
237+
238+ PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
239+ NSData *data = [document generateInstantJSONFromDocumentProvider: documentProvider error: NULL ];
239240 NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: NULL ];
240241 return annotationsJSON;
241242}
@@ -253,11 +254,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
253254
254255 PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc ] initWithData: data];
255256 PSPDFDocument *document = self.pdfController .document ;
256- if (!document.isValid ) {
257- NSLog (@" Document is invalid." );
258- return NO ;
259- }
260-
257+ VALIDATE_DOCUMENT (document, NO )
261258 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
262259 BOOL success = [document applyInstantJSONFromDataProvider: dataContainerProvider toDocumentProvider: documentProvider lenient: NO error: NULL ];
263260 if (!success) {
@@ -277,10 +274,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
277274 }
278275
279276 PSPDFDocument *document = self.pdfController .document ;
280- if (!document.isValid ) {
281- NSLog (@" Document is invalid." );
282- return nil ;
283- }
277+ VALIDATE_DOCUMENT (document, nil )
284278
285279 for (PSPDFFormElement *formElement in document.formParser .forms ) {
286280 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
@@ -299,10 +293,7 @@ - (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
299293 }
300294
301295 PSPDFDocument *document = self.pdfController .document ;
302- if (!document.isValid ) {
303- NSLog (@" Document is invalid." );
304- return ;
305- }
296+ VALIDATE_DOCUMENT (document)
306297
307298 for (PSPDFFormElement *formElement in document.formParser .forms ) {
308299 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
0 commit comments