Permalink
Browse files

Add FeedController and update app delegate to load the feed in a sepa…

…rate thread while the splash screen is displayed
  • Loading branch information...
marcel committed Jul 17, 2008
1 parent 642516f commit dd47d69c8fdb814e2999284c4a3eed57d06ba8f1
View
@@ -0,0 +1,29 @@
+//
+// FeedController.h
+// GitHub
+//
+// Created by Marcel Molina Jr on 7/16/08.
+// Copyright 2008 InfoEther. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "FeedParser.h"
+@protocol FeedControllerDelegate <NSObject>
+@optional
+- (void)parser:(FeedParser *)parser
+didFinishLoading:(NSURL *)feedURL
+ result:(NSMutableArray *)result;
+@end
+
+@interface FeedController : NSObject {
+ FeedParser *parser;
+ id<FeedControllerDelegate> delegate;
+}
+@property (nonatomic, retain) FeedParser *parser;
+@property (nonatomic, retain) id<FeedControllerDelegate> delegate;
++ (FeedController *)loadFeed:(NSURL *)feedURL delegate:(id<FeedControllerDelegate>)theDelegate;
+- (id)initWithFeedURL:(NSURL *)feedURL delegate:(id<FeedControllerDelegate>)theDelegate;
+- (void)load;
+@end
+
+
View
@@ -0,0 +1,41 @@
+//
+// FeedController.m
+// GitHub
+//
+// Created by Marcel Molina Jr on 7/16/08.
+// Copyright 2008 InfoEther. All rights reserved.
+//
+
+#import "FeedController.h"
+
+
+@implementation FeedController
+@synthesize parser;
+@synthesize delegate;
+
++ (FeedController *)loadFeed:(NSURL *)feedURL
+ delegate:(id<FeedControllerDelegate>)theDelegate
+{
+ FeedController *controller = [[self alloc] initWithFeedURL:feedURL
+ delegate:theDelegate];
+ [controller load];
+ return [controller autorelease];
+}
+- (id)initWithFeedURL:(NSURL *)feedURL
+ delegate:(id<FeedControllerDelegate>)theDelegate
+{
+ if (self = [super init]) {
+ self.delegate = theDelegate;
+ self.parser = [[FeedParser alloc] initWithFeedURL:feedURL];
+ }
+ return self;
+}
+
+- (void)load
+{
+ [parser parse];
+ [delegate parser:parser
+ didFinishLoading:parser.url
+ result:parser.newsFeedItems];
+}
+@end
View
@@ -21,6 +21,8 @@
@property (nonatomic, retain) NSXMLParser *parser;
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) NSMutableArray *newsFeedItems;
++ (FeedParser *)feedParserForFeed:(NSURL *)feedURL;
- (id)initWithFeedURL:(NSURL *)feedURL;
- (BOOL)parse;
@end
+
View
@@ -9,35 +9,43 @@
#import "FeedParser.h"
@interface FeedParser (PrivateMethods)
-- (void)initializeParser;
-- (void)resetCurrentStringValue;
+- (void)_initializeParser;
+- (void)_resetCurrentStringValue;
@end
@implementation FeedParser
@synthesize parser;
@synthesize url;
@synthesize newsFeedItems;
++ (FeedParser *)feedParserForFeed:(NSURL *)feedURL
+{
+ return [[[self alloc] initWithFeedURL:feedURL] autorelease];
+}
-- (id)initWithFeedURL:(NSURL *)feedURL {
- if (self = [super init]) {
- self.url = feedURL;
- self.newsFeedItems = [[NSMutableArray alloc] init];
- elementsWithContent = [[NSArray alloc] initWithObjects:@"id",
- @"published",
- @"title",
- @"content",
- @"name",
- nil];
- [self initializeParser];
- withinEntryElement = NO;
- }
- return self;
+- (id)initWithFeedURL:(NSURL *)feedURL
+{
+ if (self = [super init]) {
+ self.url = feedURL;
+ self.newsFeedItems = [[NSMutableArray alloc] init];
+ elementsWithContent = [[NSArray alloc] initWithObjects:@"id",
+ @"published",
+ @"title",
+ @"content",
+ @"name",
+ nil];
+ [self _initializeParser];
+ withinEntryElement = NO;
+ }
+ return self;
}
-- (void)initializeParser {
- self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
- parser.delegate = self;
- parser.shouldResolveExternalEntities = YES;
+- (void)_initializeParser
+{
+ self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
+ parser.delegate = self;
+ parser.shouldResolveExternalEntities = NO;
+ parser.shouldReportNamespacePrefixes = NO;
+ parser.shouldProcessNamespaces = NO;
}
- (BOOL)parse {
@@ -60,7 +68,8 @@ - (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
- attributes:(NSDictionary *)attributeDict {
+ attributes:(NSDictionary *)attributeDict
+{
if ([elementName isEqualToString:@"entry"]) {
currentNewsFeedItem = [[NewsFeedItem alloc] init];
@@ -82,8 +91,9 @@ - (void)parser:(NSXMLParser *)parser
}
}
-- (void)parser:(NSXMLParser *)parser
-foundCharacters:(NSString *)string {
+- (void)parser:(NSXMLParser *)parser
+foundCharacters:(NSString *)string
+{
if (!withinEntryElement) {
return;
}
@@ -97,7 +107,8 @@ - (void)parser:(NSXMLParser *)parser
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
- qualifiedName:(NSString *)qName {
+ qualifiedName:(NSString *)qName
+{
if (!withinEntryElement) {
return;
}
@@ -132,19 +143,21 @@ - (void)parser:(NSXMLParser *)parser
}
if ([elementsWithContent containsObject:elementName]) {
- [self resetCurrentStringValue];
+ [self _resetCurrentStringValue];
}
}
-- (void)resetCurrentStringValue {
+- (void)_resetCurrentStringValue
+{
if (currentStringValue) {
[currentStringValue release];
}
currentStringValue = nil;
}
-- (void)dealloc {
+- (void)dealloc
+{
[url release];
[parser release];
[newsFeedItems release];
@@ -7,13 +7,20 @@
//
#import <UIKit/UIKit.h>
+#import "SplashScreenViewController.h"
+#import "FeedController.h"
-@interface GitHubAppDelegate : NSObject <UIApplicationDelegate> {
+@interface GitHubAppDelegate : NSObject <UIApplicationDelegate, FeedControllerDelegate> {
IBOutlet UIWindow *window;
- IBOutlet UINavigationController *navigationController;
+ IBOutlet SplashScreenViewController *splashScreenViewController;
+ NSURL *_feedURL;
}
@property (nonatomic, retain) UIWindow *window;
-@property (nonatomic, retain) UINavigationController *navigationController;
+@property (nonatomic, retain) SplashScreenViewController *splashScreenViewController;
+- (void)startLoadingNewsFeed;
+- (BOOL)isLoggedIn;
+- (NSURL *)feedURL;
+- (NSString *)loggedInUser;
@end
@@ -9,27 +9,73 @@
#import "GitHubAppDelegate.h"
#import "NewsFeedItem.h"
#import "SplashScreenViewController.h"
+#define PUBLIC_NEWS_FEED_URL @"http://github.com/news.atom"
+#define USER_NEEDS_FEED_URL @"http://github.com/%@.atom"
@implementation GitHubAppDelegate
-
+@synthesize splashScreenViewController;
@synthesize window;
-@synthesize navigationController;
-- (void)applicationDidFinishLaunching:(UIApplication *)application {
- SplashScreenViewController *splashScreenViewController;
- splashScreenViewController = [[SplashScreenViewController alloc] initWithNibName:@"SplashScreenView"
- bundle:nil];
- [window addSubview:[navigationController view]];
+- (void)applicationDidFinishLaunching:(UIApplication *)application
+{
+ [NSThread detachNewThreadSelector:@selector(startLoadingNewsFeed)
+ toTarget:self
+ withObject:nil];
+
[window addSubview:[splashScreenViewController view]];
[window makeKeyAndVisible];
}
+- (void)startLoadingNewsFeed
+{
+ NSAutoreleasePool* autoreleasePool = [[NSAutoreleasePool alloc] init];
+
+ NSLog(@"Starting to load the feed");
+ [FeedController loadFeed:[self feedURL]
+ delegate:self];
+
+ [autoreleasePool release];
+}
+
+- (void)parser:(FeedParser *)parser
+didFinishLoading:(NSURL *)feedURL
+ result:(NSMutableArray *)result
+{
+ NSLog(@"Feed has been loaded");
+}
+
+- (BOOL)isLoggedIn
+{
+ return NO;
+}
+
+- (NSURL *)feedURL
+{
+ if (_feedURL == nil) {
+ if ([self isLoggedIn]) {
+ _feedURL = [NSURL URLWithString:[NSString
+ stringWithFormat:USER_NEEDS_FEED_URL,
+ [self loggedInUser]]];
+ } else {
+ _feedURL = [NSURL URLWithString:PUBLIC_NEWS_FEED_URL];
+ }
+ }
+ return _feedURL;
+}
+
+- (NSString *)loggedInUser
+{
+ // XXX stubbed for now
+ return @"marcel";
+}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
-- (void)dealloc {
+- (void)dealloc
+{
+ [splashScreenViewController release];
[window release];
[super dealloc];
}
@@ -10,10 +10,7 @@
@interface NewsFeedTableViewController : UITableViewController {
NSMutableArray *newsFeedItems;
- NSURL *_feedURL;
}
@property (nonatomic, retain) NSMutableArray *newsFeedItems;
-- (BOOL)isLoggedIn;
-- (NSURL *)feedURL;
-- (NSString *)loggedInUser;
+
@end
@@ -13,9 +13,6 @@
#import "NewsFeedItemCell.h"
#import "NewsFeedItemDetailViewController.h"
-#define PublicNewsFeedURL @"http://github.com/news.atom"
-#define UserNewsFeedURL @"http://github.com/%@.atom"
-
@implementation NewsFeedTableViewController
@synthesize newsFeedItems;
@@ -27,32 +24,15 @@ - (id)initWithCoder:(NSCoder *)coder {
}
- (void)viewDidLoad {
+ /*
FeedParser *parser;
- parser = [[FeedParser alloc] initWithFeedURL:[self feedURL]];
+ parser = [[FeedParser parserForFeedURL:[self feedURL] delegate:nil];
[parser parse];
NSLog(@"News feed items: %@", [parser newsFeedItems]);
[newsFeedItems addObjectsFromArray:[parser newsFeedItems]];
+ */
}
-- (BOOL)isLoggedIn {
- return NO;
-}
-
-- (NSURL *)feedURL {
- if (_feedURL == nil) {
- if ([self isLoggedIn]) {
- _feedURL = [NSURL URLWithString:[NSString stringWithFormat:UserNewsFeedURL, [self loggedInUser]]];
- } else {
- _feedURL = [NSURL URLWithString:PublicNewsFeedURL];
- }
- }
- return _feedURL;
-}
-
-- (NSString *)loggedInUser {
- // XXX stubbed for now
- return @"marcel";
-}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
@@ -169,9 +149,6 @@ - (void)didReceiveMemoryWarning {
- (void)dealloc {
- if (_feedURL) {
- [_feedURL release];
- }
[newsFeedItems release];
[super dealloc];
}
Oops, something went wrong.

0 comments on commit dd47d69

Please sign in to comment.