#import "MainView.h"
#import "AppDelegate.h"
@implementation MainView
@synthesize myTableView, leafViewController, mySearchBar, listContent, filteredListContent, savedContent;
#pragma mark Start Up
- (void)awakeFromNib {
listContent = [[NSMutableArray alloc] init];
filteredListContent = [[NSMutableArray alloc] initWithCapacity: [listContent count]];
[filteredListContent addObjectsFromArray: listContent];
savedContent = [[NSMutableArray alloc] initWithCapacity: [listContent count]];
mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
mySearchBar.showsCancelButton = NO;
leafViewController.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(delayedAwakeFromNib:) userInfo: nil repeats: NO];
}
- (void) delayedAwakeFromNib: (NSTimer *) timer
{
NSLog(@"delayed worked");
[myTableView reloadData];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listContent release];
[filteredListContent release];
[savedContent release];
[currentElement release];
[currentTitle release];
[currentDate release];
[currentSummary release];
[currentLink release];
[item release];
[leafViewController release];
[mySearchBar release];
[myTableView release];
[rssParser release];
[theSelectedRow release];
[super dealloc];
}
#pragma mark TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell setText:[[listContent objectAtIndex: storyIndex] objectForKey: @"title"]];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listContent count];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellAccessoryDisclosureIndicator;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([mySearchBar isFirstResponder])
[mySearchBar resignFirstResponder];
int appIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * appLink = [[listContent objectAtIndex: appIndex] objectForKey: @"link"];
appLink = [appLink stringByReplacingOccurrencesOfString:@" " withString:@""];
appLink = [appLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
appLink = [appLink stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"link: %@", appLink);
[self startDownloadingURL:[NSURL URLWithString:appLink]];
}
#pragma mark RSS Feed
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *tableSelection = [myTableView indexPathForSelectedRow];
[myTableView deselectRowAtIndexPath:tableSelection animated:YES];
if ([listContent count] == 0) {
NSString * path = @"http://miniflash.freehostia.com/rss/index.rss";
[self parseXMLFileAtURL:path];
}
cellSize = CGSizeMake([myTableView bounds].size.width, 60);
}
- (void)parseXMLFileAtURL:(NSString *)URL {
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download applications from web site (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[listContent addObject:[item copy]];
NSLog(@"adding app: %@", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
NSLog(@"all done!");
NSLog(@"listContent array has %d items", [listContent count]);
[myTableView reloadData];
}
#pragma mark Search Bar
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
if (searchBar.text.length > 0)
{
}
[searchBar resignFirstResponder];
searchBar.text = @"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
}
#pragma mark Download App
- (void)startDownloadingURL:(NSURL *)aURL
{
NSIndexPath *tableSelection = [myTableView indexPathForSelectedRow];
NSString* link = [listContent valueForKey:@"currentLink"];
NSURL * appLink = [listContent objectAtIndex:tableSelection] valueForKey:@"currentLink"];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:appLink cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSString *destinationFilename;
NSString *homeDirectory=NSHomeDirectory();
destinationFilename=[homeDirectory stringByAppendingPathComponent:@""];
NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
if (theDownload) {
[theDownload setDestination:destinationFilename allowOverwrite:YES];
} else {
}
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
[download release];
NSLog(@"Download failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)downloadDidFinish:(NSURLDownload *)download
{
[download release];
NSLog(@"%@",@"downloadDidFinish");
}
@end