Skip to content

Commit e28185f

Browse files
committed
Implement new and old arch on iOS
1 parent 94fa38f commit e28185f

11 files changed

Lines changed: 241 additions & 14 deletions

File tree

ReactNativePdfRenderer.podspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Pod::Spec.new do |s|
1515
s.source = { :git => repository, :tag => version }
1616
s.platform = :ios, "11.0"
1717
s.preserve_paths = 'README.md', 'package.json', '*.js'
18-
s.source_files = 'ios/ReactNativePdfRendererLibrary/**/*.{h,m}'
18+
s.source_files = 'ios/ReactNativePdfRendererLibrary/**/*.{h,m,mm,swift}'
1919

20-
s.dependency 'React-Core'
20+
s.frameworks = 'PDFKit'
21+
22+
install_modules_dependencies(s)
2123
end

Sample/ios/Podfile.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,29 @@ PODS:
17411741
- React-perflogger (= 0.79.4)
17421742
- React-utils (= 0.79.4)
17431743
- ReactNativePdfRenderer (2.0.0):
1744+
- DoubleConversion
1745+
- glog
1746+
- hermes-engine
1747+
- RCT-Folly (= 2024.11.18.00)
1748+
- RCTRequired
1749+
- RCTTypeSafety
17441750
- React-Core
1751+
- React-debug
1752+
- React-Fabric
1753+
- React-featureflags
1754+
- React-graphics
1755+
- React-hermes
1756+
- React-ImageManager
1757+
- React-jsi
1758+
- React-NativeModulesApple
1759+
- React-RCTFabric
1760+
- React-renderercss
1761+
- React-rendererdebug
1762+
- React-utils
1763+
- ReactCodegen
1764+
- ReactCommon/turbomodule/bridging
1765+
- ReactCommon/turbomodule/core
1766+
- Yoga
17451767
- SocketRocket (0.7.1)
17461768
- Yoga (0.0.0)
17471769

@@ -2071,7 +2093,7 @@ SPEC CHECKSUMS:
20712093
ReactAppDependencyProvider: 04be2a00933bf0930cafcc4da1e0760143573d82
20722094
ReactCodegen: 1bcfcb6519fadfb3e0072e27b8ff868b7dfbac64
20732095
ReactCommon: 2bc79208d91eda74edeff759d82c93460b07612f
2074-
ReactNativePdfRenderer: 375a8bcaf90837557c8e0a33622907a8f166bb3f
2096+
ReactNativePdfRenderer: 776f3e1e51b0e42fbbe739d1042ac39ef14dea69
20752097
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
20762098
Yoga: a6cb833e04fb8c59a012b49fb1d040fcb0cbb633
20772099

Sample/ios/Sample/AppDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2929
launchOptions: launchOptions
3030
)
3131

32-
super.application(application, didFinishLaunchingWithOptions: launchOptions)
3332
return true
3433
}
3534
}

ios/ReactNativePdfRenderer.xcodeproj/project.pbxproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
241DF20429B9FD8B003ED990 /* RNPDFView.m in Sources */ = {isa = PBXBuildFile; fileRef = 241DF20129B9FD8B003ED990 /* RNPDFView.m */; };
10+
241DF20429B9FD8B003ED990 /* RNPDFView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 241DF20129B9FD8B003ED990 /* RNPDFView.mm */; };
1111
241DF20529B9FD8B003ED990 /* RNPdfRendererModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 241DF20329B9FD8B003ED990 /* RNPdfRendererModule.m */; };
12+
248B169F2E10C36D00F84136 /* RNPdfRendererComponentView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 248B169E2E10C36D00F84136 /* RNPdfRendererComponentView.mm */; };
1213
/* End PBXBuildFile section */
1314

1415
/* Begin PBXCopyFilesBuildPhase section */
@@ -25,9 +26,11 @@
2526

2627
/* Begin PBXFileReference section */
2728
241DF20029B9FD8A003ED990 /* RNPDFView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPDFView.h; sourceTree = "<group>"; };
28-
241DF20129B9FD8B003ED990 /* RNPDFView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNPDFView.m; sourceTree = "<group>"; };
29+
241DF20129B9FD8B003ED990 /* RNPDFView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RNPDFView.mm; sourceTree = "<group>"; };
2930
241DF20229B9FD8B003ED990 /* RNPdfRendererModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPdfRendererModule.h; sourceTree = "<group>"; };
3031
241DF20329B9FD8B003ED990 /* RNPdfRendererModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNPdfRendererModule.m; sourceTree = "<group>"; };
32+
248B169D2E10C36D00F84136 /* RNPdfRendererComponentView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNPdfRendererComponentView.h; sourceTree = "<group>"; };
33+
248B169E2E10C36D00F84136 /* RNPdfRendererComponentView.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RNPdfRendererComponentView.mm; sourceTree = "<group>"; };
3134
A5AF7CE21FAA17C900C5BDC0 /* libReactNativePdfRendererLibrary.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativePdfRendererLibrary.a; sourceTree = BUILT_PRODUCTS_DIR; };
3235
/* End PBXFileReference section */
3336

