-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoStore.m
139 lines (111 loc) · 3.82 KB
/
PhotoStore.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
//
// PhotoStore.m
// Metasome
//
// Created by Omar Metwally on 9/12/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "PhotoStore.h"
#import "Photo.h"
@implementation PhotoStore
+(PhotoStore *)sharedStore
{
static PhotoStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
-(id)init
{
self = [super init];
if (self) {
// Read in Metasome.xcdatamodeld
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// Where does the SQLite file go?
NSString *path = [self itemArchivePath];
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
[NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
}
// Create the managed object context
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
// The managed object context can managed undo
[context setUndoManager:nil];
[self loadAllPhotos];
}
return self;
}
-(void)removePhoto:(Photo *)p
{
[context deleteObject:p];
[allPhotos removeObjectIdenticalTo:p];
}
-(void)moveItemAtIndex:(int)from toIndex:(int)to
{
if (from == to) return;
Photo *p = [allPhotos objectAtIndex:from];
[allPhotos removeObjectAtIndex:from];
[allPhotos insertObject:p atIndex:to];
// Compte a new orderingValue fro the object that was moved
double lowerBound = 0.0;
// Is there an object before it in the array?
if (to > 0) {
lowerBound = [[allPhotos objectAtIndex:to - 1] orderingValue];
} else {
lowerBound = [[allPhotos objectAtIndex:1] orderingValue] - 2.0;
}
double upperBound = 0.0;
// Is there an object after it in the array?
if (to < [allPhotos count] - 1) {
upperBound = [[allPhotos objectAtIndex:to + 1] orderingValue];
} else {
upperBound = [[allPhotos objectAtIndex:to -1] orderingValue] + 2.0;
}
double newOrderValue = (lowerBound + upperBound) / 2.0;
[p setOrderingValue:newOrderValue];
}
-(NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get the one and only document directory
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingFormat:@"photos.data"];
}
-(BOOL)saveChanges
{
NSError *err = nil;
BOOL successful = [context save:&err];
if (!successful) {
NSLog(@"Error saving: %@", [err localizedDescription]);
}
return successful;
}
-(void)loadAllPhotos
{
if(!allPhotos) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Photo"];
[request setEntity:e];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"orderingValue" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if(!result) {
[NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]];
}
allPhotos = [[NSMutableArray alloc] initWithArray:result];
}
}
-(NSMutableArray *)allPhotos
{
return allPhotos;
}
@end