forked from PSPDFKit/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRCTPSPDFKitView.m
More file actions
527 lines (435 loc) · 21.8 KB
/
RCTPSPDFKitView.m
File metadata and controls
527 lines (435 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//
// Copyright © 2018-2023 PSPDFKit GmbH. All rights reserved.
//
// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
// This notice may not be removed from this file.
//
#import "RCTPSPDFKitView.h"
#import <React/RCTUtils.h>
#import "RCTConvert+PSPDFAnnotation.h"
#import "RCTConvert+PSPDFViewMode.h"
#import "RCTConvert+UIBarButtonItem.h"
#if __has_include("PSPDFKitReactNativeiOS-Swift.h")
#import "PSPDFKitReactNativeiOS-Swift.h"
#else
#import <PSPDFKitReactNativeiOS/PSPDFKitReactNativeiOS-Swift.h>
#endif
#define VALIDATE_DOCUMENT(document, ...) { if (!document.isValid) { NSLog(@"Document is invalid."); if (self.onDocumentLoadFailed) { self.onDocumentLoadFailed(@{@"error": @"Document is invalid."}); } return __VA_ARGS__; }}
@interface RCTPSPDFKitViewController : PSPDFViewController
@end
@interface RCTPSPDFKitView ()<PSPDFDocumentDelegate, PSPDFViewControllerDelegate, PSPDFFlexibleToolbarContainerDelegate>
@property (nonatomic, nullable) UIViewController *topController;
@property (nonatomic, strong) NSDictionary *closeButtonAttributes;
@end
@implementation RCTPSPDFKitView
- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_pdfController = [[RCTPSPDFKitViewController alloc] init];
_pdfController.delegate = self;
_pdfController.annotationToolbarController.delegate = self;
// Store the closeButton's target and selector in order to call it later.
_closeButtonAttributes = @{@"target" : _pdfController.closeButtonItem.target,
@"action" : NSStringFromSelector(_pdfController.closeButtonItem.action)};
[_pdfController.closeButtonItem setTarget:self];
[_pdfController.closeButtonItem setAction:@selector(closeButtonPressed:)];
_closeButton = _pdfController.closeButtonItem;
[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];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(spreadIndexDidChange:) name:PSPDFDocumentViewControllerSpreadIndexDidChangeNotification object:nil];
}
[Customization changeLineStyle];
return self;
}
- (void)removeFromSuperview {
// When the React Native `PSPDFKitView` in unmounted, we need to dismiss the `PSPDFViewController` to avoid orphan popovers.
// See https://github.com/PSPDFKit/react-native/issues/277
[self.pdfController dismissViewControllerAnimated:NO completion:NULL];
[super removeFromSuperview];
}
- (void)dealloc {
[self destroyViewControllerRelationship];
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)didMoveToWindow {
UIViewController *controller = self.pspdf_parentViewController;
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],
]];
self.pdfController.pageIndex = self.pageIndex;
}
- (void)destroyViewControllerRelationship {
if (self.topController.parentViewController) {
[self.topController willMoveToParentViewController:nil];
[self.topController removeFromParentViewController];
}
}
- (void)closeButtonPressed:(nullable id)sender {
if (self.onCloseButtonPressed) {
self.onCloseButtonPressed(@{});
} else {
// Invoke the closeButtonItem's default behaviour
id target = _closeButtonAttributes[@"target"];
NSString *action = _closeButtonAttributes[@"action"];
if (target != nil && action != nil) {
SEL selector = NSSelectorFromString(action);
IMP imp = [target methodForSelector:selector];
void (*func)(id, SEL) = (void *)imp;
func(target, selector);
}
}
}
- (UIViewController *)pspdf_parentViewController {
UIResponder *parentResponder = self;
while ((parentResponder = parentResponder.nextResponder)) {
if ([parentResponder isKindOfClass:UIViewController.class]) {
return (UIViewController *)parentResponder;
}
}
return nil;
}
- (BOOL)enterAnnotationCreationMode {
[self.pdfController setViewMode:PSPDFViewModeDocument animated:YES];
[self.pdfController.annotationToolbarController updateHostView:self container:nil viewController:self.pdfController];
return [self.pdfController.annotationToolbarController showToolbarAnimated:YES completion:NULL];
}
- (BOOL)exitCurrentlyActiveMode {
return [self.pdfController.annotationToolbarController hideToolbarAnimated:YES completion:NULL];
}
- (BOOL)saveCurrentDocumentWithError:(NSError *_Nullable *)error {
return [self.pdfController.document saveWithOptions:nil error:error];
}
// MARK: - PSPDFDocumentDelegate
- (void)pdfDocumentDidSave:(nonnull PSPDFDocument *)document {
if (self.onDocumentSaved) {
self.onDocumentSaved(@{});
}
}
- (void)pdfDocument:(PSPDFDocument *)document saveDidFailWithError:(NSError *)error {
if (self.onDocumentSaveFailed) {
self.onDocumentSaveFailed(@{@"error": error.description});
}
}
// MARK: - PSPDFViewControllerDelegate
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController didTapOnAnnotation:(PSPDFAnnotation *)annotation annotationPoint:(CGPoint)annotationPoint annotationView:(UIView<PSPDFAnnotationPresenting> *)annotationView pageView:(PSPDFPageView *)pageView viewPoint:(CGPoint)viewPoint {
if (self.onAnnotationTapped) {
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
NSDictionary *annotationDictionary = [NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL];
self.onAnnotationTapped(annotationDictionary);
}
return self.disableDefaultActionForTappedAnnotations;
}
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController shouldSaveDocument:(nonnull PSPDFDocument *)document withOptions:(NSDictionary<PSPDFDocumentSaveOption,id> *__autoreleasing _Nonnull * _Nonnull)options {
return !self.disableAutomaticSaving;
}
- (void)pdfViewController:(PSPDFViewController *)pdfController didConfigurePageView:(PSPDFPageView *)pageView forPageAtIndex:(NSInteger)pageIndex {
[self onStateChangedForPDFViewController:pdfController pageView:pageView pageAtIndex:pageIndex];
}
- (void)pdfViewController:(PSPDFViewController *)pdfController willBeginDisplayingPageView:(PSPDFPageView *)pageView forPageAtIndex:(NSInteger)pageIndex {
[self onStateChangedForPDFViewController:pdfController pageView:pageView pageAtIndex:pageIndex];
}
- (void)pdfViewController:(PSPDFViewController *)pdfController didChangeDocument:(nullable PSPDFDocument *)document {
VALIDATE_DOCUMENT(document)
}
// MARK: - PSPDFFlexibleToolbarContainerDelegate
- (void)flexibleToolbarContainerDidShow:(PSPDFFlexibleToolbarContainer *)container {
PSPDFPageIndex pageIndex = self.pdfController.pageIndex;
PSPDFPageView *pageView = [self.pdfController pageViewForPageAtIndex:pageIndex];
[self onStateChangedForPDFViewController:self.pdfController pageView:pageView pageAtIndex:pageIndex];
}
- (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)container {
PSPDFPageIndex pageIndex = self.pdfController.pageIndex;
PSPDFPageView *pageView = [self.pdfController pageViewForPageAtIndex:pageIndex];
[self onStateChangedForPDFViewController:self.pdfController pageView:pageView pageAtIndex:pageIndex];
}
// MARK: - Instant JSON
- (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 error:error];
return @{@"annotations" : annotationsJSON};
}
- (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:error];
} else {
NSLog(@"Invalid JSON Annotation.");
return NO;
}
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:error];
if (annotation) {
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.
if ([annotation.uuid isEqualToString:annotationUUID]) {
success = [document removeAnnotations:@[annotation] options:nil];
break;
}
}
if (!success) {
NSLog(@"Failed to remove annotation.");
}
return success;
}
- (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:error];
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:error];
return annotationsJSON;
}
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)
NSArray<PSPDFAnnotation *> *annotations = [[document allAnnotationsOfType:type].allValues valueForKeyPath:@"@unionOfArrays.self"];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
return @{@"annotations" : annotationsJSON};
}
- (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:error];
} else {
NSLog(@"Invalid JSON Annotations.");
return NO;
}
PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc] initWithData:data];
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
BOOL success = [document applyInstantJSONFromDataProvider:dataContainerProvider toDocumentProvider:documentProvider lenient:NO error:error];
if (!success) {
NSLog(@"Failed to add annotations.");
}
[self.pdfController reloadData];
return success;
}
// MARK: - Forms
- (NSDictionary<NSString *, id> *)getFormFieldValue:(NSString *)fullyQualifiedName {
if (fullyQualifiedName.length == 0) {
NSLog(@"Invalid fully qualified name.");
return nil;
}
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)
for (PSPDFFormElement *formElement in document.formParser.forms) {
if ([formElement.fullyQualifiedFieldName isEqualToString:fullyQualifiedName]) {
id formFieldValue = formElement.value;
return @{@"value": formFieldValue ?: [NSNull new]};
}
}
return @{@"error": @"Failed to get the form field value."};
}
- (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName {
if (fullyQualifiedName.length == 0) {
NSLog(@"Invalid fully qualified name.");
return NO;
}
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
BOOL success = NO;
for (PSPDFFormElement *formElement in document.formParser.forms) {
if ([formElement.fullyQualifiedFieldName isEqualToString:fullyQualifiedName]) {
if ([formElement isKindOfClass:PSPDFButtonFormElement.class]) {
if ([value isEqualToString:@"selected"]) {
[(PSPDFButtonFormElement *)formElement select];
success = YES;
} else if ([value isEqualToString:@"deselected"]) {
[(PSPDFButtonFormElement *)formElement deselect];
success = YES;
}
} else if ([formElement isKindOfClass:PSPDFChoiceFormElement.class]) {
((PSPDFChoiceFormElement *)formElement).selectedIndices = [NSIndexSet indexSetWithIndex:value.integerValue];
success = YES;
} else if ([formElement isKindOfClass:PSPDFTextFieldFormElement.class]) {
formElement.contents = value;
success = YES;
} else if ([formElement isKindOfClass:PSPDFSignatureFormElement.class]) {
NSLog(@"Signature form elements are not supported.");
success = NO;
} else {
NSLog(@"Unsupported form element.");
success = NO;
}
break;
}
}
return success;
}
// MARK: - Notifications
- (void)annotationChangedNotification:(NSNotification *)notification {
id object = notification.object;
NSArray <PSPDFAnnotation *> *annotations;
if ([object isKindOfClass:NSArray.class]) {
annotations = object;
} else if ([object isKindOfClass:PSPDFAnnotation.class]) {
annotations = @[object];
} else {
if (self.onAnnotationsChanged) {
self.onAnnotationsChanged(@{@"error" : @"Invalid annotation error."});
}
return;
}
NSString *name = notification.name;
NSString *change;
if ([name isEqualToString:PSPDFAnnotationChangedNotification]) {
change = @"changed";
} else if ([name isEqualToString:PSPDFAnnotationsAddedNotification]) {
change = @"added";
} else if ([name isEqualToString:PSPDFAnnotationsRemovedNotification]) {
change = @"removed";
}
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:NULL];
if (self.onAnnotationsChanged) {
self.onAnnotationsChanged(@{@"change" : change, @"annotations" : annotationsJSON});
}
}
- (void)spreadIndexDidChange:(NSNotification *)notification {
PSPDFDocumentViewController *documentViewController = self.pdfController.documentViewController;
if (notification.object != documentViewController) { return; }
PSPDFPageIndex pageIndex = [documentViewController.layout pageRangeForSpreadAtIndex:documentViewController.spreadIndex].location;
PSPDFPageView *pageView = [self.pdfController pageViewForPageAtIndex:pageIndex];
[self onStateChangedForPDFViewController:self.pdfController pageView:pageView pageAtIndex:pageIndex];
}
// MARK: - Customize the Toolbar
- (void)setLeftBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode:(nullable NSString *) viewMode animated:(BOOL)animated {
NSMutableArray *leftItems = [NSMutableArray array];
for (NSString *barButtonItemString in items) {
UIBarButtonItem *barButtonItem = [RCTConvert uiBarButtonItemFrom:barButtonItemString forViewController:self.pdfController];
if (barButtonItem && ![self.pdfController.navigationItem.rightBarButtonItems containsObject:barButtonItem]) {
[leftItems addObject:barButtonItem];
}
}
if (viewMode.length) {
[self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
[self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] animated:animated];
}
}
- (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode:(nullable NSString *) viewMode animated:(BOOL)animated {
NSMutableArray *rightItems = [NSMutableArray array];
for (NSString *barButtonItemString in items) {
UIBarButtonItem *barButtonItem = [RCTConvert uiBarButtonItemFrom:barButtonItemString forViewController:self.pdfController];
if (barButtonItem && ![self.pdfController.navigationItem.leftBarButtonItems containsObject:barButtonItem]) {
[rightItems addObject:barButtonItem];
}
}
if (viewMode.length) {
[self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
[self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] animated:animated];
}
}
- (NSArray <NSString *> *)getLeftBarButtonItemsForViewMode:(NSString *)viewMode {
NSArray *items;
if (viewMode.length) {
items = [self.pdfController.navigationItem leftBarButtonItemsForViewMode:[RCTConvert PSPDFViewMode:viewMode]];
} else {
items = [self.pdfController.navigationItem leftBarButtonItems];
}
return [self buttonItemsStringFromUIBarButtonItems:items];
}
- (NSArray <NSString *> *)getRightBarButtonItemsForViewMode:(NSString *)viewMode {
NSArray *items;
if (viewMode.length) {
items = [self.pdfController.navigationItem rightBarButtonItemsForViewMode:[RCTConvert PSPDFViewMode:viewMode]];
} else {
items = [self.pdfController.navigationItem rightBarButtonItems];
}
return [self buttonItemsStringFromUIBarButtonItems:items];
}
// MARK: - Helpers
- (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController pageView:(PSPDFPageView *)pageView pageAtIndex:(NSInteger)pageIndex {
if (self.onStateChanged) {
BOOL isDocumentLoaded = [pdfController.document isValid];
PSPDFPageCount pageCount = pdfController.document.pageCount;
BOOL isAnnotationToolBarVisible = [pdfController.annotationToolbarController isToolbarVisible];
BOOL hasSelectedAnnotations = pageView.selectedAnnotations.count > 0;
BOOL hasSelectedText = pageView.selectionView.selectedText.length > 0;
BOOL isFormEditingActive = NO;
for (PSPDFAnnotation *annotation in pageView.selectedAnnotations) {
if ([annotation isKindOfClass:PSPDFWidgetAnnotation.class]) {
isFormEditingActive = YES;
break;
}
}
self.onStateChanged(@{@"documentLoaded" : @(isDocumentLoaded),
@"currentPageIndex" : @(pdfController.pageIndex),
@"pageCount" : @(pageCount),
@"annotationCreationActive" : @(isAnnotationToolBarVisible),
@"affectedPageIndex": @(pageIndex),
@"annotationEditingActive" : @(hasSelectedAnnotations),
@"textSelectionActive" : @(hasSelectedText),
@"formEditingActive" : @(isFormEditingActive)
});
}
}
- (NSArray <NSString *> *)buttonItemsStringFromUIBarButtonItems:(NSArray <UIBarButtonItem *> *)barButtonItems {
NSMutableArray *barButtonItemsString = [NSMutableArray new];
[barButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull barButtonItem, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *buttonNameString = [RCTConvert stringBarButtonItemFrom:barButtonItem forViewController:self.pdfController];
if (buttonNameString) {
[barButtonItemsString addObject:buttonNameString];
}
}];
return [barButtonItemsString copy];
}
@end
@implementation RCTPSPDFKitViewController
- (void)viewWillTransitionToSize:(CGSize)newSize withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:newSize withTransitionCoordinator:coordinator];
/* Workaround for internal issue 25653:
We re-apply the current view state to workaround an issue where the last page view would be layed out incorrectly
in single page mode and scroll per spread page trasition after device rotation.
We do this because the `PSPDFViewController` is not embedded as recommended in
https://pspdfkit.com/guides/ios/current/customizing-the-interface/embedding-the-pdfviewcontroller-inside-a-custom-container-view-controller
and because React Native itself handles the React Native view.
TL;DR: We are adding the `PSPDFViewController` to `RCTPSPDFKitView` and not to the container controller's view.
*/
[coordinator animateAlongsideTransition:NULL completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self applyViewState:self.viewState animateIfPossible:NO];
}];
}
@end