-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMModelCollection.m
207 lines (170 loc) · 5.16 KB
/
MModelCollection.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
//
// MModelCollection.m
// Packer
//
// Created by Ben Gotow on 4/17/13.
// Copyright (c) 2013 Mib.io. All rights reserved.
//
#import "MModelCollection.h"
#import "MAPIClient.h"
#import "MModel.h"
#import "MModelCollectionPaginatingFetcher.h"
@implementation MModelCollection
- (id)initWithCollectionName:(NSString*)name andClass:(Class)c
{
self = [super init];
if (self) {
_collectionName = name;
_collectionClass = c;
_cacheArray = [NSMutableArray array];
_cacheDictionary = [NSMutableDictionary dictionary];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_collectionClass = NSClassFromString([aDecoder decodeObjectForKey: @"_collectionClass"]);
_collectionName = [aDecoder decodeObjectForKey: @"_collectionName"];
_cacheArray = [aDecoder decodeObjectForKey: @"_cache"];
if (!_cacheArray)
_cacheArray = [NSMutableArray array];
for (MModel * model in _cacheArray)
[model setParent: self];
[self rebuildDictionaryCache];
NSLog(@"Initialized collection of %lu %@ objects", (unsigned long)[_cacheArray count], NSStringFromClass(_collectionClass));
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_collectionName forKey:@"_collectionName"];
[aCoder encodeObject:NSStringFromClass(_collectionClass) forKey:@"_collectionClass"];
[aCoder encodeObject:_cacheArray forKey:@"_cache"];
}
- (void)setFetcher:(MModelCollectionFetcher *)fetcher
{
[fetcher setDelegate: self];
_fetcher = fetcher;
}
- (NSString*)resourcePath
{
if (self.collectionIsNested)
return [NSString stringWithFormat: @"%@/%@", [_parent resourcePath], _collectionName];
else
return _collectionName;
}
- (MModel*)objectAtIndex:(NSUInteger)index
{
NSArray * all = [self all];
if ([all count] > index)
return [all objectAtIndex: index];
return nil;
}
- (MModel*)objectWithID:(NSString*)ID
{
if ([self supportsDictionaryCache])
return [_cacheDictionary objectForKey: ID];
for (MModel * obj in [self all])
if ([[obj ID] isEqualToString: ID])
return obj;
return nil;
}
- (NSArray*)all
{
[self refreshIfOld];
return _cacheArray;
}
- (NSArray*)allCached
{
return _cacheArray;
}
- (NSUInteger)count
{
return [[self all] count];
}
- (void)refresh
{
[self refreshWithCallback: NULL];
}
- (void)refreshIfOld
{
BOOL expired = (!_refreshDate || ([_refreshDate timeIntervalSinceNow] > 5000));
if (expired)
[self refreshWithCallback: NULL];
}
- (void)refreshWithCallback:(RefreshCallbackBlock)callback
{
if (!_fetcher)
[self setFetcher: [[MModelCollectionPaginatingFetcher alloc] init]];
if (callback)
_refreshCallback = callback;
if (_refreshInProgress)
return;
_refreshInProgress = YES;
[[self fetcher] fetch];
}
- (void)modelsFetched:(NSArray*)jsons replaceExistingContents:(BOOL)replaceExistingContents
{
NSMutableArray * unused = [NSMutableArray arrayWithArray: _cacheArray];
for (NSDictionary * json in jsons) {
NSString * ID = [json objectForKey: @"id"];
if ([ID isKindOfClass: [NSString class]] == NO)
ID = [(NSNumber*)ID stringValue];
MModel * model = nil;
for (MModel * obj in unused) {
if ([[obj ID] isEqualToString: ID]) {
model = obj;
break;
}
}
if (model) {
[model updateWithResourceJSON: json];
[unused removeObject: model];
[model setParent: self];
if (![_cacheArray containsObject: model])
[_cacheArray addObject: model];
} else {
model = [[self.collectionClass alloc] initWithDictionary: json];
[model setParent: self];
[_cacheArray addObject: model];
}
}
if (replaceExistingContents) {
[unused makeObjectsPerformSelector:@selector(setParent:) withObject: nil];
[_cacheArray removeObjectsInArray: unused];
}
[_cacheArray sortUsingSelector: @selector(sort:)];
[self rebuildDictionaryCache];
_refreshDate = [NSDate date];
_refreshInProgress = NO;
[[MAPIClient shared] updateDiskCache: NO];
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIF_FOR(_collectionName) object:self];
if (_refreshCallback)
_refreshCallback(YES);
_refreshCallback = nil;
}
- (void)modelsFetchFailed
{
// we set the refresh date to prevent another refresh from being triggered
// immediately, and failing again.
_refreshDate = [NSDate date];
_refreshInProgress = NO;
if (_refreshCallback)
_refreshCallback(NO);
_refreshCallback = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIF_FOR(_collectionName) object:self];
}
#pragma mark Additional Cache for Fast ID Lookup
- (BOOL)supportsDictionaryCache
{
return YES;
}
- (void)rebuildDictionaryCache
{
_cacheDictionary = [NSMutableDictionary dictionary];
for (MModel * model in _cacheArray)
[_cacheDictionary setObject:model forKey:[model ID]];
}
@end