Copy of rackspace-ios version 2.1.1
[pithos-ios] / Classes / RSSFeedViewController.m
1 //
2 //  RSSFeedViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 1/12/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "RSSFeedViewController.h"
10 #import "ASIHTTPRequest.h"
11 #import "ActivityIndicatorView.h"
12 #import "UIViewController+Conveniences.h"
13 #import "RSSParser.h"
14 #import "UIColor+MoreColors.h"
15 #import "FeedItem.h"
16 #import "NSObject+Conveniences.h"
17
18
19 @implementation RSSFeedViewController
20
21 @synthesize feed, feedItems, activityIndicatorView;
22
23 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
24     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
25 }
26
27
28 #pragma mark -
29 #pragma mark View lifecycle
30
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];     
34 }
35
36 - (void)viewDidLoad {
37     [super viewDidLoad];
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];
43     }    
44     
45         // register for rotation events to keep the rss feed width correct
46         [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(orientationDidChange:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
47     
48 }
49
50 - (void)viewWillAppear:(BOOL)animated {
51     [super viewWillAppear:animated];
52     
53     self.navigationItem.title = [feed objectForKey:@"name"];
54     
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];    
58     
59     NSURL *url = [NSURL URLWithString:[feed objectForKey:@"url"]];
60     
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;
70             }            
71             [rssParser release];
72             [xmlParser release];
73             self.tableView.separatorColor = [UIColor lightGrayColor];
74         } else {
75             requestFailed = YES;
76             self.tableView.scrollEnabled = NO;
77         }  
78         [self.activityIndicatorView removeFromSuperview];
79         [self.tableView reloadData];
80     }];
81     [request setFailedBlock:^{
82         requestFailed = YES;
83         self.tableView.scrollEnabled = NO;
84         [self.activityIndicatorView removeFromSuperview];
85         [self.tableView reloadData];
86     }];    
87     [request startAsynchronous];
88     
89 }
90
91 #pragma mark -
92 #pragma mark Table view data source
93
94 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
95     return 1;
96 }
97
98 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
99     // Return the number of rows in the section.
100     if (requestFailed) {
101         return 1;
102     } else if (self.feedItems) {
103         return [self.feedItems count];
104     } else {
105         return 0;
106     }
107 }
108
109 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
110     if (requestFailed) {
111         return tableView.frame.size.height;
112     } else {
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;
120         } else {
121             return tableView.rowHeight;
122         }
123     }
124 }
125
126 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
127     
128     if (requestFailed) {
129         return [self tableView:tableView emptyCellWithImage:[UIImage imageNamed:@"empty-rss.png"] title:@"Feed Unavailable" subtitle:@"Please check your connection and try again."];
130     } else {
131         static NSString *CellIdentifier = @"Cell";
132         
133         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
134         if (cell == nil) {
135             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
136             
137             FeedItem *item = [self.feedItems objectAtIndex:indexPath.row];            
138             
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];
143             
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);
146
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;
154             title.tag = 55;
155             [cell addSubview:title];
156
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];
166             date.tag = 56;
167             [cell addSubview:date];
168             
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];
178             author.tag = 57;
179             [cell addSubview:author];
180             
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;
190             content.tag = 58;
191             [cell addSubview:content];            
192         }
193         
194         FeedItem *item = [self.feedItems objectAtIndex:indexPath.row];
195                 
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];
200         
201         // Title
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;
207
208         // Date
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];
214         
215         // Author
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];
221         
222         // Content
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;
228         
229         return cell;
230     }    
231 }
232
233 #pragma mark - Memory management
234
235 - (void)dealloc {
236     [feed release];
237     [feedItems release];
238     [activityIndicatorView release];
239     [super dealloc];
240 }
241
242 @end
243