Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added item model, handles downloading/parsing outside of controller
- Loading branch information
Benjamin van der Veen
authored and
Benjamin van der Veen
committed
Mar 15, 2011
1 parent
c9f222d
commit 7305594
Showing
7 changed files
with
98 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#import "SMXMLDocument.h" | ||
#import "SMWebRequest.h" | ||
|
||
@interface Item : NSObject { | ||
NSString *title; | ||
NSURL *link; | ||
} | ||
|
||
@property (nonatomic, readonly) NSString *title; | ||
@property (nonatomic, readonly) NSURL *link; | ||
|
||
- (id)initWithElement:(SMXMLElement *)element; | ||
|
||
+ (SMWebRequest *)createItemsRequest; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#import "Item.h" | ||
|
||
|
||
@implementation Item | ||
|
||
@synthesize title, link; | ||
|
||
- (void)setTitle:(NSString *)value { | ||
[title release]; | ||
title = [value copy]; | ||
} | ||
|
||
- (void)setLink:(NSURL *)value { | ||
[link release]; | ||
link = [value retain]; | ||
} | ||
|
||
- (id)initWithElement:(SMXMLElement *)element { | ||
if (self = [super init]) { | ||
self.title = [element childNamed:@"title"].value; | ||
self.link = [NSURL URLWithString:[element childNamed:@"link"].value]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
self.title = nil; | ||
self.link = nil; | ||
[super dealloc]; | ||
} | ||
|
||
+ (SMWebRequest *)createItemsRequest { | ||
NSURL *url = [NSURL URLWithString:@"http://news.ycombinator.com/rss"]; | ||
return [SMWebRequest requestWithURL:url | ||
delegate:(id<SMWebRequestDelegate>)self | ||
context:nil]; | ||
} | ||
|
||
// This method is called on a background thread. Don't touch your instance members! | ||
+ (id)webRequest:(SMWebRequest *)webRequest resultObjectForData:(NSData *)data context:(id)context { | ||
// We do this gnarly parsing on a background thread to keep the UI responsive. | ||
SMXMLDocument *document = [SMXMLDocument documentWithData:data]; | ||
|
||
// Select the bits in which we're interested. | ||
NSArray *itemsXml = [[document.root childNamed:@"channel"] childrenNamed:@"item"]; | ||
|
||
NSMutableArray *items = [NSMutableArray array]; | ||
|
||
// Convert them into model objects | ||
for (SMXMLElement *itemXml in itemsXml) { | ||
[items addObject:[[[Item alloc] initWithElement:itemXml] autorelease]]; | ||
} | ||
|
||
return items; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
#import "SMXMLDocument.h" | ||
#import "Item.h" | ||
|
||
@interface ItemController : UIViewController <UIWebViewDelegate> { | ||
UIWebView *webView; | ||
SMXMLElement *item; | ||
Item *item; | ||
} | ||
|
||
- (id)initWithItem:(SMXMLElement *)item; | ||
- (id)initWithItem:(Item *)item; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters