Functionally complete new example.

This commit is contained in:
Jeremy Ellison
2011-01-11 13:57:28 -05:00
parent 378aaac12b
commit a4e7e60467
17 changed files with 79 additions and 41 deletions

View File

@@ -78,6 +78,7 @@
_passwordField.returnKeyType = UIReturnKeyGo;
}
self.dataSource = [TTListDataSource dataSourceWithItems:items];
[_usernameField becomeFirstResponder];
}

View File

@@ -8,6 +8,7 @@
#import "DBPostTableViewController.h"
#import <Three20/Three20+Additions.h>
#import "DBUser.h"
@implementation DBPostTableViewController
@@ -51,9 +52,6 @@
}
_requiresLoggedInUser = YES;
if (![self isNewRecord]) {
_requiredUserID = _post.userID;
}
[super loadView];
@@ -61,40 +59,56 @@
_bodyTextEditor.font = [UIFont systemFontOfSize:12];
_bodyTextEditor.autoresizesToText = NO;
_bodyTextEditor.delegate = self;
_bodyTextEditor.text = _post.body;
}
- (void)createModel {
BOOL isAuthorizedUser = [[DBUser currentUser].userID isEqualToNumber:_post.userID] || [self isNewRecord];
NSMutableArray* items = [NSMutableArray array];
_bodyTextEditor.text = _post.body;
[items addObject:[TTTableControlItem itemWithCaption:@"" control:(UIControl*)_bodyTextEditor]];
// Attachment item.
if (_newAttachment) {
// has new attachment. show it. allow update.
[items addObject:[TTTableImageItem itemWithText:@"Tap to Replace Image" imageURL:@"" defaultImage:_newAttachment URL:@"db://updateAttachment"]];
} else if (![[_post attachmentPath] isWhitespaceAndNewlines]) {
// Has existing attachment. allow replace
NSString* url = [NSString stringWithFormat:@"%@%@", [RKObjectManager sharedManager].client.baseURL, _post.attachmentPath];
[items addObject:[TTTableImageItem itemWithText:@"Tap to Replace Image" imageURL:url defaultImage:nil URL:@"db://updateAttachment"]];
if (isAuthorizedUser) {
[items addObject:[TTTableControlItem itemWithCaption:@"" control:(UIControl*)_bodyTextEditor]];
if (_newAttachment) {
// has new attachment. show it. allow update.
[items addObject:[TTTableImageItem itemWithText:@"Tap to Replace Image" imageURL:@"" defaultImage:_newAttachment URL:@"db://updateAttachment"]];
} else if (![[_post attachmentPath] isWhitespaceAndNewlines]) {
// Has existing attachment. allow replace
NSString* url = [NSString stringWithFormat:@"%@%@", [RKObjectManager sharedManager].client.baseURL, _post.attachmentPath];
[items addObject:[TTTableImageItem itemWithText:@"Tap to Replace Image" imageURL:url defaultImage:nil URL:@"db://updateAttachment"]];
} else {
// has no attachment. allow new one.
[items addObject:[TTTableTextItem itemWithText:@"Tap to Add Image" URL:@"db://updateAttachment"]];
}
} else {
// has no attachment. allow new one.
[items addObject:[TTTableTextItem itemWithText:@"Tap to Add Image" URL:@"db://updateAttachment"]];
[items addObject:[TTTableLongTextItem itemWithText:_post.body]];
NSString* url = [NSString stringWithFormat:@"%@%@", [RKObjectManager sharedManager].client.baseURL, _post.attachmentPath];
[items addObject:[TTTableImageItem itemWithText:@"" imageURL:url URL:nil]];
}
if ([self isNewRecord]) {
self.title = @"New Post";
[items addObject:[TTTableButton itemWithText:@"Create" delegate:self selector:@selector(createButtonWasPressed:)]];
} else {
self.title = @"Edit Post";
[items addObject:[TTTableButton itemWithText:@"Update" delegate:self selector:@selector(updateButtonWasPressed:)]];
[items addObject:[TTTableButton itemWithText:@"Delete" delegate:self selector:@selector(destroyButtonWasPressed:)]];
if (isAuthorizedUser) {
self.title = @"Edit Post";
[items addObject:[TTTableButton itemWithText:@"Update" delegate:self selector:@selector(updateButtonWasPressed:)]];
[items addObject:[TTTableButton itemWithText:@"Delete" delegate:self selector:@selector(destroyButtonWasPressed:)]];
} else {
self.title = @"Post";
}
}
self.dataSource = [TTListDataSource dataSourceWithItems:items];
NSString* byLine = @"";
if (![self isNewRecord]) {
NSString* username = (isAuthorizedUser ? @"me" : _post.username);
byLine = [NSString stringWithFormat:@"posted by %@", username];
}
self.dataSource = [TTSectionedDataSource dataSourceWithArrays:byLine, items, nil];
}
- (void)updateAttachment {
[_bodyTextEditor resignFirstResponder];
UIImagePickerController* controller = [[[UIImagePickerController alloc] init] autorelease];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];

View File

@@ -71,7 +71,12 @@
for(DBPost* post in model.objects) {
NSString* url = [NSString stringWithFormat:@"db://posts/%@", post.postID];
[postItems addObject:[TTTableLongTextItem itemWithText:post.body URL:url]];
NSString* imageURL = [NSString stringWithFormat:@"%@%@", [RKObjectManager sharedManager].client.baseURL,
post.attachmentPath];
TTTableImageItem* item = [TTTableImageItem itemWithText:post.body
imageURL:imageURL
URL:url];
[postItems addObject:item];
}
self.dataSource = [TTSectionedDataSource dataSourceWithArrays:@"Topic",

View File

@@ -9,6 +9,7 @@
#import "DiscussionBoardAppDelegate.h"
#import <RestKit/RestKit.h>
#import <RestKit/ObjectMapping/RKDynamicRouter.h>
#import <RestKit/ObjectMapping/RKRailsRouter.h>
#import <Three20/Three20.h>
#import <Three20/Three20+Additions.h>
#import "DBTopicsTableViewController.h"
@@ -52,13 +53,16 @@ static NSString* const kAccessTokenHeaderField = @"HTTP_USER_ACCESS_TOKEN";
// Set Up Router
RKDynamicRouter* router = [[[RKDynamicRouter alloc] init] autorelease];
// RKRailsRouter* router = [[[RKRailsRouter alloc] init] autorelease];
[router routeClass:[DBUser class] toResourcePath:@"/signup" forMethod:RKRequestMethodPOST];
[router routeClass:[DBUser class] toResourcePath:@"/login" forMethod:RKRequestMethodPUT];
// [router setModelName:@"topic" forClass:[DBTopic class]];
[router routeClass:[DBTopic class] toResourcePath:@"/topics" forMethod:RKRequestMethodPOST];
[router routeClass:[DBTopic class] toResourcePath:@"/topics/(topicID)" forMethod:RKRequestMethodPUT];
[router routeClass:[DBTopic class] toResourcePath:@"/topics/(topicID)" forMethod:RKRequestMethodDELETE];
// [router setModelName:@"post" forClass:[DBPost class]];
[router routeClass:[DBPost class] toResourcePath:@"/topics/(topicID)/posts" forMethod:RKRequestMethodPOST];
[router routeClass:[DBPost class] toResourcePath:@"/topics/(topicID)/posts/(postID)" forMethod:RKRequestMethodPUT];
[router routeClass:[DBPost class] toResourcePath:@"/topics/(topicID)/posts/(postID)" forMethod:RKRequestMethodDELETE];
@@ -89,9 +93,6 @@ static NSString* const kAccessTokenHeaderField = @"HTTP_USER_ACCESS_TOKEN";
NSLog(@"User: %@", user);
[objectManager.client setValue:[DBUser currentUser].singleAccessToken forHTTPHeaderField:@"USER_ACCESS_TOKEN"];
// Testing
TTOpenURL(@"db://posts/9");
return YES;
}

View File

@@ -24,6 +24,7 @@
@property (nonatomic, retain) NSDate* updatedAt;
@property (nonatomic, retain) NSNumber* userID;
@property (nonatomic, retain) NSNumber* postID;
@property (nonatomic, retain) NSString* username;
@property (nonatomic, retain) UIImage* newAttachment;

View File

@@ -22,6 +22,7 @@
@dynamic updatedAt;
@dynamic userID;
@dynamic postID;
@dynamic username;
@synthesize newAttachment = _newAttachment;
@@ -38,6 +39,7 @@
@"attachment_path", @"attachmentPath",
@"attachment_updated_at", @"attachmentUpdatedAt",
@"body", @"body",
@"user_login", @"username",
nil];
}
@@ -45,17 +47,18 @@
return @"postID";
}
- (NSDictionary*)paramsForSerialization {
- (id<RKRequestSerializable>)paramsForSerialization {
RKParams* params = [RKParams params];
[params setValue:self.body forParam:@"post[body]"];
NSLog(@"Self Body: %@", self.body);
if (_newAttachment) {
NSData* data = UIImagePNGRepresentation(_newAttachment);
NSLog(@"Data Size: %d", [data length]);
[params setData:data MIMEType:@"application/octet-stream" forParam:@"topic[attachment]"];
RKParamsAttachment* attachment = [params setData:data MIMEType:@"application/octet-stream" forParam:@"post[attachment]"];
attachment.fileName = @"image.png";
}
// Suppress warning. todo: should this method return an <RKRequestSerializable> by default?
return (NSDictionary*)params;
return params;
}
@end

View File

@@ -18,5 +18,6 @@
@property (nonatomic, retain) NSNumber* userID;
@property (nonatomic, retain) NSDate* createdAt;
@property (nonatomic, retain) NSDate* updatedAt;
@property (nonatomic, retain) NSString* username;
@end

View File

@@ -15,6 +15,7 @@
@dynamic userID;
@dynamic createdAt;
@dynamic updatedAt;
@dynamic username;
#pragma mark RKObjectMappable methods
@@ -24,7 +25,8 @@
@"name",@"name",
@"user_id",@"userID",
@"created_at",@"createdAt",
@"updated_at",@"updatedAt",
@"updated_at",@"updatedAt",
@"user_login", @"username",
nil];
}
@@ -32,7 +34,7 @@
return @"topicID";
}
- (NSDictionary*)paramsForSerialization {
- (id<RKRequestSerializable>)paramsForSerialization {
return [NSDictionary dictionaryWithObjectsAndKeys:
self.name, @"topic[name]", nil];
}

View File

@@ -7,19 +7,19 @@ class PostsController < ApplicationController
before_filter :requre_owner, :only => [:update, :destroy]
index.response do |wants|
wants.json{ render :json => collection.to_json(:methods => :attachment_path) }
wants.json{ render :json => collection.to_json(:methods => [:attachment_path, :user_login]) }
end
create.response do |wants|
wants.json{ render :json => object.to_json(:methods => :attachment_path) }
wants.json{ render :json => object.to_json(:methods => [:attachment_path, :user_login]) }
end
update.response do |wants|
wants.json{ render :json => object.to_json(:methods => :attachment_path) }
wants.json{ render :json => object.to_json(:methods => [:attachment_path, :user_login]) }
end
destroy.response do |wants|
wants.json{ render :json => {} }
end
show.response do |wants|
wants.json{ render :json => object.to_json(:methods => :attachment_path) }
wants.json{ render :json => object.to_json(:methods => [:attachment_path, :user_login]) }
end
create.before do

View File

@@ -5,19 +5,19 @@ class TopicsController < ApplicationController
before_filter :requre_owner, :only => [:update, :destroy]
index.response do |wants|
wants.json { render :json => collection }
wants.json { render :json => collection.to_json(:methods => :user_login) }
end
create.response do |wants|
wants.json{ render :json => object }
wants.json{ render :json => object.to_json(:methods => :user_login) }
end
update.response do |wants|
wants.json{ render :json => object }
wants.json{ render :json => object.to_json(:methods => :user_login) }
end
destroy.response do |wants|
wants.json{ render :json => {} }
end
show.response do |wants|
wants.json{ render :json => object }
wants.json{ render :json => object.to_json(:methods => :user_login) }
end
create.before do

View File

@@ -7,4 +7,8 @@ class Post < ActiveRecord::Base
attachment.url
end
def user_login
user.login
end
end

View File

@@ -4,4 +4,8 @@ class Topic < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :user
def user_login
user.login
end
end