This repository was archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathDocumentFolder.m
233 lines (164 loc) · 8.06 KB
/
DocumentFolder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// DocumentFolder.m
// Viewer v1.0.0
//
// Created by Julius Oklamcak on 2012-09-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "DocumentFolder.h"
#import "ReaderDocument.h"
@implementation DocumentFolder
#pragma mark Constants
#define kDocumentFolder @"DocumentFolder"
#pragma mark Properties
@dynamic name;
@dynamic type;
@dynamic documents;
@synthesize isChecked;
#pragma mark DocumentFolder Core Data class methods
+ (NSArray *)allInMOC:(NSManagedObjectContext *)inMOC
{
assert(inMOC != nil); // Check parameter
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kDocumentFolder inManagedObjectContext:inMOC]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; // Sort order
[request setReturnsObjectsAsFaults:NO]; [request setFetchBatchSize:24]; // Optimize fetch
__autoreleasing NSError *error = nil; // Error information object
NSArray *objectList = [inMOC executeFetchRequest:request error:&error];
if (objectList == nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return objectList;
}
+ (BOOL)existsInMOC:(NSManagedObjectContext *)inMOC name:(NSString *)string
{
assert(inMOC != nil); assert(string != nil); // Check parameters
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kDocumentFolder inManagedObjectContext:inMOC]];
[request setPredicate:[NSPredicate predicateWithFormat:@"name == %@", string]]; // Name predicate
__autoreleasing NSError *error = nil; // Error information object
NSUInteger count = [inMOC countForFetchRequest:request error:&error];
if (error != nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return ((count > 0) ? YES : NO);
}
+ (BOOL)existsInMOC:(NSManagedObjectContext *)inMOC type:(DocumentFolderType)kind
{
assert(inMOC != nil); // Check parameter
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kDocumentFolder inManagedObjectContext:inMOC]];
[request setPredicate:[NSPredicate predicateWithFormat:@"type == %d", kind]]; // Type predicate
__autoreleasing NSError *error = nil; // Error information object
NSUInteger count = [inMOC countForFetchRequest:request error:&error];
if (error != nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return ((count > 0) ? YES : NO);
}
+ (DocumentFolder *)folderInMOC:(NSManagedObjectContext *)inMOC type:(DocumentFolderType)kind
{
assert(inMOC != nil); // Check parameter
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kDocumentFolder inManagedObjectContext:inMOC]];
[request setPredicate:[NSPredicate predicateWithFormat:@"type == %d", kind]]; // Type predicate
[request setReturnsObjectsAsFaults:NO]; //[request setFetchBatchSize:24]; // Optimize fetch
__autoreleasing NSError *error = nil; // Error information object
NSArray *objectList = [inMOC executeFetchRequest:request error:&error];
if (objectList == nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return [objectList lastObject];
}
+ (DocumentFolder *)insertInMOC:(NSManagedObjectContext *)inMOC name:(NSString *)string type:(DocumentFolderType)kind
{
assert(inMOC != nil); assert(string != nil); // Check parameters
DocumentFolder *object = [NSEntityDescription insertNewObjectForEntityForName:kDocumentFolder inManagedObjectContext:inMOC];
if ((object != nil) && ([object isMemberOfClass:[DocumentFolder class]])) // Valid DocumentFolder object
{
object.name = string; object.type = [NSNumber numberWithInteger:kind]; // Set name and type
__autoreleasing NSError *error = nil; // Error information object
if ([inMOC hasChanges] == YES) // Save changes
{
if ([inMOC save:&error] == YES) // Did save changes
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[object objectID] forKey:DocumentFolderNotificationObjectID];
[notificationCenter postNotificationName:DocumentFolderAddedNotification object:nil userInfo:userInfo];
}
else // Log any errors
{
NSLog(@"%s %@", __FUNCTION__, error); assert(NO);
}
}
}
return object;
}
+ (void)renameInMOC:(NSManagedObjectContext *)inMOC objectID:(NSManagedObjectID *)objectID name:(NSString *)string
{
assert(inMOC != nil); assert(objectID != nil); assert(string != nil); // Check parameters
DocumentFolder *object = (id)[inMOC existingObjectWithID:objectID error:NULL]; // Get object
if ((object != nil) && ([object isMemberOfClass:[DocumentFolder class]])) // Valid object
{
object.name = string; // Update folder name
__autoreleasing NSError *error = nil; // Error information object
if ([inMOC hasChanges] == YES) // Save changes
{
if ([inMOC save:&error] == YES) // Did save changes
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[object objectID] forKey:DocumentFolderNotificationObjectID];
[notificationCenter postNotificationName:DocumentFolderRenamedNotification object:nil userInfo:userInfo];
}
else // Log any errors
{
NSLog(@"%s %@", __FUNCTION__, error); assert(NO);
}
}
}
}
+ (void)deleteInMOC:(NSManagedObjectContext *)inMOC objectID:(NSManagedObjectID *)objectID
{
assert(inMOC != nil); assert(objectID != nil); // Check parameters
DocumentFolder *object = (id)[inMOC existingObjectWithID:objectID error:NULL]; // Get object
if ((object != nil) && ([object isMemberOfClass:[DocumentFolder class]])) // Valid object
{
[inMOC deleteObject:object]; // Delete object
__autoreleasing NSError *error = nil; // Error information object
if ([inMOC hasChanges] == YES) // Save changes
{
if ([inMOC save:&error] == YES) // Did save changes
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[object objectID] forKey:DocumentFolderNotificationObjectID];
[notificationCenter postNotificationName:DocumentFolderDeletedNotification object:nil userInfo:userInfo];
}
else // Log any errors
{
NSLog(@"%s %@", __FUNCTION__, error); assert(NO);
}
}
}
}
#pragma mark DocumentFolder Core Data instance methods
- (void)willTurnIntoFault
{
self.isChecked = NO;
}
#pragma mark Notification name strings
NSString *const DocumentFolderAddedNotification = @"DocumentFolderAddedNotification";
NSString *const DocumentFolderRenamedNotification = @"DocumentFolderRenamedNotification";
NSString *const DocumentFolderDeletedNotification = @"DocumentFolderDeletedNotification";
NSString *const DocumentFolderNotificationObjectID = @"DocumentFolderNotificationObjectID";
NSString *const DocumentFoldersDeletedNotification = @"DocumentFoldersDeletedNotification";
@end