Skip to content

Commit

Permalink
added item model, handles downloading/parsing outside of controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin van der Veen authored and Benjamin van der Veen committed Mar 15, 2011
1 parent c9f222d commit 7305594
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 18 deletions.
16 changes: 16 additions & 0 deletions SampleProject/Item.h
@@ -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
57 changes: 57 additions & 0 deletions SampleProject/Item.m
@@ -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
5 changes: 3 additions & 2 deletions SampleProject/ItemController.h
@@ -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
9 changes: 4 additions & 5 deletions SampleProject/ItemController.m
Expand Up @@ -2,16 +2,15 @@

@interface ItemController ()

@property (nonatomic, retain) SMXMLElement *item;
@property (nonatomic, retain) Item *item;

@end


@implementation ItemController

@synthesize item;

- (id)initWithItem:(SMXMLElement *)theItem {
- (id)initWithItem:(Item *)theItem {
if ((self = [super init])) {
self.item = theItem;
self.hidesBottomBarWhenPushed = YES;
Expand All @@ -30,7 +29,7 @@ - (void)loadView {
titleLabel.numberOfLines = 2;
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.text = [item childNamed:@"title"].value;
titleLabel.text = item.title;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.textColor = [UIColor whiteColor];
Expand All @@ -43,7 +42,7 @@ - (void)loadView {
}

- (void)viewWillAppear:(BOOL)animated {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[item childNamed:@"link"].value]]];
[webView loadRequest:[NSURLRequest requestWithURL:item.link]];
}

@end
2 changes: 1 addition & 1 deletion SampleProject/ListController.h
@@ -1,6 +1,6 @@
#import "SMWebRequest.h"

@interface ListController : UITableViewController <SMWebRequestDelegate> {
@interface ListController : UITableViewController {
NSArray *items;
SMWebRequest *request;
}
Expand Down
13 changes: 3 additions & 10 deletions SampleProject/ListController.m
Expand Up @@ -44,7 +44,7 @@ - (void)loadView {
}

- (void)refresh {
self.request = [SMWebRequest requestWithURL:[NSURL URLWithString:@"http://news.ycombinator.com/rss"] delegate:self context:nil];
self.request = [Item createItemsRequest];
[request addTarget:self action:@selector(requestComplete:) forRequestEvents:SMWebRequestEventComplete];
[request start];
}
Expand All @@ -56,14 +56,6 @@ - (void)viewWillAppear:(BOOL)animated {
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
}

// 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 and return the bits in which we're interested.
return [[document.root childNamed:@"channel"] childrenNamed:@"item"];
}

- (void)requestComplete:(NSArray *)theItems {
self.items = theItems;
Expand Down Expand Up @@ -94,7 +86,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

cell.textLabel.text = [[items objectAtIndex:indexPath.row] childNamed:@"title"].value;
Item *item = [items objectAtIndex:indexPath.row];
cell.textLabel.text = item.title;
return cell;
}

Expand Down
14 changes: 14 additions & 0 deletions SampleProject/WebRequestSamples.xcodeproj/project.pbxproj
Expand Up @@ -16,6 +16,7 @@
41013930131B6501005D9663 /* ListController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4101392F131B6501005D9663 /* ListController.m */; };
41013953131B6C6B005D9663 /* SMXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 41013952131B6C6B005D9663 /* SMXMLDocument.m */; };
41013A52131B9010005D9663 /* ItemController.m in Sources */ = {isa = PBXBuildFile; fileRef = 41013A51131B9010005D9663 /* ItemController.m */; };
412F4DFF132F094400E9BFB7 /* Item.m in Sources */ = {isa = PBXBuildFile; fileRef = 412F4DFE132F094400E9BFB7 /* Item.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -35,6 +36,8 @@
41013952131B6C6B005D9663 /* SMXMLDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SMXMLDocument.m; sourceTree = "<group>"; };
41013A50131B9010005D9663 /* ItemController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ItemController.h; sourceTree = "<group>"; };
41013A51131B9010005D9663 /* ItemController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ItemController.m; sourceTree = "<group>"; };
412F4DFD132F094400E9BFB7 /* Item.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Item.h; sourceTree = "<group>"; };
412F4DFE132F094400E9BFB7 /* Item.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Item.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* WebRequestSamples-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "WebRequestSamples-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -72,6 +75,7 @@
28EEBF621118D79A00187D67 /* Shared */ = {
isa = PBXGroup;
children = (
412F4DF6132F090C00E9BFB7 /* Models */,
8D1107310486CEB800E47090 /* WebRequestSamples-Info.plist */,
01C7FB111312E93E00DBD0B1 /* main.m */,
01C7FB131312E94600DBD0B1 /* AppDelegate.h */,
Expand Down Expand Up @@ -124,6 +128,15 @@
name = "XML Document";
sourceTree = "<group>";
};
412F4DF6132F090C00E9BFB7 /* Models */ = {
isa = PBXGroup;
children = (
412F4DFD132F094400E9BFB7 /* Item.h */,
412F4DFE132F094400E9BFB7 /* Item.m */,
);
name = Models;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -189,6 +202,7 @@
41013930131B6501005D9663 /* ListController.m in Sources */,
41013953131B6C6B005D9663 /* SMXMLDocument.m in Sources */,
41013A52131B9010005D9663 /* ItemController.m in Sources */,
412F4DFF132F094400E9BFB7 /* Item.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 7305594

Please sign in to comment.