001.
#import "DataModel.h"
002.
#import "Recipe.h"
003.
004.
005.
static
NSString
*
const
FAVORITES_KEY = @
"Favorites"
;
006.
007.
@interface
DataModel ()
008.
{
009.
010.
NSArray
*myObject;
011.
012.
013.
014.
NSString
* name;
015.
NSString
* imageid;
016.
NSString
* path;
017.
018.
019.
020.
}
021.
022.
023.
024.
@property
(
nonatomic
,
copy
)
NSMutableArray
* recipes;
025.
@property
(
nonatomic
,
retain
)
NSMutableArray
* favorites;
026.
@property
(
nonatomic
,
retain
)
NSDictionary
* dict;
027.
028.
029.
@end
030.
031.
@implementation
DataModel
032.
033.
@synthesize
recipes,favorites,dict;
034.
+ (
id
)sharedInstance
035.
{
036.
static
DataModel* instance =
nil
;
037.
if
(instance ==
nil
)
038.
{
039.
instance = [[
self
alloc] init];
040.
}
041.
return
instance;
042.
}
043.
044.
- (
id
)init
045.
{
046.
if
((
self
= [
super
init]))
047.
{
048.
049.
[
self
MyData];
050.
051.
}
052.
return
self
;
053.
}
054.
055.
056.
057.
-(
void
) MyData
058.
{
059.
imageid = @
"ImageID"
;
060.
name = @
"Name"
;
061.
path = @
"ImageID"
;
062.
063.
064.
065.
recipes = [
NSMutableArray
arrayWithCapacity:10];
066.
Recipe* recipe = [[Recipe alloc] init];
067.
068.
070.
071.
072.
NSURLRequest
*jSonRequest = [
NSURLRequest
requestWithURL:searchURL];
073.
[
NSURLConnection
sendAsynchronousRequest:jSonRequest queue:[
NSOperationQueue
mainQueue] completionHandler:^(
NSURLResponse
*response,
NSData
*data,
NSError
*connectionError) {
074.
075.
myObject = [
NSJSONSerialization
JSONObjectWithData:data options:
NSJSONReadingAllowFragments
error:&connectionError];
076.
recipes = [
NSMutableArray
arrayWithCapacity:myObject.count];
077.
078.
for
(
NSDictionary
*dataDict in myObject)
079.
{
080.
081.
082.
recipe.name = [dataDict objectForKey:@
"Name"
];
083.
recipe.image = [dataDict objectForKey:@
"ImageID"
];
084.
recipe.Path = [dataDict objectForKey:@
"Path"
];
085.
086.
dict = [
NSDictionary
dictionaryWithObjectsAndKeys:recipe.name,name,recipe.image,imageid,recipe.Path,path,
nil
];
087.
[recipes addObject:dict];
088.
089.
}
090.
NSLog
(@
"recipes = %@"
,recipes);
091.
}];
092.
093.
094.
095.
096.
NSArray
* array = [[
NSUserDefaults
standardUserDefaults] objectForKey:FAVORITES_KEY];
097.
favorites = [
NSMutableArray
arrayWithArray:array];
098.
099.
}
100.
101.
- (
int
)recipeCount
102.
{
103.
return
self
.recipes.count;
104.
}
105.
106.
- (Recipe*)recipeAtIndex:(
int
)index
107.
{
108.
109.
return
[
self
.recipes objectAtIndex:index];
110.
111.
}
112.
113.
- (
void
)removeRecipeAtIndex:(
int
)index
114.
{
115.
[
self
.recipes removeObjectAtIndex:index];
116.
}
117.
118.
119.
- (Recipe*)findRecipeNamed:(
NSString
*)name_
120.
{
121.
for
(Recipe* recipe in
self
.recipes)
122.
{
123.
if
([recipe.name isEqualToString:name_])
124.
return
recipe;
125.
}
126.
return
nil
;
127.
}
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
- (
int
)favoritesCount
138.
{
139.
return
self
.favorites.count;
140.
}
141.
142.
- (Recipe*)favoriteAtIndex:(
int
)index
143.
{
144.
NSString
* name_ = [
self
.favorites objectAtIndex:index];
145.
return
[
self
findRecipeNamed:name_];
146.
}
147.
148.
149.
- (
void
)removeFavoriteAtIndex:(
int
)index
150.
{
151.
[
self
.favorites removeObjectAtIndex:index];
152.
153.
154.
155.
156.
[[
NSUserDefaults
standardUserDefaults] setObject:
self
.favorites forKey:FAVORITES_KEY];
157.
[[
NSUserDefaults
standardUserDefaults] synchronize];
158.
159.
160.
}
161.
162.
163.
164.
165.
- (
BOOL
)isFavorite:(Recipe*)recipe
166.
{
167.
return
[
self
.favorites containsObject:recipe.name];
168.
}
169.
170.
- (
void
)addToFavorites:(Recipe*)recipe
171.
{
172.
[
self
.favorites addObject:recipe.name];
173.
[[
NSUserDefaults
standardUserDefaults] setObject:
self
.favorites forKey:FAVORITES_KEY];
174.
[[
NSUserDefaults
standardUserDefaults] synchronize];
175.
}
176.
177.
- (
void
)removeFromFavorites:(Recipe*)recipe
178.
{
179.
[
self
.favorites removeObject:recipe.name];
180.
[[
NSUserDefaults
standardUserDefaults] setObject:
self
.favorites forKey:FAVORITES_KEY];
181.
[[
NSUserDefaults
standardUserDefaults] synchronize];
182.
}