Skip to content

Commit 252ce9f

Browse files
committed
First commit
0 parents  commit 252ce9f

File tree

6 files changed

+708
-0
lines changed

6 files changed

+708
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Xcode
2+
.DS_Store
3+
build/
4+
*.pbxuser
5+
!default.pbxuser
6+
*.mode1v3
7+
!default.mode1v3
8+
*.mode2v3
9+
!default.mode2v3
10+
*.perspectivev3
11+
!default.perspectivev3
12+
*.xcworkspace
13+
!default.xcworkspace
14+
xcuserdata
15+
profile
16+
*.moved-aside
17+
DerivedData
18+
.idea/

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Luke Scott
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

LSDocument.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright (c) 2013 Luke Scott
3+
// https://github.com/lukescott/LSFileWrapper
4+
// Distributed under MIT license
5+
//
6+
7+
#import <UIKit/UIKit.h>
8+
9+
@class LSFileWrapper;
10+
11+
@interface LSDocument : UIDocument
12+
13+
- (void)saveWithCompletionHandler:(void (^)(BOOL success))completionHandler;
14+
15+
- (BOOL)writePathFirst:(LSFileWrapper *)path
16+
andAttributes:(NSDictionary *)additionalFileAttributes
17+
safelyToURL:(NSURL *)url
18+
forSaveOperation:(UIDocumentSaveOperation)saveOperation
19+
error:(NSError *__autoreleasing *)outError;
20+
- (void)savePathFirst:(NSString *)path
21+
firstHandler:(void (^)(BOOL success))firstHandler
22+
completionHandler:(void (^)(BOOL success))completionHandler;
23+
24+
@end

LSDocument.m

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// Copyright (c) 2013 Luke Scott
3+
// https://github.com/lukescott/LSFileWrapper
4+
// Distributed under MIT license
5+
//
6+
7+
#import "LSDocument.h"
8+
#import "LSFileWrapper.h"
9+
10+
@interface LSFileWrapper ()
11+
@property (nonatomic, strong) NSURL *url;
12+
@end
13+
14+
@implementation LSDocument
15+
16+
- (BOOL)writeContents:(LSFileWrapper *)contents
17+
andAttributes:(NSDictionary *)additionalFileAttributes
18+
safelyToURL:(NSURL *)url
19+
forSaveOperation:(UIDocumentSaveOperation)saveOperation
20+
error:(NSError *__autoreleasing *)outError
21+
{
22+
return [contents writeUpdatesToURL:self.fileURL error:outError];
23+
}
24+
25+
- (void)saveWithCompletionHandler:(void (^)(BOOL success))completionHandler
26+
{
27+
[self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:completionHandler];
28+
}
29+
30+
- (BOOL)writePathFirst:(LSFileWrapper *)file
31+
andAttributes:(NSDictionary *)additionalFileAttributes
32+
safelyToURL:(NSURL *)url
33+
forSaveOperation:(UIDocumentSaveOperation)saveOperation
34+
error:(NSError *__autoreleasing *)outError
35+
{
36+
return [file writeUpdatesToURL:url error:outError];
37+
}
38+
39+
- (void)savePathFirst:(NSString *)path
40+
firstHandler:(void (^)(BOOL success))firstHandler
41+
completionHandler:(void (^)(BOOL success))completionHandler
42+
{
43+
NSError *contentError;
44+
NSURL *url = [self.fileURL copy];
45+
LSFileWrapper *contents = [self contentsForType:self.fileType error:&contentError];
46+
if(!contents) {
47+
return;
48+
}
49+
50+
LSFileWrapper *file = [contents fileWrapperWithPath:path];
51+
NSURL *firstURL = [url URLByAppendingPathComponent:path];
52+
53+
[self performAsynchronousFileAccessUsingBlock:^{
54+
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
55+
56+
[fileCoordinator coordinateWritingItemAtURL:url
57+
options:NSFileCoordinatorWritingForMerging
58+
error:nil
59+
byAccessor:
60+
^(NSURL *newURL) {
61+
NSError *writeError;
62+
BOOL wroteFirst, wroteAll;
63+
64+
wroteFirst = [self writePathFirst:file
65+
andAttributes:nil
66+
safelyToURL:firstURL
67+
forSaveOperation:UIDocumentSaveForOverwriting
68+
error:&writeError];
69+
70+
if(firstHandler) {
71+
dispatch_async(dispatch_get_main_queue(), ^(void) {
72+
firstHandler(wroteFirst);
73+
});
74+
}
75+
76+
if(wroteFirst) {
77+
wroteAll = [self writeContents:contents
78+
andAttributes:nil
79+
safelyToURL:url
80+
forSaveOperation:UIDocumentSaveForOverwriting
81+
error:&writeError];
82+
}
83+
84+
if(completionHandler) {
85+
dispatch_async(dispatch_get_main_queue(), ^(void) {
86+
completionHandler(wroteAll);
87+
});
88+
}
89+
}];
90+
}];
91+
}
92+
93+
- (BOOL)readFromURL:(NSURL *)url error:(NSError *__autoreleasing *)outError
94+
{
95+
__block LSFileWrapper *wrapper = [[LSFileWrapper alloc] initWithURL:url isDirectory:NO];
96+
__block BOOL result;
97+
dispatch_sync(dispatch_get_main_queue(), ^(void) {
98+
result = [self loadFromContents:wrapper
99+
ofType:self.fileType
100+
error:outError];
101+
});
102+
[wrapper loadCache];
103+
return result;
104+
}
105+
106+
@end

LSFileWrapper.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Copyright (c) 2013 Luke Scott
3+
// https://github.com/lukescott/LSFileWrapper
4+
// Distributed under MIT license
5+
//
6+
7+
#import <Foundation/Foundation.h>
8+
#import <AssetsLibrary/AssetsLibrary.h>
9+
10+
@interface LSFileWrapper : NSObject
11+
12+
- (id)initFile;
13+
- (id)initDirectory;
14+
- (id)initWithURL:(NSURL *)url isDirectory:(BOOL)isDir;
15+
16+
- (void)loadCache;
17+
18+
- (NSData *)data;
19+
- (NSString *)string;
20+
- (NSDictionary *)dictionary;
21+
- (UIImage *)image;
22+
23+
- (void)updateContent:(id<NSObject>)content;
24+
- (void)deleteContent;
25+
26+
- (LSFileWrapper *)fileWrapperWithPath:(NSString *)path;
27+
- (LSFileWrapper *)fileWrapperWithPath:(NSString *)path create:(BOOL)create isDirectory:(BOOL)isDir;
28+
29+
- (NSString *)addFileWrapper:(LSFileWrapper *)fileWrapper withFilename:(NSString *)filename;
30+
- (void)setFileWrapper:(LSFileWrapper *)fileWrapper withFilename:(NSString *)filename;
31+
- (NSString *)addContent:(id<NSObject>)content_ withFilename:(NSString *)filename;
32+
- (void)setContent:(id<NSObject>)content_ withFilename:(NSString *)filename;
33+
34+
- (BOOL)writeUpdatesToURL:(NSURL *)url error:(NSError *__autoreleasing *)outError;
35+
36+
@property (readonly, strong, nonatomic) NSString *filename;
37+
@property (readonly, strong, nonatomic) NSString *fileType;
38+
@property (readonly, nonatomic) BOOL updated;
39+
@property (readonly, nonatomic) BOOL isDirectory;
40+
@property (assign, nonatomic) BOOL cacheFile;
41+
@end

0 commit comments

Comments
 (0)