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;
@@ -174,10 +176,7 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain
174176
175177- (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAnnotations : (PSPDFPageIndex)pageIndex type : (PSPDFAnnotationType)type {
176178 PSPDFDocument *document = self.pdfController .document ;
177- if (!document.isValid ) {
178- NSLog (@" Document is invalid." );
179- return nil ;
180- }
179+ VALIDATE_DOCUMENT (document, nil );
181180
182181 NSArray <PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex: pageIndex type: type];
183182 NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations: annotations];
@@ -196,10 +195,7 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
196195 }
197196
198197 PSPDFDocument *document = self.pdfController .document ;
199- if (!document.isValid ) {
200- NSLog (@" Document is invalid." );
201- return NO ;
202- }
198+ VALIDATE_DOCUMENT (document, NO )
203199 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
204200
205201 BOOL success = NO ;
@@ -217,11 +213,7 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
217213
218214- (BOOL )removeAnnotationWithUUID : (NSString *)annotationUUID {
219215 PSPDFDocument *document = self.pdfController .document ;
220- if (!document.isValid ) {
221- NSLog (@" Document is invalid." );
222- return NO ;
223- }
224-
216+ VALIDATE_DOCUMENT (document, NO )
225217 BOOL success = NO ;
226218
227219 NSArray <PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType: PSPDFAnnotationTypeAll].allValues valueForKeyPath: @" @unionOfArrays.self" ];
@@ -241,10 +233,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
241233
242234- (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
243235 PSPDFDocument *document = self.pdfController .document ;
244- if (!document.isValid ) {
245- NSLog (@" Document is invalid." );
246- return nil ;
247- }
236+ VALIDATE_DOCUMENT (document, nil )
248237
249238 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
250239 NSData *data = [document generateInstantJSONFromDocumentProvider: documentProvider error: NULL ];
@@ -265,11 +254,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
265254
266255 PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc ] initWithData: data];
267256 PSPDFDocument *document = self.pdfController .document ;
268- if (!document.isValid ) {
269- NSLog (@" Document is invalid." );
270- return NO ;
271- }
272-
257+ VALIDATE_DOCUMENT (document, NO )
273258 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
274259 BOOL success = [document applyInstantJSONFromDataProvider: dataContainerProvider toDocumentProvider: documentProvider lenient: NO error: NULL ];
275260 if (!success) {
@@ -289,10 +274,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
289274 }
290275
291276 PSPDFDocument *document = self.pdfController .document ;
292- if (!document.isValid ) {
293- NSLog (@" Document is invalid." );
294- return nil ;
295- }
277+ VALIDATE_DOCUMENT (document, nil )
296278
297279 for (PSPDFFormElement *formElement in document.formParser .forms ) {
298280 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
@@ -311,10 +293,7 @@ - (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
311293 }
312294
313295 PSPDFDocument *document = self.pdfController .document ;
314- if (!document.isValid ) {
315- NSLog (@" Document is invalid." );
316- return ;
317- }
296+ VALIDATE_DOCUMENT (document)
318297
319298 for (PSPDFFormElement *formElement in document.formParser .forms ) {
320299 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
0 commit comments