@@ -57,10 +60,12 @@
5760
A5AF7CE31FAA17C900C5BDC0 /* ReactNativePdfRendererLibrary */ = {
5861
isa = PBXGroup;
5962
children = (
63+
248B169D2E10C36D00F84136 /* RNPdfRendererComponentView.h */,
64+
248B169E2E10C36D00F84136 /* RNPdfRendererComponentView.mm */,
6065
241DF20229B9FD8B003ED990 /* RNPdfRendererModule.h */,
6166
241DF20329B9FD8B003ED990 /* RNPdfRendererModule.m */,
6267
241DF20029B9FD8A003ED990 /* RNPDFView.h */,
63-
241DF20129B9FD8B003ED990 /* RNPDFView.m */,
68+
241DF20129B9FD8B003ED990 /* RNPDFView.mm */,
6469
);
6570
path = ReactNativePdfRendererLibrary;
6671
sourceTree = "<group>";
@@ -138,8 +143,9 @@
138143
isa = PBXSourcesBuildPhase;
139144
buildActionMask = 2147483647;
140145
files = (
141-
241DF20429B9FD8B003ED990 /* RNPDFView.m in Sources */,
146+
241DF20429B9FD8B003ED990 /* RNPDFView.mm in Sources */,
142147
241DF20529B9FD8B003ED990 /* RNPdfRendererModule.m in Sources */,
148+
248B169F2E10C36D00F84136 /* RNPdfRendererComponentView.mm in Sources */,
143149
);
144150
runOnlyForDeploymentPostprocessing = 0;
145151
};

ios/ReactNativePdfRendererLibrary/RNPDFView.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@
2323
// Created by Douglas Nassif Roma Junior on 08/03/23.
2424
//
2525

26-
@import Foundation;
27-
@import UIKit;
28-
@import PDFKit;
26+
#import <Foundation/Foundation.h>
27+
#import <UIKit/UIKit.h>
28+
#import <PDFKit/PDFKit.h>
2929

30+
#ifdef RCT_NEW_ARCH_ENABLED
31+
#else
3032
#import <React/RCTComponent.h>
33+
#endif
3134

3235
@interface RNPDFView: PDFView
3336

37+
#ifdef RCT_NEW_ARCH_ENABLED
38+
#else
3439
@property (nonatomic, copy) RCTBubblingEventBlock onPageChange;
40+
#endif
3541

3642
-(void) setDistanceBetweenPages:(NSNumber*) distance;
3743
-(void) setParams:(NSDictionary*) params;

ios/ReactNativePdfRendererLibrary/RNPDFView.m renamed to ios/ReactNativePdfRendererLibrary/RNPDFView.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ -(void) setParams:(NSDictionary*) params {
5454
}
5555
self.displayMode = singlePage ? kPDFDisplaySinglePage : kPDFDisplaySinglePageContinuous;
5656

57-
self.document = pdfDocument;
57+
dispatch_async(dispatch_get_main_queue(), ^{
58+
self.document = pdfDocument;
59+
});
5860

5961
dispatch_async(dispatch_get_main_queue(), ^{
6062
self.minScaleFactor = self.scaleFactorForSizeToFit;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
29+
#import <UIKit/UIKit.h>
30+
#import <PDFKit/PDFKit.h>
31+
#import <React/RCTViewComponentView.h>
32+
33+
#import "RNPDFView.h"
34+
35+
NS_ASSUME_NONNULL_BEGIN
36+
37+
@interface RNPdfRendererComponentView : RCTViewComponentView
38+
39+
@end
40+
41+
NS_ASSUME_NONNULL_END
42+
43+
#endif // RCT_NEW_ARCH_ENABLED
44+
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

ios/ReactNativePdfRendererLibrary/RNPdfRendererModule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
// Created by Douglas Nassif Roma Junior on 08/03/23.
2424
//
2525

26+
#ifdef RCT_NEW_ARCH_ENABLED
27+
#else
28+
2629
@import Foundation;
2730
@import UIKit;
2831
@import PDFKit;
2932

3033
#import <React/RCTViewManager.h>
34+
#import <PDFKit/PDFKit.h>
35+
3136
#import "RNPDFView.h"
3237

3338
@interface RNPdfRendererModule : RCTViewManager
3439
@end
40+
41+
#endif // RCT_NEW_ARCH_ENABLED

ios/ReactNativePdfRendererLibrary/RNPdfRendererModule.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
// Created by Douglas Nassif Roma Junior on 08/03/23.
2424
//
2525

26+
#ifdef RCT_NEW_ARCH_ENABLED
27+
#else
28+
2629
#import "RNPdfRendererModule.h"
2730

2831
@implementation RNPdfRendererModule
@@ -73,3 +76,5 @@ - (void)handlePageChange:(NSNotification*) notification {
7376
}
7477

7578
@end
79+
80+
#endif // RCT_NEW_ARCH_ENABLED

0 commit comments

Comments
 (0)