Skip to content

Commit 2167720

Browse files
author
Tj
committed
Some style updates, warning fixes. Mainly adding path fetching for an arbitrary NSBundle. Not ready to call this done, but branching and pushing for now.
1 parent 8fe2479 commit 2167720

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

NSArray+Map.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
@implementation NSArray (Map)
88

9-
- (NSArray *)rnfs_mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
9+
- (NSArray *)rnfs_mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block
10+
{
1011
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
12+
1113
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
1214
[result addObject:block(obj, idx)];
1315
}];
16+
1417
return result;
1518
}
1619

RNFSManager.m

+50-22
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,36 @@ @implementation RNFSManager
1414

1515
static int MainBundleDirectory = 999;
1616

17-
@synthesize bridge = _bridge;
1817
RCT_EXPORT_MODULE();
1918

20-
- (dispatch_queue_t)methodQueue {
19+
- (dispatch_queue_t)methodQueue
20+
{
2121
return dispatch_queue_create("pe.lum.rnfs", DISPATCH_QUEUE_SERIAL);
2222
}
2323

24-
RCT_EXPORT_METHOD(readDir:(NSString*)directory inFolder:(NSNumber*)folder callback:(RCTResponseSenderBlock)callback){
24+
RCT_EXPORT_METHOD(readDir:(NSString *)directory
25+
inFolder:(nonnull NSNumber *)folder
26+
callback:(RCTResponseSenderBlock)callback)
27+
{
2528
NSString *path;
26-
int folderInt = [folder integerValue];
29+
NSInteger folderInt = [folder integerValue];
2730

2831
if (folderInt == MainBundleDirectory) {
2932
path = [[NSBundle mainBundle] bundlePath];
3033
} else {
3134
NSArray *paths = NSSearchPathForDirectoriesInDomains(folderInt, NSUserDomainMask, YES);
32-
path = [paths objectAtIndex:0];
35+
path = [paths firstObject];
3336
}
3437

3538
NSFileManager *fileManager = [NSFileManager defaultManager];
3639
NSError *error = nil;
37-
NSString * dirPath = [path stringByAppendingPathComponent:directory];
40+
NSString *dirPath = [path stringByAppendingPathComponent:directory];
3841
NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error];
3942

40-
contents = [contents rnfs_mapObjectsUsingBlock:^id(id obj, NSUInteger idx) {
43+
contents = [contents rnfs_mapObjectsUsingBlock:^id(NSString *obj, NSUInteger idx) {
4144
return @{
42-
@"name": (NSString*)obj,
43-
@"path": [dirPath stringByAppendingPathComponent:(NSString*)obj]
45+
@"name": obj,
46+
@"path": [dirPath stringByAppendingPathComponent:obj]
4447
};
4548
}];
4649

@@ -51,7 +54,9 @@ - (dispatch_queue_t)methodQueue {
5154
callback(@[[NSNull null], contents]);
5255
}
5356

54-
RCT_EXPORT_METHOD(stat:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
57+
RCT_EXPORT_METHOD(stat:(NSString *)filepath
58+
callback:(RCTResponseSenderBlock)callback)
59+
{
5560
NSError *error = nil;
5661
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
5762

@@ -60,17 +65,21 @@ - (dispatch_queue_t)methodQueue {
6065
}
6166

6267
attributes = @{
63-
@"ctime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileCreationDate]],
64-
@"mtime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileModificationDate]],
68+
@"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]],
69+
@"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]],
6570
@"size": [attributes objectForKey:NSFileSize],
6671
@"type": [attributes objectForKey:NSFileType],
67-
@"mode": [NSNumber numberWithInteger:[[NSString stringWithFormat:@"%o", [(NSNumber*)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]]
72+
@"mode": @([[NSString stringWithFormat:@"%ld", (long)[(NSNumber *)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue])
6873
};
6974

7075
callback(@[[NSNull null], attributes]);
7176
}
7277

73-
RCT_EXPORT_METHOD(writeFile:(NSString*)filepath contents:(NSString*)base64Content attributes:(NSDictionary*)attributes callback:(RCTResponseSenderBlock)callback){
78+
RCT_EXPORT_METHOD(writeFile:(NSString *)filepath
79+
contents:(NSString *)base64Content
80+
attributes:(NSDictionary *)attributes
81+
callback:(RCTResponseSenderBlock)callback)
82+
{
7483
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];
7584
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
7685

@@ -81,7 +90,9 @@ - (dispatch_queue_t)methodQueue {
8190
callback(@[[NSNull null], [NSNumber numberWithBool:success]]);
8291
}
8392

84-
RCT_EXPORT_METHOD(unlink:(NSString*)filepath callback:(RCTResponseSenderBlock)callback) {
93+
RCT_EXPORT_METHOD(unlink:(NSString*)filepath
94+
callback:(RCTResponseSenderBlock)callback)
95+
{
8596
NSFileManager *manager = [NSFileManager defaultManager];
8697
BOOL exists = [manager fileExistsAtPath:filepath isDirectory:false];
8798

@@ -98,7 +109,9 @@ - (dispatch_queue_t)methodQueue {
98109
callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]);
99110
}
100111

101-
RCT_EXPORT_METHOD(readFile:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
112+
RCT_EXPORT_METHOD(readFile:(NSString *)filepath
113+
callback:(RCTResponseSenderBlock)callback)
114+
{
102115
NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath];
103116
NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
104117

@@ -109,20 +122,35 @@ - (dispatch_queue_t)methodQueue {
109122
callback(@[[NSNull null], base64Content]);
110123
}
111124

112-
- (NSNumber*) dateToTimeIntervalNumber:(NSDate*)date {
113-
return [NSNumber numberWithDouble:[date timeIntervalSince1970]];
125+
RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed
126+
callback:(RCTResponseSenderBlock)callback)
127+
{
128+
NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingFormat:@"/%@.bundle", bundleNamed];
129+
NSBundle *bundle = [NSBundle bundleWithPath:path];
130+
if (!bundle.isLoaded) {
131+
[bundle load];
132+
}
133+
134+
callback(@[[NSNull null], path]);
135+
}
136+
137+
- (NSNumber *) dateToTimeIntervalNumber:(NSDate *)date
138+
{
139+
return @([date timeIntervalSince1970]);
114140
}
115141

116-
- (NSArray*) makeErrorPayload:(NSError*)error {
142+
- (NSArray *)makeErrorPayload:(NSError *)error
143+
{
117144
return @[@{
118145
@"description": error.localizedDescription,
119-
@"code": [NSNumber numberWithInteger:error.code]
146+
@"code": @(error.code)
120147
}];
121148
}
122149

123-
- (NSString*) getPathForDirectory:(int)directory {
150+
- (NSString *)getPathForDirectory:(int)directory
151+
{
124152
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
125-
return [paths objectAtIndex:0];
153+
return [paths firstObject];
126154
}
127155

128156
- (NSDictionary *)constantsToExport

0 commit comments

Comments
 (0)