-
Notifications
You must be signed in to change notification settings - Fork 5
/
MMutableModelCollection.m
62 lines (51 loc) · 1.84 KB
/
MMutableModelCollection.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
//
// MMutableModelCollection.m
// Endorsee
//
// Created by Ben Gotow on 3/30/14.
// Copyright (c) 2014 Foundry376. All rights reserved.
//
#import "MMutableModelCollection.h"
@implementation MMutableModelCollection
- (BOOL)supportsDictionaryCache
{
return NO; // because added items may not have IDs yet
}
- (void)addItem:(MModel*)model
{
[model setParent: self];
[_cacheArray addObject: model];
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIF_FOR(self.collectionName) object:self];
}
- (void)addItemsFromArray:(NSArray*)array
{
for (MModel * item in array) {
[item setParent: self];
[_cacheArray addObject: item];
}
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIF_FOR(self.collectionName) object:self];
}
- (void)removeItemAtIndex:(NSUInteger)index
{
NSArray * all = [self all];
if ([all count] > index) {
MModel * obj = [[self all] objectAtIndex: index];
// NOTE: This implementation doesnt account for the possiblity that an item
// could be saving for the first time as it's being deleted.. That would involve
// adding a new "saving" flag to the object and probably just rejecting the deletion.
// (to keep it simple)
if ([obj ID]) {
MAPITransaction * t = [MAPITransaction transactionForPerforming:TRANSACTION_DELETE of:obj];
[[MAPIClient shared] queueAPITransaction: t];
} else {
[[MAPIClient shared] removeQueuedTransactionsFor: obj];
}
[_cacheArray removeObject: obj];
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIF_FOR(self.collectionName) object:self];
}
}
- (void)removeItemWithID:(NSString*)ID
{
[self removeItemAtIndex: [[self all] indexOfObject: [self objectWithID: ID]]];
}
@end