-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemManager.m
331 lines (264 loc) · 10.7 KB
/
ItemManager.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
//
// ItemListModel.m
// daily Expenses
//
// Created by renan veloso silva on 02/07/13.
// Copyright (c) 2013 renan veloso silva. All rights reserved.
//
#import "ItemManager.h"
#import "Utility.h"
@implementation ItemManager
static id _instance;
@synthesize listItens, allItens, dateInCurrentView;
+ (ItemManager *) sharedInstance{
@synchronized(self){
if (!_instance) {
_instance = [[self alloc] init];
}
}
return _instance;
}
#pragma mark - init methods
-(id)init{
self = [super init];
if (self) {
[self inicialize];
[self loadData];
}
return self;
}
-(void)inicialize{
monthList = [[ItemFilter sharedInstance] monthList];
hasLog = [Config sharedInstance].hasLog;
allItens = [[NSMutableArray alloc]init];
self.totalValue = [[NSNumber alloc] init];
dateInCurrentView = [[NSString alloc] initWithString:[[Utility sharedInstance] getCurrentDate]];
}
//TODO: criar um Manager para Criar, Consultar, Apagar arquivos
-(void)loadData{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [documentPath objectAtIndex:0];
newPlistFile = [[NSString alloc] initWithString:[documentFolder stringByAppendingPathComponent:@"NewPlist.plist"]];
if (hasLog){
NSString* temp = newPlistFile;
temp = [temp stringByReplacingOccurrencesOfString:@" " withString:@"\\ "];
NSLog(@"file path: %@",temp);
}
NSArray *arrayPlist = [NSArray arrayWithContentsOfFile:newPlistFile];
if (!arrayPlist) {
NSString *bundlePathOfPlist = [[NSBundle mainBundle] pathForResource:@"itemList" ofType:@"plist"];
arrayPlist = [NSArray arrayWithContentsOfFile:bundlePathOfPlist];
[filemgr createFileAtPath:newPlistFile contents:nil attributes:nil];
[arrayPlist writeToFile:newPlistFile atomically:YES];
}
for (NSDictionary *dict in arrayPlist) {
ItemModel *item = [self convertDicToItemModel:dict];
[allItens addObject:item];
}
[self filterItensByCurrentDate];
}
-(ItemModel*)convertDicToItemModel:(NSDictionary*)dict{
ItemModel *item = [[ItemModel alloc] init];
item.item_id = [dict objectForKey:@"id"];
item.label = [dict objectForKey:@"label"];
item.type = [dict objectForKey:@"type"];
item.parcel = [dict objectForKey:@"parcel"];
item.isCredit = [[Utility sharedInstance] stringToBool:[dict objectForKey:@"isCredit"]];
item.isSpent = [[Utility sharedInstance] stringToBool:[dict objectForKey:@"isSpent"]];
item.value = [dict objectForKey:@"value"];
item.dateSpent = [dict objectForKey:@"dateSpent"];
item.dateUpdated = [dict objectForKey:@"dateUpdated"];
item.dateCreated = [dict objectForKey:@"dateCreated"];
item.notes = [dict objectForKey:@"notes"];
item.typeImg = [self getTypeImage:item.type];
return item;
}
#pragma mark - add, update, remove Item
-(void)addItemToList:(ItemModel*)item{
int addId = [self getHighestId] +1;
item.item_id = [NSString stringWithFormat:@"%d",addId];
item.dateCreated = [[Utility sharedInstance] getCurrentDate];
item.typeImg = [self getTypeImage:item.type];
item = [self setDefautItemAttributes:item];
[allItens addObject:item];
[self saveItemToPlist];
[self filterItensByCurrentDate];
}
-(void)updateItemToList:(ItemModel*)item{
item.dateUpdated = [[Utility sharedInstance] getCurrentDate];
item = [self setDefautItemAttributes:item];
item.typeImg = [self getTypeImage:item.type];
[allItens setObject:item atIndexedSubscript:[self findIndexById:item.item_id]];
[self updatePlistFileBasedOnList];
[self getTotalValue];
}
-(void)removeItemBySpendItem :(ItemModel*)item{
[allItens removeObject:item];
[listItens removeObject:item];
[self removePlistFileBasedOnList];
}
#pragma mark - auxiliar methods
-(NSInteger)findIndexById:(NSString*)item_id{
for (int i=0; i<allItens.count; i++) {
if ([[[allItens objectAtIndex:i] item_id] isEqualToString:item_id]){
return i;
}
}
return 0;
}
-(UIImage*)getTypeImage:(NSString*)type{
NSDictionary *catategoryList = [[Config sharedInstance] categoryList];
UIImage *image = [UIImage imageNamed:[catategoryList objectForKey:type]];
return image;
}
-(ItemModel*)getSpendItemById:(NSString*)idValue{
ItemModel *itemToReturn;
for (ItemModel* item in allItens) {
if ([item.item_id isEqualToString:idValue]){
itemToReturn = item;
break;
}
}
return itemToReturn;
}
-(int)getHighestId{
ItemModel *itemR = [allItens objectAtIndex:0];
int highestId = [itemR.item_id intValue];
for (int i=1; i<allItens.count; i++) {
itemR = [allItens objectAtIndex:i];
if ([itemR.item_id intValue] > highestId){
highestId = [itemR.item_id intValue];
}
}
return highestId;
}
-(void)getTotalValue{
self.totalValue = 0;
for (ItemModel *itemR in listItens) {
self.totalValue = [NSNumber numberWithFloat:[self.totalValue floatValue] + [self getValue:itemR.value isChecked:itemR.isCredit]];
}
}
-(float)getValue:(NSString*)valueR isChecked:(BOOL)isChecked{
float realValue;
realValue = [valueR floatValue];
if (!isChecked){
realValue = (-1)*realValue;
}
return realValue;
}
#pragma mark - verify method
-(ItemModel*)setDefautItemAttributes:(ItemModel*)item{
if ([[Utility sharedInstance]isEmptyString:item.label]) item.label = @"";
if ([[Utility sharedInstance]isEmptyString:item.type]) item.type = @"label";
if ([[Utility sharedInstance]isEmptyString:item.dateSpent]) item.dateSpent = @"";
if ([[Utility sharedInstance]isEmptyString:item.notes]) item.notes = @"";
if ([[Utility sharedInstance]isEmptyString:item.dateCreated]) item.dateCreated = @"";
if ([[Utility sharedInstance]isEmptyString:item.dateUpdated]) item.dateUpdated = @"";
if ([[Utility sharedInstance]isEmptyString:item.value]) item.value = @"0";
if ([[Utility sharedInstance]isEmptyString:item.parcel]) item.parcel = @"1";
return item;
}
#pragma mark - save, update, remove methods from plist
-(void)saveItemToPlist{
NSMutableArray *addData = [NSMutableArray arrayWithContentsOfFile:newPlistFile];
[addData addObject:[self addItem:[allItens lastObject]]];
[addData writeToFile:newPlistFile atomically:YES];
}
-(void)updatePlistFileBasedOnList{
NSMutableArray *addData = [[NSMutableArray alloc] init];
for (ItemModel *itemR in allItens) {
[addData addObject:[self addItem:itemR]];
}
[addData writeToFile:newPlistFile atomically:YES];
}
-(void)removePlistFileBasedOnList{
NSMutableArray *addData = [NSMutableArray arrayWithContentsOfFile:newPlistFile];
BOOL verify = NO;
NSMutableArray *itensToRemove = [[NSMutableArray alloc] init];
for (NSDictionary *item in addData) {
for (ItemModel *itemC in allItens) {
NSString *itemId = [item objectForKey:@"id"];
NSString *itemCId = itemC.item_id;
if ([itemId isEqualToString:itemCId]) {
verify = YES;
}
}
if (!verify) {
[itensToRemove addObject:item];
float total = [self.totalValue floatValue];
float itemToRemoveVelu = [[item objectForKey:@"value"] floatValue];
self.totalValue = [NSNumber numberWithFloat: (total - itemToRemoveVelu)];
}
verify = NO;
}
[addData removeObjectsInArray:itensToRemove];
[addData writeToFile:newPlistFile atomically:YES];
}
-(NSDictionary*)addItem:(ItemModel*)item{
NSDictionary* itemDict = [[NSDictionary alloc]initWithObjectsAndKeys:
item.item_id, @"id",
item.dateSpent, @"dateSpent",
item.dateCreated, @"dateCreated",
item.dateUpdated, @"dateUpdated",
item.label, @"label",
item.notes, @"notes",
item.parcel, @"parcel",
item.value, @"value",
item.type, @"type",
[[Utility sharedInstance] boolToString:item.isSpent], @"isSpent",
[[Utility sharedInstance] boolToString:item.isCredit], @"isCredit",
nil];
return itemDict;
}
#pragma mark - filter itens
-(void)filterItensByCurrentDate{
ItemFilter* filter = [ItemFilter sharedInstance];
if (listItens) {
[listItens removeAllObjects];
[listItens addObjectsFromArray :[NSMutableArray arrayWithArray:[filter filterByDate:[[Utility sharedInstance] getCurrentDate] onList:allItens]]];
}else{
listItens = [[NSMutableArray alloc] initWithArray:[filter filterByDate:[[Utility sharedInstance] getCurrentDate] onList:allItens]];
}
[self getTotalValue];
}
-(void)getListDayBefore{
ItemFilter* filter = [ItemFilter sharedInstance];
[listItens removeAllObjects];
dateInCurrentView = [[NSString alloc] initWithString:[[Utility sharedInstance] getDayBefore:dateInCurrentView]] ;
[listItens addObjectsFromArray:[NSArray arrayWithArray:[filter filterByDate:dateInCurrentView onList:allItens]]];
[self getTotalValue];
}
-(void)getListDayAfter{
ItemFilter* filter = [ItemFilter sharedInstance];
[listItens removeAllObjects];
dateInCurrentView = [[NSString alloc] initWithString:[[Utility sharedInstance] getDayAfter:dateInCurrentView]] ;
[listItens addObjectsFromArray:[NSArray arrayWithArray:[filter filterByDate:dateInCurrentView onList:allItens]]];
[self getTotalValue];
}
-(NSArray*)getAvailableMonths{
NSMutableArray* months = [[NSMutableArray alloc] init];
for (ItemModel* itemR in allItens) {
NSString*currentMonth = [[Utility sharedInstance] getMonthByDate:itemR.dateSpent];
if (![months containsObject:currentMonth]) {
[months addObject:currentMonth];
}
}
return months;
}
-(NSDictionary*)getItensByMonthList:(NSArray*)monthListR{
NSMutableDictionary* monthsItensList = [[NSMutableDictionary alloc] init];
NSMutableArray* tempItemList = [[NSMutableArray alloc] init];
for (NSString* monthR in monthListR) {
for (ItemModel* itemR in allItens) {
if ([[[Utility sharedInstance] getMonthByDate:itemR.dateSpent] isEqualToString:monthR]) {
[tempItemList addObject:itemR];
}
}
[monthsItensList setObject:tempItemList forKey:monthR];
tempItemList = [[NSMutableArray alloc] init];
}
return monthsItensList;
}
@end