|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Douglas Nassif Roma Junior |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | +// |
| 23 | +// Created by Douglas Nassif Roma Junior on 08/03/23. |
| 24 | +// |
| 25 | + |
| 26 | +#ifdef RCT_NEW_ARCH_ENABLED |
| 27 | + |
| 28 | +#import "RNPdfRendererComponentView.h" |
| 29 | + |
| 30 | +#import <react/renderer/components/rnpdfrenderer_codegen/ComponentDescriptors.h> |
| 31 | +#import <react/renderer/components/rnpdfrenderer_codegen/EventEmitters.h> |
| 32 | +#import <react/renderer/components/rnpdfrenderer_codegen/Props.h> |
| 33 | +#import <react/renderer/components/rnpdfrenderer_codegen/RCTComponentViewHelpers.h> |
| 34 | + |
| 35 | +using namespace facebook::react; |
| 36 | + |
| 37 | +@interface RNPdfRendererComponentView () <RCTRNPdfRendererViewViewProtocol> |
| 38 | +@end |
| 39 | + |
| 40 | +@implementation RNPdfRendererComponentView { |
| 41 | + RNPDFView * _pdfView; |
| 42 | +} |
| 43 | + |
| 44 | +BOOL observerAdded = NO; |
| 45 | + |
| 46 | +-(instancetype)init |
| 47 | +{ |
| 48 | + if (!observerAdded) { |
| 49 | + observerAdded = YES; |
| 50 | + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handlePageChange:) name:PDFViewPageChangedNotification object:nil]; |
| 51 | + } |
| 52 | + if(self = [super init]) { |
| 53 | + _pdfView = [RNPDFView new]; |
| 54 | + _pdfView.backgroundColor = UIColor.clearColor; |
| 55 | + [self addSubview:_pdfView]; |
| 56 | + } |
| 57 | + return self; |
| 58 | +} |
| 59 | + |
| 60 | +- (void)dealloc |
| 61 | +{ |
| 62 | + if (observerAdded) { |
| 63 | + observerAdded = NO; |
| 64 | + [NSNotificationCenter.defaultCenter removeObserver:self name:PDFViewPageChangedNotification object:nil]; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +- (void)handlePageChange:(NSNotification*) notification { |
| 69 | + if ([RNPDFView class] != [notification.object class]) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + RNPDFView *view = notification.object; |
| 74 | + |
| 75 | + NSUInteger currentPageNumber = [view.document indexForPage:view.currentPage]; |
| 76 | + |
| 77 | + RNPdfRendererViewEventEmitter::OnPageChange result = RNPdfRendererViewEventEmitter::OnPageChange{}; |
| 78 | + result.position = currentPageNumber; |
| 79 | + result.total = view.document.pageCount; |
| 80 | + |
| 81 | + if (_eventEmitter) { |
| 82 | + self.eventEmitter.onPageChange(result); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps |
| 87 | +{ |
| 88 | + const auto &oldViewProps = *std::static_pointer_cast<RNPdfRendererViewProps const>(_props); |
| 89 | + const auto &newViewProps = *std::static_pointer_cast<RNPdfRendererViewProps const>(props); |
| 90 | + |
| 91 | + if (oldViewProps.params.source != newViewProps.params.source || |
| 92 | + oldViewProps.params.maxZoom != newViewProps.params.maxZoom || |
| 93 | + oldViewProps.params.singlePage != newViewProps.params.singlePage) { |
| 94 | + |
| 95 | + NSString *sourceString = [NSString stringWithUTF8String:newViewProps.params.source.c_str()]; |
| 96 | + |
| 97 | + NSDictionary* params = @{ |
| 98 | + @"source": sourceString, |
| 99 | + @"maxZoom": @(newViewProps.params.maxZoom), |
| 100 | + @"singlePage": @(newViewProps.params.singlePage) |
| 101 | + }; |
| 102 | + |
| 103 | + [_pdfView setParams: params]; |
| 104 | + |
| 105 | + [self setNeedsLayout]; |
| 106 | + } |
| 107 | + |
| 108 | + if (oldViewProps.distanceBetweenPages != newViewProps.distanceBetweenPages) { |
| 109 | + [_pdfView setDistanceBetweenPages: @(newViewProps.distanceBetweenPages)]; |
| 110 | + } |
| 111 | + |
| 112 | + [super updateProps:props oldProps:oldProps]; |
| 113 | +} |
| 114 | + |
| 115 | +-(void)layoutSubviews |
| 116 | +{ |
| 117 | + [super layoutSubviews]; |
| 118 | + _pdfView.frame = self.bounds; |
| 119 | +} |
| 120 | + |
| 121 | +// Event emitter convenience method |
| 122 | +- (const RNPdfRendererViewEventEmitter &)eventEmitter |
| 123 | +{ |
| 124 | + return static_cast<const RNPdfRendererViewEventEmitter &>(*_eventEmitter); |
| 125 | +} |
| 126 | + |
| 127 | ++ (ComponentDescriptorProvider)componentDescriptorProvider |
| 128 | +{ |
| 129 | + return concreteComponentDescriptorProvider<RNPdfRendererViewComponentDescriptor>(); |
| 130 | +} |
| 131 | + |
| 132 | +@end |
| 133 | + |
| 134 | +#endif // RCT_NEW_ARCH_ENABLED |
0 commit comments