Skip to content

Commit d960b9c

Browse files
committed
refactor: modernize, fix fabric
1 parent 01b39ff commit d960b9c

File tree

11 files changed

+209
-563
lines changed

11 files changed

+209
-563
lines changed

RNDocumentPicker.podspec

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "json"
22

33
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4-
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
54

65
Pod::Spec.new do |s|
76
s.name = "RNDocumentPicker"
@@ -11,32 +10,11 @@ Pod::Spec.new do |s|
1110
s.license = package["license"]
1211
s.authors = package["author"]
1312

14-
s.platforms = { :osx => "10.15" }
13+
s.platforms = { :osx => "11.0" }
1514
s.source = { :git => "https://github.com/victorhenrion/react-native-document-picker-macos.git", :tag => "#{s.version}" }
1615

17-
s.source_files = "macos/**/*.{h,m,mm}"
16+
s.source_files = "macos/**/*.{h,m,mm,cpp}"
17+
s.private_header_files = "macos/**/*.h"
1818

19-
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
20-
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
21-
if respond_to?(:install_modules_dependencies, true)
22-
install_modules_dependencies(s)
23-
else
24-
s.dependency "React-Core"
25-
26-
# Don't install the dependencies when we run `pod install` in the old architecture.
27-
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
28-
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
29-
s.pod_target_xcconfig = {
30-
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
31-
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
32-
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
33-
}
34-
s.dependency "React-RCTFabric"
35-
s.dependency "React-Codegen"
36-
s.dependency "RCT-Folly"
37-
s.dependency "RCTRequired"
38-
s.dependency "RCTTypeSafety"
39-
s.dependency "ReactCommon/turbomodule/core"
40-
end
41-
end
19+
install_modules_dependencies(s)
4220
end

macos/RNDocumentPicker.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
#import <Foundation/Foundation.h>
2-
#import <React/RCTBridgeModule.h>
3-
41
#ifdef RCT_NEW_ARCH_ENABLED
5-
#import <React/RCTTurboModule.h>
6-
#endif
72

8-
NS_ASSUME_NONNULL_BEGIN
3+
#import <RNDocumentPickerSpec/RNDocumentPickerSpec.h>
94

10-
@interface RNDocumentPicker : NSObject <RCTBridgeModule
11-
#ifdef RCT_NEW_ARCH_ENABLED
12-
, RCTTurboModule
13-
#endif
14-
>
5+
@interface RNDocumentPicker : NSObject <NativeRNDocumentPickerSpec>
156
@end
167

17-
NS_ASSUME_NONNULL_END
8+
#else
9+
10+
#import <React/RCTBridgeModule.h>
11+
12+
@interface RNDocumentPicker : NSObject <RCTBridgeModule>
13+
@end
14+
15+
#endif

macos/RNDocumentPicker.mm

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,93 @@
11
#import "RNDocumentPicker.h"
2-
#import <React/RCTUtils.h>
2+
33
#import <Cocoa/Cocoa.h>
4-
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
54
#import <CoreServices/CoreServices.h>
6-
7-
#ifdef RCT_NEW_ARCH_ENABLED
8-
#import <ReactCommon/TurboModuleUtils.h>
9-
#import "RNDocumentPickerSpec.h"
10-
#endif
5+
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
116

127
@implementation RNDocumentPicker
138

149
RCT_EXPORT_MODULE();
1510

16-
#pragma mark - React exports
11+
#ifdef RCT_NEW_ARCH_ENABLED
12+
13+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params
14+
{
15+
return std::make_shared<facebook::react::NativeRNDocumentPickerSpecJSI>(params);
16+
}
1717

