Skip to content

Commit c8d7b3e

Browse files
committed
🛠 Cleaned up and fixed a few minor issues. Added support for removing wrappers by path.
1 parent df8cd64 commit c8d7b3e

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

LSFileWrapper.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
118118
*
119119
* @warning Should only be called on the LSFileWrapper of type Directory.
120120
*
121-
* @param path The path of child wrapper as NSString.
121+
* @param path Relative path of child wrapper as NSString.
122122
*
123123
* @return Optional stored child wrapper as LSFileWrapper.
124124
*/
@@ -129,7 +129,7 @@ FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
129129
*
130130
* @warning Should only be called on the LSFileWrapper of type Directory.
131131
*
132-
* @param path The path of child wrapper as NSString.
132+
* @param path Relative path of child wrapper as NSString.
133133
* @param create Boolean indicating if LSFileWrapper should be created at specified path when none is found.
134134
* @param isDir Boolean indicating if LSFileWrapper that should be created at specified path should be of type Directory.
135135
*
@@ -175,9 +175,9 @@ FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
175175
*
176176
* @warning Should only be called on the LSFileWrapper of type Directory.
177177
*
178-
* @param filename Child wrapper's filename which should be removed from the current LSFileWrapper.
178+
* @param path Relative path to child wrapper which should be removed from the current LSFileWrapper tree.
179179
*/
180-
- (void)removeFileWrapperWithFilename:(nonnull NSString *)filename NS_SWIFT_NAME(removeFileWrapper(with:));
180+
- (void)removeFileWrapperWithPath:(nonnull NSString *)path NS_SWIFT_NAME(removeFileWrapper(with:));
181181

182182
/**
183183
* @brief Adds a new child wrapper of type File with the supplied name to the current LSFileWrapper. If a wrapper is already present with the same name, then the new wrapper will be saved under the returned named to prevent collisions.
@@ -262,7 +262,6 @@ FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
262262

263263
@property (readonly, strong, nonatomic, nullable) NSString *filename;
264264
@property (readonly, strong, nonatomic, nullable) NSString *fileType;
265-
@property (readonly, strong, nonatomic, nullable) NSMutableDictionary<NSString*, LSFileWrapper*> *fileWrappers;
266265
@property (readonly, strong, nonatomic, nullable) NSURL *writtenURL;
267266
@property (readonly, nonatomic) BOOL updated;
268267
@property (readonly, nonatomic) BOOL isDirectory;

LSFileWrapper.m

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ - (UIImage *)image
160160
return nil;
161161
}
162162
#elif TARGET_OS_OSX
163-
- (NSImage *)image {
163+
- (NSImage *)image
164+
{
164165
if (content == nil && writtenURL != nil) {
165166
content = [[NSImage alloc] initWithContentsOfFile:writtenURL.path];
166167
cacheFile = YES;
@@ -262,7 +263,8 @@ - (void)setFileWrapper:(LSFileWrapper *)fileWrapper withFilename:(NSString *)fil
262263
[self setFileWrapper:fileWrapper filename:filename_ replace:YES];
263264
}
264265

265-
- (void)removeFileWrapper:(LSFileWrapper *)fileWrapper {
266+
- (void)removeFileWrapper:(LSFileWrapper *)fileWrapper
267+
{
266268
if (!isDirectory) {
267269
@throw [NSException exceptionWithName:NSInternalInconsistencyException
268270
reason:@"LSFileWrapper file is not a directory."
@@ -275,24 +277,26 @@ - (void)removeFileWrapper:(LSFileWrapper *)fileWrapper {
275277
userInfo:nil];
276278
return;
277279
}
278-
[self removeFileWrapperWithFilename:fileWrapper.filename];
280+
[self removeFileWrapperWithPath:fileWrapper.filename];
279281
}
280282

281-
- (void)removeFileWrapperWithFilename:(NSString *)filename_ {
282-
if (!isDirectory) {
283-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
284-
reason:@"LSFileWrapper file is not a directory."
285-
userInfo:nil];
283+
- (void)removeFileWrapperWithPath:(NSString *)path
284+
{
285+
NSString *dirpath = [path stringByDeletingLastPathComponent];
286+
NSString *filename_ = [path lastPathComponent];
287+
LSFileWrapper *dirFileWrapper = [self walkDirectoryPath:dirpath create:NO];
288+
289+
if (!dirFileWrapper) {
286290
return;
287291
}
288-
LSFileWrapper *existing = [fileWrappers objectForKey:filename_];
292+
LSFileWrapper *existing = [dirFileWrapper.fileWrappers objectForKey:filename_];
289293
if (existing.writtenURL) {
290294
existing.deleted = YES;
291295
}
292296
else {
293-
[fileWrappers removeObjectForKey:filename_];
297+
[[dirFileWrapper fileWrappers] removeObjectForKey:filename_];
294298
}
295-
updated = YES;
299+
dirFileWrapper.updated = YES;
296300
}
297301

298302
- (NSString *)addContent:(id<NSObject>)content_ withFilename:(NSString *)filename_
@@ -332,7 +336,8 @@ - (BOOL)writeUpdatesToURL:(NSURL *)url error:(NSError *__autoreleasing *)outErro
332336
return [self writeUpdates:updates filemanager:[[NSFileManager alloc] init] error:outError];
333337
}
334338

335-
- (BOOL)writeToURL:(NSURL *)url error:(NSError *__autoreleasing *)outError {
339+
- (BOOL)writeToURL:(NSURL *)url error:(NSError *__autoreleasing *)outError
340+
{
336341
NSMutableArray *updates = [[NSMutableArray alloc] init];
337342

338343
if (parent) {
@@ -346,7 +351,8 @@ - (BOOL)writeToURL:(NSURL *)url error:(NSError *__autoreleasing *)outError {
346351
}
347352

348353
#if TARGET_OS_OSX
349-
- (BOOL)writeToURL:(NSURL *)url forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(nullable NSURL *)absoluteOriginalContentsURL backupDocumentURL:(nullable NSURL *)backupFileURL error:(NSError *__autoreleasing *)outError {
354+
- (BOOL)writeToURL:(NSURL *)url forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(nullable NSURL *)absoluteOriginalContentsURL backupDocumentURL:(nullable NSURL *)backupFileURL error:(NSError *__autoreleasing *)outError
355+
{
350356
switch (saveOperation) {
351357
case NSAutosaveInPlaceOperation:
352358
// Auto overwrite
@@ -483,8 +489,10 @@ - (NSString *)setFileWrapper:(LSFileWrapper *)fileWrapper filename:(NSString *)f
483489
format = [NSString stringWithFormat:@"%@ %%d", basename];
484490
}
485491
while (true) {
492+
// Checking both cases for iOS and macOS interoperability
486493
LSFileWrapper *existing = [fileWrappers objectForKey:filename_];
487-
if (!existing || existing.deleted) {
494+
LSFileWrapper *existingCase = [fileWrappers objectForKey:[filename_ lowercaseString]];
495+
if ((!existing || existing.deleted) && (!existingCase || existingCase.deleted)) {
488496
break;
489497
}
490498
filename_ = [NSString stringWithFormat:format, ++num];

0 commit comments

Comments
 (0)