This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
/
ASHCoreDataStack.m
384 lines (322 loc) · 21.4 KB
/
ASHCoreDataStack.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// ASHCoreDataStack.m
// C-41
//
// Created by Samuel E. Giddins on 12/22/13.
// Copyright (c) 2013 Ash Furrow. All rights reserved.
//
#import "ASHCoreDataStack.h"
// Models
#import "ASHRecipe.h"
#import "ASHStep.h"
@interface ASHCoreDataStack ()
@property (readwrite, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readwrite, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readwrite, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
@implementation ASHCoreDataStack
+(instancetype)defaultStack {
static ASHCoreDataStack *stack;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
stack = [[self alloc] init];
});
return stack;
}
+(instancetype)inMemoryStack {
static ASHCoreDataStack *stack;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
stack = [[self alloc] init];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[stack managedObjectModel]];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
stack.persistentStoreCoordinator = persistentStoreCoordinator;
});
return stack;
}
-(void)saveContext {
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"C_41" withExtension:@"momd"];
self.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return self.managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"C_41.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#pragma mark - Public Methods
-(void)ensureInitialLoad {
NSString *initialLoadKey = @"Initial Load";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasInitialLoad = [userDefaults boolForKey:initialLoadKey];
if (hasInitialLoad == NO) {
[userDefaults setBool:YES forKey:initialLoadKey];
{
ASHRecipe *c41Recipe = [NSEntityDescription insertNewObjectForEntityForName:@"ASHRecipe" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
c41Recipe.name = NSLocalizedString(@"C-41 Colour Process", @"Initial setup title");
c41Recipe.blurb = NSLocalizedString(@"Standard C-41 colour negative film recipe.", @"Initial setup subtitle");
c41Recipe.filmType = ASHRecipeFilmTypeColourNegative;
ASHStep *prewashStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
prewashStep.recipe = c41Recipe;
prewashStep.name = NSLocalizedString(@"Prewash", @"C-41 Prewash step name");
prewashStep.blurb = NSLocalizedString(@"Water", @"C-41 prewas step description");
prewashStep.temperatureC = 39;
prewashStep.duration = 60;
ASHStep *developerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
developerStep.recipe = c41Recipe;
developerStep.name = NSLocalizedString(@"Developer", @"C-41 developer step name");
developerStep.temperatureC = 39;
developerStep.agitationDuration = 10;
developerStep.agitationFrequency = 60;
developerStep.duration = 210;
ASHStep *blixStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
blixStep.recipe = c41Recipe;
blixStep.name = NSLocalizedString(@"Blix", @"C-41 blix step name");
blixStep.temperatureC = 39;
blixStep.agitationDuration = 10;
blixStep.agitationFrequency = 60;
blixStep.duration = 390;
ASHStep *washStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
washStep.recipe = c41Recipe;
washStep.name = NSLocalizedString(@"Wash", @"C-41 wash step name");
washStep.blurb = NSLocalizedString(@"Water", @"C-41 was step description");
washStep.temperatureC = 23;
washStep.agitationDuration = 0;
washStep.agitationFrequency = 0;
washStep.duration = 180;
ASHStep *stabilizerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
stabilizerStep.recipe = c41Recipe;
stabilizerStep.name = NSLocalizedString(@"Stabilizer", @"C-41 stabilizer step name");
stabilizerStep.temperatureC = 23;
stabilizerStep.agitationDuration = 15;
stabilizerStep.agitationFrequency = 60;
stabilizerStep.duration = 60;
c41Recipe.steps = [NSOrderedSet orderedSetWithArray:@[prewashStep, developerStep, blixStep, washStep, stabilizerStep]];
}
{
ASHRecipe *e6Recipe = [NSEntityDescription insertNewObjectForEntityForName:@"ASHRecipe" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
e6Recipe.name = NSLocalizedString(@"E-6 Colour Process", @"Initial setup title");
e6Recipe.blurb = NSLocalizedString(@"Standard E-6 colour positive film recipe.", @"Initial setup subtitle");
e6Recipe.filmType = ASHRecipeFilmTypeColourPositive;
ASHStep *prewashStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
prewashStep.recipe = e6Recipe;
prewashStep.name = NSLocalizedString(@"Prewash", @"E-6 Prewash step name");
prewashStep.blurb = NSLocalizedString(@"Water", @"E-6 prewas step description");
prewashStep.temperatureC = 38;
prewashStep.duration = 60;
ASHStep *firstDeveloperStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
firstDeveloperStep.recipe = e6Recipe;
firstDeveloperStep.name = NSLocalizedString(@"First Developer", @"E-6 developer step name");
firstDeveloperStep.temperatureC = 38;
firstDeveloperStep.agitationDuration = 5;
firstDeveloperStep.agitationFrequency = 15;
firstDeveloperStep.duration = 360;
ASHStep *firstWashStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
firstWashStep.recipe = e6Recipe;
firstWashStep.name = NSLocalizedString(@"Wash", @"E-6 wash step name");
firstWashStep.blurb = NSLocalizedString(@"Water", @"E-6 was step description");
firstWashStep.temperatureC = 38;
firstWashStep.agitationDuration = 0;
firstWashStep.agitationFrequency = 0;
firstWashStep.duration = 150;
ASHStep *colourDeveloperStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
colourDeveloperStep.recipe = e6Recipe;
colourDeveloperStep.name = NSLocalizedString(@"Colour Developer", @"E-6 developer step name");
colourDeveloperStep.temperatureC = 38;
colourDeveloperStep.agitationDuration = 5;
colourDeveloperStep.agitationFrequency = 15;
colourDeveloperStep.duration = 360;
ASHStep *secondWashStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
secondWashStep.recipe = e6Recipe;
secondWashStep.name = NSLocalizedString(@"Wash", @"E-6 wash step name");
secondWashStep.blurb = NSLocalizedString(@"Water", @"E-6 was step description");
secondWashStep.temperatureC = 38;
secondWashStep.agitationDuration = 0;
secondWashStep.agitationFrequency = 0;
secondWashStep.duration = 150;
ASHStep *blixStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
blixStep.recipe = e6Recipe;
blixStep.name = NSLocalizedString(@"Blix", @"E-6 blix step name");
blixStep.temperatureC = 38;
blixStep.agitationDuration = 5;
blixStep.agitationFrequency = 15;
blixStep.duration = 360;
ASHStep *finalWashStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
finalWashStep.recipe = e6Recipe;
finalWashStep.name = NSLocalizedString(@"Wash", @"E-6 wash step name");
finalWashStep.blurb = NSLocalizedString(@"Water", @"E-6 was step description");
finalWashStep.temperatureC = 38;
finalWashStep.agitationDuration = 0;
finalWashStep.agitationFrequency = 0;
finalWashStep.duration = 240;
e6Recipe.steps = [NSOrderedSet orderedSetWithArray:@[prewashStep, firstDeveloperStep, firstWashStep, colourDeveloperStep, secondWashStep, blixStep, finalWashStep]];
}
{
ASHRecipe *delta3200Recipe = [NSEntityDescription insertNewObjectForEntityForName:@"ASHRecipe" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
delta3200Recipe.name = NSLocalizedString(@"Ilford Delta 3200", @"Initial setup title");
delta3200Recipe.blurb = NSLocalizedString(@"Black and white process for Ilford’s high-ISO film.", @"Initial setup subtitle");
delta3200Recipe.filmType = ASHRecipeFilmTypeBlackAndWhite;
ASHStep *developerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
developerStep.recipe = delta3200Recipe;
developerStep.name = NSLocalizedString(@"Developer", @"Delta 3200 developer step name");
developerStep.blurb = NSLocalizedString(@"Ilfosol", @"Delta 3200 developer step description");
developerStep.temperatureC = 23;
developerStep.agitationDuration = 10;
developerStep.agitationFrequency = 60;
developerStep.duration = 600;
ASHStep *stopBathStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
stopBathStep.recipe = delta3200Recipe;
stopBathStep.name = NSLocalizedString(@"Stop Bath", @"Delta 3200 stop bath step name");
stopBathStep.blurb = NSLocalizedString(@"Ilfostop", @"Delta 3200 stop bath step description");
stopBathStep.temperatureC = 23;
stopBathStep.agitationDuration = 10;
stopBathStep.agitationFrequency = 0;
stopBathStep.duration = 10;
ASHStep *fixerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
fixerStep.recipe = delta3200Recipe;
fixerStep.name = NSLocalizedString(@"Fixer", @"Delta 3200 fixer step name");
fixerStep.blurb = NSLocalizedString(@"Ilford Rapid Fixer", @"Delta 3200 fixer step name");
fixerStep.temperatureC = 23;
fixerStep.agitationDuration = 10;
fixerStep.agitationFrequency = 60;
fixerStep.duration = 180;
ASHStep *washStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
washStep.recipe = delta3200Recipe;
washStep.name = NSLocalizedString(@"Wash", @"Delta 3200 wash step name");
washStep.blurb = NSLocalizedString(@"Water", @"Delta 3200 was step description");
washStep.temperatureC = 23;
washStep.agitationDuration = 0;
washStep.agitationFrequency = 0;
washStep.duration = 300;
delta3200Recipe.steps = [NSOrderedSet orderedSetWithArray:@[developerStep, stopBathStep, fixerStep, washStep]];
}
{
ASHRecipe *delta400Recipe = [NSEntityDescription insertNewObjectForEntityForName:@"ASHRecipe" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
delta400Recipe.name = NSLocalizedString(@"Ilford Delta 400", @"Initial setup title");
delta400Recipe.blurb = NSLocalizedString(@"Black and white process for Ilford’s general-purpose film.", @"Initial setup subtitle");
delta400Recipe.filmType = ASHRecipeFilmTypeBlackAndWhite;
ASHStep *developerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
developerStep.recipe = delta400Recipe;
developerStep.name = NSLocalizedString(@"Developer", @"Delta 400 developer step name");
developerStep.blurb = NSLocalizedString(@"Ilfosol", @"Delta 400 developer step description");
developerStep.temperatureC = 20;
developerStep.agitationDuration = 10;
developerStep.agitationFrequency = 60;
developerStep.duration = 540;
ASHStep *stopBathStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
stopBathStep.recipe = delta400Recipe;
stopBathStep.name = NSLocalizedString(@"Stop Bath", @"Delta 400 stop bath step name");
stopBathStep.blurb = NSLocalizedString(@"Ilfostop", @"Delta 400 stop bath step description");
stopBathStep.temperatureC = 20;
stopBathStep.agitationDuration = 10;
stopBathStep.agitationFrequency = 0;
stopBathStep.duration = 10;
ASHStep *fixerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
fixerStep.recipe = delta400Recipe;
fixerStep.name = NSLocalizedString(@"Fixer", @"Delta 400 fixer step name");
fixerStep.blurb = NSLocalizedString(@"Ilford Rapid Fixer", @"Delta 400 fixer step name");
fixerStep.temperatureC = 20;
fixerStep.agitationDuration = 10;
fixerStep.agitationFrequency = 60;
fixerStep.duration = 180;
ASHStep *washStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
washStep.recipe = delta400Recipe;
washStep.name = NSLocalizedString(@"Wash", @"Delta 400 wash step name");
washStep.blurb = NSLocalizedString(@"Water", @"Delta 400 was step description");
washStep.temperatureC = 20;
washStep.agitationDuration = 0;
washStep.agitationFrequency = 0;
washStep.duration = 300;
delta400Recipe.steps = [NSOrderedSet orderedSetWithArray:@[developerStep, stopBathStep, fixerStep, washStep]];
}
{
ASHRecipe *delta100Recipe = [NSEntityDescription insertNewObjectForEntityForName:@"ASHRecipe" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
delta100Recipe.name = NSLocalizedString(@"Ilford Delta 100", @"Initial setup title");
delta100Recipe.blurb = NSLocalizedString(@"Black and white process for Ilford’s low-ISO film.", @"Initial setup subtitle");
delta100Recipe.filmType = ASHRecipeFilmTypeBlackAndWhite;
ASHStep *developerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
developerStep.recipe = delta100Recipe;
developerStep.name = NSLocalizedString(@"Developer", @"Delta 100 developer step name");
developerStep.blurb = NSLocalizedString(@"Ilfosol", @"Delta 100 developer step description");
developerStep.temperatureC = 20;
developerStep.agitationDuration = 10;
developerStep.agitationFrequency = 60;
developerStep.duration = 360;
ASHStep *stopBathStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
stopBathStep.recipe = delta100Recipe;
stopBathStep.name = NSLocalizedString(@"Stop Bath", @"Delta 100 stop bath step name");
stopBathStep.blurb = NSLocalizedString(@"Ilfostop", @"Delta 100 stop bath step description");
stopBathStep.temperatureC = 20;
stopBathStep.agitationDuration = 10;
stopBathStep.agitationFrequency = 0;
stopBathStep.duration = 10;
ASHStep *fixerStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
fixerStep.recipe = delta100Recipe;
fixerStep.name = NSLocalizedString(@"Fixer", @"Delta 100 fixer step name");
fixerStep.blurb = NSLocalizedString(@"Ilford Rapid Fixer", @"Delta 100 fixer step name");
fixerStep.temperatureC = 20;
fixerStep.agitationDuration = 10;
fixerStep.agitationFrequency = 60;
fixerStep.duration = 180;
ASHStep *washStep = [NSEntityDescription insertNewObjectForEntityForName:@"ASHStep" inManagedObjectContext:[ASHCoreDataStack defaultStack].managedObjectContext];
washStep.recipe = delta100Recipe;
washStep.name = NSLocalizedString(@"Wash", @"Delta 100 wash step name");
washStep.blurb = NSLocalizedString(@"Water", @"Delta 100 was step description");
washStep.temperatureC = 20;
washStep.agitationDuration = 0;
washStep.agitationFrequency = 0;
washStep.duration = 300;
delta100Recipe.steps = [NSOrderedSet orderedSetWithArray:@[developerStep, stopBathStep, fixerStep, washStep]];
}
[[ASHCoreDataStack defaultStack] saveContext];
}
}
@end