-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollectionSource.m
117 lines (97 loc) · 2.56 KB
/
CollectionSource.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
//
// CollectionSource.m
// CollectionTest
//
// Created by Joachim Fornallaz on 24.09.10.
// Copyright 2010 Joachim Fornallaz. All rights reserved.
//
#import "CollectionSource.h"
#import "ResourceLoader.h"
@implementation CollectionSource
@synthesize queue, title, imageDict, itemList, preliminary, resourceCache, delegate;
#pragma mark -
#pragma mark Object lifecycle
- (id)initWithFeedURL:(NSURL *)feedURL
{
self = [super init];
if (self != nil) {
self.itemList = [NSArray array];
sourceURL = [feedURL copy];
}
return self;
}
- (void)dealloc
{
[resourceLoader stopLoading];
self.itemList = nil;
[sourceURL release];
[super dealloc];
}
#pragma mark -
#pragma mark Instance methods
- (void)sync
{
if ([sourceURL isFileURL]) {
// <#statements#>
} else {
NSData *data = [ResourceLoader dataForURL:sourceURL delegate:self preliminary:NO resourceCache:self.resourceCache];
if (data) {
preliminary = YES;
[self parseData:data];
}
}
}
- (void)parseData:(NSData *)data
{
self.queue = [[NSOperationQueue alloc] init];
FeedParseOperation *parser = [[FeedParseOperation alloc] initWithData:data delegate:self];
parser.elementsToParse = [NSArray arrayWithObjects:@"title", @"enclosure", nil];
[self.queue addOperation:parser]; // this will start the "ParseOperation"
[parser release];
}
#pragma mark -
#pragma mark ResourceLoaderDelegate methods
- (BOOL)resourceLoader:(ResourceLoader *)loader shouldStartLoadWithRequest:(NSURLRequest *)request
{
resourceLoader = loader;
return YES;
}
- (void)resourceLoaderDidFinishLoadingData:(NSData *)data withMIMEType:(NSString *)MIMEType
{
preliminary = NO;
resourceLoader = nil;
[self parseData:data];
}
- (void)resourceLoaderDidReturnNotModifiedData
{
preliminary = NO;
resourceLoader = nil;
[self.delegate collectionSourceDidRefresh:self];
}
- (void)resourceLoaderDidFailLoad
{
resourceLoader = nil;
[self.delegate collectionSourceRefreshDidFail:self];
}
#pragma mark -
#pragma mark FeedParseOperationDelegate methods
- (id)objectForElement:(NSString *)elementName text:(NSString *)nodeText attributes:(NSDictionary *)attributeDict
{
id object = nil;
if ([elementName isEqualToString:@"enclosure"]) {
object = [attributeDict objectForKey:@"url"];
}
return object;
}
- (void)didFinishParsing:(NSArray *)anItemList title:(NSString *)aTitle image:(NSDictionary *)anImageDict
{
self.title = aTitle;
self.imageDict = anImageDict;
self.itemList = anItemList;
[self.delegate collectionSourceDidRefresh:self];
}
- (void)parseErrorOccurred:(NSError *)error
{
[self.delegate collectionSourceRefreshDidFail:self];
}
@end