-
Notifications
You must be signed in to change notification settings - Fork 10
/
FSMutableArray.j
276 lines (221 loc) · 7.08 KB
/
FSMutableArray.j
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
@import <Foundation/CPObject.j>
@import <Foundation/CPMutableArray.j>
@implementation CPArray (KeyValueObservingFix)
- (void)addObserver:(id)anObserver forKeyPath:(CPString)aKeyPath options:(CPKeyValueObservingOptions)anOptions context:(id)aContext
{ // do not raise because we know...
}
- (void)removeObserver:(id)anObserver forKeyPath:(CPString)aKeyPath
{ // do not raise because we know...
}
@end
@implementation FSMutableArray : CPMutableArray
{ id _entity @accessors(property=entity);
id _proxyObject;
id _defaults @accessors(property=defaults);
id _kvoKey @accessors(property=kvoKey);
id _kvoOwner @accessors(property=kvoOwner);
id _kvoMethod;
}
+ (id)alloc
{
var array = [];
array.isa = self;
var ivars = class_copyIvarList(self),
count = ivars.length;
while (count--)
array[ivar_getName(ivars[count])] = nil;
return array;
}
- (id)initWithArray:(id)anObject ofEntity:(id)someEntity
{ self = [super init];
_proxyObject = anObject;
_entity=someEntity;
return self;
}
- (id)copy
{ var i = 0,
theCopy = [],
count = [self count];
for (; i < count; i++)
[theCopy addObject:[self objectAtIndex:i]];
return theCopy;
}
- (id)_representedObject
{
return _proxyObject;
}
- (void)_setRepresentedObject:(id)anObject
{
[_proxyObject setArray: anObject];
}
- (unsigned)count
{ return [[self _representedObject] count];
}
- (int)indexOfObject:(CPObject)anObject inRange:(CPRange)aRange
{
var index = aRange.location,
count = aRange.length,
shouldIsEqual = !!anObject.isa;
for (; index < count; ++index)
{
var object = [self objectAtIndex:index];
if (anObject === object || shouldIsEqual && !!object.isa && [anObject isEqual:object])
return index;
}
return CPNotFound;
}
- (int)indexOfObject:(CPObject)anObject
{
return [self indexOfObject:anObject inRange:CPMakeRange(0, [self count])];
}
- (int)indexOfObjectIdenticalTo:(CPObject)anObject inRange:(CPRange)aRange
{
var index = aRange.location,
count = aRange.length;
for (; index < count; ++index)
if (anObject === [self objectAtIndex:index])
return index;
return CPNotFound;
}
- (int)indexOfObjectIdenticalTo:(CPObject)anObject
{
return [self indexOfObjectIdenticalTo:anObject inRange:CPMakeRange(0, [self count])];
}
- (id)objectAtIndex:(unsigned)anIndex
{
return [[self objectsAtIndexes:[CPIndexSet indexSetWithIndex:anIndex]] firstObject];
}
- (CPArray)objectsAtIndexes:(CPIndexSet)theIndexes
{ return [[self _representedObject] objectsAtIndexes:theIndexes];
}
- (FSObject) _addToDBObject: anObject
{
if(_defaults)
{ if([anObject isKindOfClass: [CPDictionary class]])
{ [anObject addEntriesFromDictionary: _defaults];
} else if( [anObject isKindOfClass: [FSObject class] ] )
{ if(!anObject._changes) anObject._changes = [CPMutableDictionary dictionary];
[anObject._changes addEntriesFromDictionary: _defaults];
}
}
return [_entity insertObject: anObject];
}
- (void)addObject:(id)anObject
{
[self insertObject:anObject atIndex:[self count]];
}
- (void)addObjectsFromArray:(CPArray)anArray
{
var index = 0,
count = [anArray count];
[self insertObjects:anArray atIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange([self count], count)]];
}
- (void)insertObject:(id)anObject atIndex:(unsigned)anIndex
{
[self insertObjects:[anObject] atIndexes:[CPIndexSet indexSetWithIndex:anIndex]];
}
- (void)insertObjects:(CPArray)theObjects atIndexes:(CPIndexSet)theIndexes
{ var target = [[self _representedObject] copy];
var myarr=[];
var l=[theObjects count];
for(var i=0; i<l; i++)
{ var o=[theObjects objectAtIndex: i ];
if(_entity) o= [self _addToDBObject: o ];
[myarr addObject: o];
}
[target insertObjects: myarr atIndexes:theIndexes];
[self _setRepresentedObject:target];
}
- (void)removeObject:(id)anObject
{ [self removeObject:anObject inRange:CPMakeRange(0, [self count])];
}
- (void)removeObjectsInArray:(CPArray)theObjects
{
var l=[theObjects count];
for(var i=0; i<l; i++)
{ [_entity deleteObject: [theObjects objectAtIndex: i ] ];
}
var target = [[self _representedObject] copy];
[target removeObjectsInArray:theObjects];
[self _setRepresentedObject:target];
}
- (void)removeObject:(id)theObject inRange:(CPRange)theRange
{ var index;
while ((index = [self indexOfObject:theObject inRange:theRange]) !== CPNotFound)
{ [_entity deleteObject: [self objectAtIndex: index ] ];
[self removeObjectAtIndex:index];
theRange = CPIntersectionRange(CPMakeRange(index, length - index), theRange);
}
}
- (void)removeLastObject
{
[self removeObjectsAtIndexes:[CPIndexSet indexSetWithIndex:[self count] - 1]];
}
- (void)removeObjectAtIndex:(unsigned)anIndex
{
[self removeObjectsAtIndexes:[CPIndexSet indexSetWithIndex:anIndex]];
}
- (void)removeObjectsAtIndexes:(CPIndexSet)theIndexes
{ var target = [[self _representedObject] copy];
var theObjects=[[self _representedObject] objectsAtIndexes: theIndexes ];
var l=[theObjects count];
for(var i=0; i<l; i++)
{ [_entity deleteObject: [theObjects objectAtIndex: i ] ];
}
[target removeObjectsAtIndexes:theIndexes];
[self _setRepresentedObject:target];
}
- (void)replaceObjectAtIndex:(unsigned)anIndex withObject:(id)anObject
{
[self replaceObjectsAtIndexes:[CPIndexSet indexSetWithIndex:anIndex] withObjects:[anObject]]
}
- (void)replaceObjectsAtIndexes:(CPIndexSet)theIndexes withObjects:(CPArray)theObjects
{ var target = [[self _representedObject] copy];
[target replaceObjectsAtIndexes:theIndexes withObjects:theObjects];
[self _setRepresentedObject:target];
}
-(CPString) description
{ return [[self _representedObject] description];
}
- (void)sortUsingFunction:(Function)aFunction context:(id)aContext
{ var target = [[self _representedObject] copy];
[target sortUsingFunction:aFunction context:aContext];
[self _setRepresentedObject:target];
}
-(void) connection:(CPConnection)someConnection didReceiveData:(id)data
{ var j = JSON.parse(data);
var a = [];
if(j)
{ var i,l=j.length;
for(i=0;i < l;i++)
{ var pk=j[i][_entity._pk];
var peek;
if (peek=[_entity _registeredObjectForPK:pk]) // enforce singleton pattern
a.push(peek);
else
{ var t=[[FSObject alloc] initWithEntity:_entity];
var o=j[i];
t._data = [CPMutableDictionary dictionary];
for (var propName in o) {
if (o.hasOwnProperty(propName)) {
var pnv = o[propName];
if(pnv !== nil)
t._data.setValueForKey(propName, pnv);
}
}
[_entity _registerObjectInPKCache:t];
a.push(t);
}
}
}
if(_kvoKey&& _kvoOwner)
[_kvoOwner willChangeValueForKey: _kvoKey];
[self _setRepresentedObject:a];
if(_kvoKey && _kvoOwner)
[_kvoOwner didChangeValueForKey: _kvoKey];
if(_kvoMethod && _kvoOwner)
[_kvoOwner performSelector:_kvoMethod withObject:self];
if(_entity.__ACForSpinner && _entity.__ACForSpinner.__tableViewForSpinner)
[_entity.__ACForSpinner.__tableViewForSpinner _stopAnimation: self];
}
@end