2 // RSSFeedViewController.m
5 // Created by Mike Mayo on 1/12/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
9 #import "RSSFeedViewController.h"
10 #import "ASIHTTPRequest.h"
11 #import "ActivityIndicatorView.h"
12 #import "UIViewController+Conveniences.h"
14 #import "UIColor+MoreColors.h"
16 #import "NSObject+Conveniences.h"
19 @implementation RSSFeedViewController
21 @synthesize feed, feedItems, activityIndicatorView;
23 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
24 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
29 #pragma mark View lifecycle
31 - (void)orientationDidChange:(NSNotification *)notification {
32 // reload the table view to correct UILabel widths
33 [NSTimer scheduledTimerWithTimeInterval:0.25 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
38 self.navigationItem.title = @"RSS Feed";
39 self.tableView.allowsSelection = NO;
40 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
41 self.tableView.backgroundColor = [UIColor iPadTableBackgroundColor];
42 self.tableView.separatorColor = [UIColor iPadTableBackgroundColor];
45 // register for rotation events to keep the rss feed width correct
46 [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(orientationDidChange:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
50 - (void)viewWillAppear:(BOOL)animated {
51 [super viewWillAppear:animated];
53 self.navigationItem.title = [feed objectForKey:@"name"];
55 NSString *activityMessage = @"Loading...";
56 self.activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
57 [self.activityIndicatorView addToView:self.view scrollOffset:self.tableView.contentOffset.y];
59 NSURL *url = [NSURL URLWithString:[feed objectForKey:@"url"]];
61 __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
62 request.delegate = self;
63 [request setCompletionBlock:^{
64 if ((200 <= [request responseStatusCode]) && ([request responseStatusCode] <= 299)) {
65 NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[request responseData]];
66 RSSParser *rssParser = [[RSSParser alloc] init];
67 xmlParser.delegate = rssParser;
68 if ([xmlParser parse]) {
69 self.feedItems = rssParser.feedItems;
73 self.tableView.separatorColor = [UIColor lightGrayColor];
76 self.tableView.scrollEnabled = NO;
78 [self.activityIndicatorView removeFromSuperview];
79 [self.tableView reloadData];
81 [request setFailedBlock:^{
83 self.tableView.scrollEnabled = NO;
84 [self.activityIndicatorView removeFromSuperview];
85 [self.tableView reloadData];
87 [request startAsynchronous];
92 #pragma mark Table view data source
94 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
98 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
99 // Return the number of rows in the section.
102 } else if (self.feedItems) {
103 return [self.feedItems count];
109 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
111 return tableView.frame.size.height;
113 if ([self.feedItems count] > 0) {
114 FeedItem *item = [self.feedItems objectAtIndex:indexPath.row];
115 CGSize titleSize = [item.title sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
116 CGSize contentSize = [item.content sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
117 CGSize dateSize = [[RSSFeedViewController dateToStringWithTime:item.pubDate] sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
118 CGSize authorSize = [[NSString stringWithFormat:@"Posted by %@", item.creator] sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
119 return 41.0 + titleSize.height + contentSize.height + dateSize.height + authorSize.height;
121 return tableView.rowHeight;
126 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
129 return [self tableView:tableView emptyCellWithImage:[UIImage imageNamed:@"empty-rss.png"] title:@"Feed Unavailable" subtitle:@"Please check your connection and try again."];
131 static NSString *CellIdentifier = @"Cell";
133 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
135 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
137 FeedItem *item = [self.feedItems objectAtIndex:indexPath.row];
139 UIFont *titleFont = [UIFont boldSystemFontOfSize:17.0];
140 UIFont *dateFont = [UIFont systemFontOfSize:15.0];
141 UIFont *contentFont = [UIFont systemFontOfSize:15.0];
142 UIFont *authorFont = [UIFont systemFontOfSize:13.0];
144 CGSize titleSize = [item.title sizeWithFont:titleFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
145 CGRect titleRect = CGRectMake(10.0, 13.0, titleSize.width, titleSize.height);
147 UILabel *title = [[[UILabel alloc] initWithFrame:titleRect] autorelease];
148 title.font = titleFont;
149 title.textColor = [UIColor colorWithRed:0.302 green:0.388 blue:0.663 alpha:1.0];
150 title.backgroundColor = [UIColor clearColor];
151 title.numberOfLines = 0;
152 title.lineBreakMode = UILineBreakModeWordWrap;
153 title.text = item.title;
155 [cell addSubview:title];
157 CGSize dateSize = [[RSSFeedViewController dateToStringWithTime:item.pubDate] sizeWithFont:dateFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
158 CGRect dateRect = CGRectMake(10.0, 13.0 + titleRect.size.height, dateSize.width, dateSize.height);
159 UILabel *date = [[[UILabel alloc] initWithFrame:dateRect] autorelease];
160 date.font = dateFont;
161 date.textColor = [UIColor grayColor];
162 date.backgroundColor = [UIColor clearColor];
163 date.numberOfLines = 0;
164 date.lineBreakMode = UILineBreakModeWordWrap;
165 date.text = [RSSFeedViewController dateToStringWithTime:item.pubDate];
167 [cell addSubview:date];
169 CGSize authorSize = [[NSString stringWithFormat:@"Posted by %@", item.creator] sizeWithFont:authorFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
170 CGRect authorRect = CGRectMake(10.0, 14.0 + dateSize.height + titleRect.size.height, authorSize.width, authorSize.height);
171 UILabel *author = [[[UILabel alloc] initWithFrame:authorRect] autorelease];
172 author.font = authorFont;
173 author.textColor = [UIColor colorWithRed:1.0 green:0.6 blue:0.208 alpha:1.0];
174 author.backgroundColor = [UIColor clearColor];
175 author.numberOfLines = 0;
176 author.lineBreakMode = UILineBreakModeWordWrap;
177 author.text = [NSString stringWithFormat:@"Posted by %@", item.creator];
179 [cell addSubview:author];
181 CGSize contentSize = [item.content sizeWithFont:contentFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
182 CGRect contentRect = CGRectMake(10.0, 27.0 + dateSize.height + authorSize.height + titleRect.size.height, contentSize.width, contentSize.height);
183 UILabel *content = [[[UILabel alloc] initWithFrame:contentRect] autorelease];
184 content.font = contentFont;
185 content.textColor = [UIColor blackColor];
186 content.backgroundColor = [UIColor clearColor];
187 content.numberOfLines = 0;
188 content.lineBreakMode = UILineBreakModeWordWrap;
189 content.text = item.content;
191 [cell addSubview:content];
194 FeedItem *item = [self.feedItems objectAtIndex:indexPath.row];
196 UIFont *titleFont = [UIFont boldSystemFontOfSize:17.0];
197 UIFont *dateFont = [UIFont systemFontOfSize:15.0];
198 UIFont *contentFont = [UIFont systemFontOfSize:15.0];
199 UIFont *authorFont = [UIFont systemFontOfSize:13.0];
202 CGSize titleSize = [item.title sizeWithFont:titleFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
203 CGRect titleRect = CGRectMake(10.0, 13.0, titleSize.width, titleSize.height);
204 UILabel *title = (UILabel *)[cell viewWithTag:55];
205 title.frame = titleRect;
206 title.text = item.title;
209 CGSize dateSize = [[RSSFeedViewController dateToStringWithTime:item.pubDate] sizeWithFont:dateFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
210 CGRect dateRect = CGRectMake(10.0, 13.0 + titleRect.size.height, dateSize.width, dateSize.height);
211 UILabel *date = (UILabel *)[cell viewWithTag:56];
212 date.frame = dateRect;
213 date.text = [RSSFeedViewController dateToStringWithTime:item.pubDate];
216 CGSize authorSize = [[NSString stringWithFormat:@"Posted by %@", item.creator] sizeWithFont:authorFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
217 CGRect authorRect = CGRectMake(10.0, 14.0 + dateSize.height + titleRect.size.height, authorSize.width, authorSize.height);
218 UILabel *author = (UILabel *)[cell viewWithTag:57];
219 author.frame = authorRect;
220 author.text = [NSString stringWithFormat:@"Posted by %@", item.creator];
223 CGSize contentSize = [item.content sizeWithFont:contentFont constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
224 CGRect contentRect = CGRectMake(10.0, 27.0 + dateSize.height + authorSize.height + titleRect.size.height, contentSize.width, contentSize.height);
225 UILabel *content = (UILabel *)[cell viewWithTag:58];
226 content.frame = contentRect;
227 content.text = item.content;
233 #pragma mark - Memory management
238 [activityIndicatorView release];