18-
RCT_EXPORT_METHOD(
19-
pick:(NSDictionary *)options
20-
resolver:(RCTPromiseResolveBlock)resolve
21-
rejecter:(RCTPromiseRejectBlock)reject)
18+
- (void)pick:(JS::NativeRNDocumentPicker::SpecPickOptions &)options
19+
resolve:(RCTPromiseResolveBlock)resolve
20+
reject:(RCTPromiseRejectBlock)reject
21+
{
22+
BOOL allowDirectorySelection = options.allowDirectorySelection().value_or(NO);
23+
BOOL allowFileSelection = options.allowFileSelection().value_or(NO);
24+
BOOL multiple = options.multiple().value_or(NO);
25+
NSArray<NSString *> *allowedUTIs = nil;
26+
27+
if (auto utisOpt = options.allowedUTIs(); utisOpt) {
28+
const auto &vec = *utisOpt;
29+
NSMutableArray<NSString *> *tmp = [NSMutableArray arrayWithCapacity:vec.size()];
30+
for (NSString *uti : vec) [tmp addObject:uti];
31+
allowedUTIs = tmp;
32+
}
33+
34+
[self pickImpl:allowDirectorySelection
35+
allowFileSelection:allowFileSelection
36+
multiple:multiple
37+
allowedUTIs:allowedUTIs
38+
resolve:resolve
39+
reject:reject];
40+
}
41+
42+
#else
43+
44+
RCT_EXPORT_METHOD(pick:(NSDictionary<NSString *, id> * _Nonnull)options
45+
resolve:(RCTPromiseResolveBlock)resolve
46+
reject:(RCTPromiseRejectBlock)reject)
47+
{
48+
BOOL allowDirectorySelection = [options[@"allowDirectorySelection"] boolValue];
49+
BOOL allowFileSelection = [options[@"allowFileSelection"] boolValue];
50+
BOOL multiple = [options[@"multiple"] boolValue];
51+
NSArray<NSString *> *allowedUTIs = nil;
52+
53+
if ([options[@"allowedUTIs"] isKindOfClass:[NSArray class]]) {
54+
allowedUTIs = (NSArray<NSString *> *)options[@"allowedUTIs"];
55+
}
56+
57+
[self pickImpl:allowDirectorySelection
58+
allowFileSelection:allowFileSelection
59+
multiple:multiple
60+
allowedUTIs:allowedUTIs
61+
resolve:resolve
62+
reject:reject];
63+
}
64+
65+
#endif
66+
67+
- (void)pickImpl:(BOOL)allowDirectorySelection
68+
allowFileSelection:(BOOL)allowFileSelection
69+
multiple:(BOOL)multiple
70+
allowedUTIs:(NSArray<NSString *> *)allowedUTIs
71+
resolve:(RCTPromiseResolveBlock)resolve
72+
reject:(RCTPromiseRejectBlock)reject
2273
{
2374
dispatch_async(dispatch_get_main_queue(), ^{
2475
NSOpenPanel *panel = [NSOpenPanel openPanel];
2576

26-
BOOL allowDirectories = [options objectForKey:@"allowDirectorySelection"] ? [options[@"allowDirectorySelection"] boolValue] : NO;
27-
BOOL allowFiles = [options objectForKey:@"allowFileSelection"] ? [options[@"allowFileSelection"] boolValue] : YES;
28-
BOOL multiple = [options objectForKey:@"multiple"] ? [options[@"multiple"] boolValue] : NO;
29-
NSArray *utis = [options objectForKey:@"allowedUTIs"];
30-
31-
panel.canChooseDirectories = allowDirectories;
32-
panel.canChooseFiles = allowFiles;
77+
panel.canChooseDirectories = allowDirectorySelection;
78+
panel.canChooseFiles = allowFileSelection;
3379
panel.allowsMultipleSelection = multiple;
34-
panel.resolvesAliases = YES;
35-
36-
if (utis != nil && [utis isKindOfClass:[NSArray class]] && utis.count > 0) {
37-
if (@available(macOS 11.0, *)) {
38-
NSMutableArray<UTType *> *contentTypes = [NSMutableArray new];
39-
for (NSString *uti in utis) {
40-
UTType *type = [UTType typeWithIdentifier:uti];
41-
if (type) {
42-
[contentTypes addObject:type];
43-
}
80+
panel.resolvesAliases = YES;
81+
82+
if (allowedUTIs != nil && [allowedUTIs isKindOfClass:[NSArray class]] && allowedUTIs.count > 0) {
83+
NSMutableArray<UTType *> *contentTypes = [NSMutableArray new];
84+
for (NSString *uti in allowedUTIs) {
85+
UTType *type = [UTType typeWithIdentifier:uti];
86+
if (type) {
87+
[contentTypes addObject:type];
4488
}
45-
panel.allowedContentTypes = contentTypes;
46-
} else {
47-
panel.allowedFileTypes = utis;
4889
}
90+
panel.allowedContentTypes = contentTypes;
4991
}
5092

5193
NSInteger result = [panel runModal];
@@ -58,20 +100,9 @@ @implementation RNDocumentPicker
58100
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil];
59101
NSNumber *size = attributes[NSFileSize];
60102
NSString *mimeType = nil;
61-
if (@available(macOS 11.0, *)) {
62-
UTType *uti = [url resourceValuesForKeys:@[NSURLContentTypeKey] error:nil][NSURLContentTypeKey];
63-
if (uti) {
64-
mimeType = uti.preferredMIMEType;
65-
}
66-
} else {
67-
NSString *uti = nil;
68-
[url getResourceValue:&uti forKey:NSURLTypeIdentifierKey error:nil];
69-
if (uti) {
70-
CFStringRef mimeTypeCF = UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti, kUTTagClassMIMEType);
71-
if (mimeTypeCF) {
72-
mimeType = (__bridge_transfer NSString *)mimeTypeCF;
73-
}
74-
}
103+
UTType *uti = [url resourceValuesForKeys:@[NSURLContentTypeKey] error:nil][NSURLContentTypeKey];
104+
if (uti) {
105+
mimeType = uti.preferredMIMEType;
75106
}
76107
NSString *name = url.lastPathComponent;
77108

@@ -100,11 +131,4 @@ @implementation RNDocumentPicker
100131
});
101132
}
102133

103-
#ifdef RCT_NEW_ARCH_ENABLED
104-
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params
105-
{
106-
return std::make_shared<facebook::react::NativeRNDocumentPickerSpec>(params);
107-
}
108-
#endif
109-
110134
@end

0 commit comments

Comments
 (0)