-
Notifications
You must be signed in to change notification settings - Fork 1
/
FavoritesData.m
executable file
·139 lines (101 loc) · 3.22 KB
/
FavoritesData.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
//
// AddToFavoritesAppDelegate.h
// AddToFavorites
//
// Created by Gianluca Tranchedone on 08/02/11.
// Copyright 2011 Sketch to Code. All rights reserved.
//
#import "FavoritesData.h"
#import "SynthesizeSingleton.h"
@interface FavoritesData (Private)
- (NSString *) docsDir;
@end
@implementation FavoritesData
NSString *const REFRESH_FAVORITES = @"refreshFavorites";
SYNTHESIZE_SINGLETON_FOR_CLASS(FavoritesData);
- (void) addFavorite:(Favorite *) fav
{
if(!favoritesLoaded) [self loadFavorites];
Favorite *favorite = [self favoriteWithId:fav.favId];
if(!favorite) [_favArray addObject:fav];
[self saveFavorites];
[[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FAVORITES object:nil];
}
- (void) removeFavoriteById:(NSString *) favId
{
for(int i = 0; i < _favArray.count; i++)
{
Favorite *fav = (Favorite *)[_favArray objectAtIndex:i];
if([fav.favId isEqualToString:favId])
[_favArray removeObjectAtIndex:i];
}
[self saveFavorites];
[[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FAVORITES object:nil];
}
- (Favorite *) favoriteWithId:(NSString *) favId
{
for(int i = 0; i < _favArray.count; i++)
{
Favorite *fav = (Favorite *)[_favArray objectAtIndex:i];
if([fav.favId isEqualToString:favId]) return fav;
}
return nil;
}
- (NSArray *) getFavorites
{
if(!favoritesLoaded) [self loadFavorites];
return _favArray;
}
- (void) saveFavorites
{
NSMutableData *savedData;
NSKeyedArchiver *encoder;
savedData = [NSMutableData data];
encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:savedData];
[encoder encodeObject:_favArray forKey:@"favorites"];
[encoder finishEncoding];
[savedData writeToFile:_dataFilePath atomically:YES];
[encoder release];
}
- (void) loadFavorites
{
if(favoritesLoaded) return;
NSString *dataFileName = @"favorites.dat";
_dataFilePath = [[[self docsDir] stringByAppendingPathComponent:dataFileName] retain];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:_dataFilePath];
if(success)
{
NSMutableData *savedData;
NSKeyedUnarchiver *decoder;
NSMutableArray *tmpFavArray;
savedData = [NSData dataWithContentsOfFile:_dataFilePath];
decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:savedData];
tmpFavArray = [decoder decodeObjectForKey:@"favorites"];
if(tmpFavArray) _favArray = [tmpFavArray retain];
else _favArray = [[[NSMutableArray alloc] init] retain];
[decoder finishDecoding];
[decoder release];
}
else _favArray = [[[NSMutableArray alloc] init] retain];
favoritesLoaded = YES;
}
/*
- (NSString *) docsDir
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
*/
- (NSString *)docsDir
{
#ifdef DEBUGX
NSLog(@"%s", __FUNCTION__);
#endif
NSFileManager *fileManager = [[NSFileManager new] autorelease]; // File manager instance
NSURL *pathURL = [fileManager URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
return [pathURL path]; // Path to the application's "~/Library/Application Support" directory
}
@end