mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-15 01:02:15 +08:00
Audited all code in the app and produced extensive TODO list. Fixed build problems when on the Release profile.
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
// Put current user id first because it might be nil.
|
||||
[[DBUser currentUser].userID isEqualToNumber:_requiredUserID]) {
|
||||
isAuthenticated = YES;
|
||||
// TODO: Move this isAuthenticated logic into the model!
|
||||
} else if (_requiredUserID == nil &&
|
||||
[DBUser currentUser].singleAccessToken &&
|
||||
[DBUser currentUser] != nil) {
|
||||
|
||||
@@ -98,6 +98,7 @@
|
||||
[self invalidateModel];
|
||||
}
|
||||
|
||||
// TODO: Move login and sign-up into the model as higher level concepts
|
||||
- (void)loginOrSignup {
|
||||
if (_showingSignup) {
|
||||
// Signup
|
||||
@@ -142,6 +143,7 @@
|
||||
assert([objects count] == 1);
|
||||
DBUser* user = [objects objectAtIndex:0];
|
||||
NSLog(@"Authentication Token: %@", user.singleAccessToken);
|
||||
// TODO: Move this into the DBUser...
|
||||
[[NSUserDefaults standardUserDefaults] setObject:user.userID forKey:kCurrentUserIDKey];
|
||||
[[NSUserDefaults standardUserDefaults] synchronize];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:kUserLoggedInNotificationName object:user];
|
||||
@@ -149,7 +151,12 @@
|
||||
}
|
||||
|
||||
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
|
||||
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
|
||||
// TODO: TTAlert???
|
||||
[[[[UIAlertView alloc] initWithTitle:@"Error"
|
||||
message:[error localizedDescription]
|
||||
delegate:nil
|
||||
cancelButtonTitle:@"OK"
|
||||
otherButtonTitles:nil] autorelease] show];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -16,19 +16,23 @@
|
||||
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
|
||||
_post = [[DBPost objectWithPrimaryKeyValue:postID] retain];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithTopicID:(NSString*)topicID {
|
||||
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
|
||||
// TODO: Why are we using topic ID here?
|
||||
_topicID = [[NSNumber numberWithInt:[topicID intValue]] retain];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_post release];
|
||||
[_topicID release];
|
||||
TT_RELEASE_SAFELY(_post);
|
||||
TT_RELEASE_SAFELY(_topicID);
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@@ -46,7 +50,10 @@
|
||||
self.tableViewStyle = UITableViewStyleGrouped;
|
||||
self.autoresizesForKeyboard = YES;
|
||||
self.variableHeightRows = YES;
|
||||
|
||||
[[TTNavigator navigator].URLMap from:@"db://updateAttachment" toObject:self selector:@selector(updateAttachment)];
|
||||
|
||||
// TODO: Use self.post method?
|
||||
if (nil == _post) {
|
||||
_post = [[DBPost object] retain];
|
||||
_post.topicID = _topicID;
|
||||
@@ -64,6 +71,7 @@
|
||||
}
|
||||
|
||||
- (void)createModel {
|
||||
// TODO: Move this into the model: [self.currentUser canModifyObject:_post]
|
||||
BOOL isAuthorizedUser = [[DBUser currentUser].userID isEqualToNumber:_post.userID] || [self isNewRecord];
|
||||
|
||||
NSMutableArray* items = [NSMutableArray array];
|
||||
@@ -75,6 +83,7 @@
|
||||
// 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]) {
|
||||
// TODO: Create model method to determine if there is an attachment
|
||||
// Has existing attachment. allow replace
|
||||
NSString* url = _post.attachmentPath;
|
||||
[items addObject:[TTTableImageItem itemWithText:@"Tap to Replace Image" imageURL:url defaultImage:nil URL:@"db://updateAttachment"]];
|
||||
@@ -115,15 +124,7 @@
|
||||
[self presentModalViewController:controller animated:YES];
|
||||
}
|
||||
|
||||
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
|
||||
_newAttachment = [info objectForKey:UIImagePickerControllerOriginalImage];
|
||||
[self dismissModalViewControllerAnimated:YES];
|
||||
[self invalidateModel];
|
||||
}
|
||||
|
||||
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
|
||||
[self dismissModalViewControllerAnimated:YES];
|
||||
}
|
||||
#pragma mark Actions
|
||||
|
||||
- (void)createButtonWasPressed:(id)sender {
|
||||
_post.body = _bodyTextEditor.text;
|
||||
@@ -131,7 +132,6 @@
|
||||
[[RKObjectManager sharedManager] postObject:_post delegate:self];
|
||||
}
|
||||
|
||||
|
||||
- (void)updateButtonWasPressed:(id)sender {
|
||||
_post.body = _bodyTextEditor.text;
|
||||
_post.newAttachment = _newAttachment;
|
||||
@@ -142,10 +142,26 @@
|
||||
[[RKObjectManager sharedManager] deleteObject:_post delegate:self];
|
||||
}
|
||||
|
||||
#pragma mark UIImagePickerControllerDelegate methods
|
||||
|
||||
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
|
||||
_newAttachment = [info objectForKey:UIImagePickerControllerOriginalImage];
|
||||
[self dismissModalViewControllerAnimated:YES];
|
||||
[self invalidateModel];
|
||||
}
|
||||
|
||||
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
|
||||
[self dismissModalViewControllerAnimated:YES];
|
||||
}
|
||||
|
||||
#pragma mark RKObjectLoaderDelegate methods
|
||||
|
||||
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
|
||||
// TODO: RKLog helpers?
|
||||
NSLog(@"Loaded Objects: %@", objects);
|
||||
NSLog(@"Status Code: %d", objectLoader.response.statusCode);
|
||||
// Post notification telling view controllers to reload.
|
||||
// TODO
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:kObjectCreatedUpdatedOrDestroyedNotificationName object:objects];
|
||||
// dismiss.
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
@@ -155,12 +171,15 @@
|
||||
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
|
||||
}
|
||||
|
||||
// Text Editor Delegate
|
||||
#pragma mark TTTextEditorDelegate methods
|
||||
|
||||
- (void)textEditorDidBeginEditing:(TTTextEditor*)textEditor {
|
||||
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:textEditor action:@selector(resignFirstResponder)];
|
||||
[doneButton autorelease];
|
||||
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
|
||||
style:UIBarButtonItemStyleDone
|
||||
target:textEditor
|
||||
action:@selector(resignFirstResponder)];
|
||||
self.navigationItem.rightBarButtonItem = doneButton;
|
||||
[doneButton release];
|
||||
}
|
||||
|
||||
- (void)textEditorDidEndEditing:(TTTextEditor*)textEditor {
|
||||
|
||||
@@ -6,15 +6,21 @@
|
||||
// Copyright 2011 Two Toasters. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Three20/Three20.h>
|
||||
#import "DBResourceListTableViewController.h"
|
||||
#import "DBTopic.h"
|
||||
|
||||
/**
|
||||
* Displays a table of Posts within a given Topic
|
||||
*/
|
||||
@interface DBPostsTableViewController : DBResourceListTableViewController {
|
||||
// TODO: Just use a Topic
|
||||
NSString* _topicID;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Topic we are viewing Posts within
|
||||
*/
|
||||
@property (nonatomic, readonly) DBTopic* topic;
|
||||
|
||||
@end
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
if (self = [super initWithStyle:UITableViewStylePlain]) {
|
||||
_topicID = [topicID retain];
|
||||
self.title = @"Posts";
|
||||
// TODO: Use routing or something to generate this URL. RKGeneratePathWithObject
|
||||
_resourcePath = [[NSString stringWithFormat:@"/topics/%@/posts", _topicID] retain];
|
||||
_resourceClass = [DBPost class];
|
||||
}
|
||||
@@ -30,28 +31,37 @@
|
||||
|
||||
- (void)loadView {
|
||||
[super loadView];
|
||||
|
||||
self.variableHeightRows = YES;
|
||||
|
||||
UIBarButtonItem* newItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonWasPressed:)] autorelease];
|
||||
UIBarButtonItem* newItem = [[[UIBarButtonItem alloc]
|
||||
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
|
||||
target:self
|
||||
action:@selector(addButtonWasPressed:)] autorelease];
|
||||
self.navigationItem.rightBarButtonItem = newItem;
|
||||
}
|
||||
|
||||
- (void)addButtonWasPressed:(id)sender {
|
||||
// TODO: Move this onto the model...
|
||||
// RKMakeObjectPath / RKGeneratePathWithObject / RKGeneratePathForObject(@"db://topics/(topicID)/posts/new", self.topic);
|
||||
NSString* url = [NSString stringWithFormat:@"db://topics/%@/posts/new", _topicID];
|
||||
TTOpenURL(url);
|
||||
}
|
||||
|
||||
- (void)createModel {
|
||||
if (nil == [DBTopic objectWithPrimaryKeyValue:_topicID]) {
|
||||
// TODO: Cleanup???
|
||||
// this topic was deleted or something.
|
||||
[self.navigationController popToRootViewControllerAnimated:YES];
|
||||
return;
|
||||
}
|
||||
|
||||
[super createModel];
|
||||
}
|
||||
|
||||
- (void)didLoadModel:(BOOL)firstTime {
|
||||
[super didLoadModel:firstTime];
|
||||
|
||||
if ([self.model isKindOfClass:[RKRequestTTModel class]]) {
|
||||
RKRequestTTModel* model = (RKRequestTTModel*)self.model;
|
||||
NSMutableArray* postItems = [NSMutableArray arrayWithCapacity:[model.objects count]];
|
||||
@@ -68,7 +78,7 @@
|
||||
[topicItems addObject:[TTTableTextItem itemWithText:@"Edit" URL:editURL]];
|
||||
}
|
||||
|
||||
for(DBPost* post in model.objects) {
|
||||
for (DBPost* post in model.objects) {
|
||||
NSString* url = [NSString stringWithFormat:@"db://posts/%@", post.postID];
|
||||
NSString* imageURL = post.attachmentPath;
|
||||
TTTableImageItem* item = [TTTableImageItem itemWithText:post.body
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
// Copyright 2011 Two Toasters. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Three20/Three20.h>
|
||||
#import <RestKit/Three20/Three20.h>
|
||||
|
||||
|
||||
@@ -10,10 +10,12 @@
|
||||
#import "DBManagedObjectCache.h"
|
||||
|
||||
@implementation UINavigationBar (CustomImage)
|
||||
|
||||
- (void)drawRect:(CGRect)rect {
|
||||
UIImage *image = [UIImage imageNamed:@"navigationBarBackground.png"];
|
||||
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation DBResourceListTableViewController
|
||||
@@ -45,6 +47,7 @@
|
||||
UIView* tableSpacer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 31)];
|
||||
self.tableView.tableHeaderView = tableSpacer;
|
||||
|
||||
// TODO: Use more generic notifications
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userStateChanged:) name:kUserLoggedInNotificationName object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userStateChanged:) name:kUserLoggedOutNotificationName object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadNotification:) name:kObjectCreatedUpdatedOrDestroyedNotificationName object:nil];
|
||||
@@ -56,12 +59,12 @@
|
||||
|
||||
- (void)viewDidUnload {
|
||||
[super viewDidUnload];
|
||||
[_loadedAtLabel release];
|
||||
_loadedAtLabel = nil;
|
||||
|
||||
TT_RELEASE_SAFELY(_loadedAtLabel);
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
// TODO: Why not just use invalidateModel as the target of the notifications?
|
||||
- (void)reloadNotification:(NSNotification*)note {
|
||||
[self invalidateModel];
|
||||
}
|
||||
@@ -70,12 +73,15 @@
|
||||
[self invalidateModel];
|
||||
}
|
||||
|
||||
// TODO: Why not use a createResourcePath method instead of an ivar?
|
||||
- (void)createModel {
|
||||
self.model = [[[RKRequestTTModel alloc] initWithResourcePath:_resourcePath] autorelease];
|
||||
}
|
||||
|
||||
// TODO: Comment me up!
|
||||
- (void)didLoadModel:(BOOL)firstTime {
|
||||
[super didLoadModel:firstTime];
|
||||
|
||||
if ([self.model isKindOfClass:[RKRequestTTModel class]]) {
|
||||
RKRequestTTModel* model = (RKRequestTTModel*)self.model;
|
||||
|
||||
|
||||
@@ -20,10 +20,11 @@
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_topic release];
|
||||
TT_RELEASE_SAFELY(_topic);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
// TODO: Move this into the model...
|
||||
- (BOOL)isNewRecord {
|
||||
return [[_topic topicID] intValue] == 0;
|
||||
}
|
||||
@@ -67,6 +68,8 @@
|
||||
self.dataSource = [TTListDataSource dataSourceWithItems:items];
|
||||
}
|
||||
|
||||
#pragma mark Actions
|
||||
|
||||
- (void)createButtonWasPressed:(id)sender {
|
||||
_topic.name = _topicNameField.text;
|
||||
[[RKObjectManager sharedManager] postObject:_topic delegate:self];
|
||||
@@ -81,16 +84,20 @@
|
||||
[[RKObjectManager sharedManager] deleteObject:_topic delegate:self];
|
||||
}
|
||||
|
||||
#pragma mark RKObjectLoaderDelegate methods
|
||||
|
||||
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
|
||||
NSLog(@"Loaded Objects: %@", objects);
|
||||
NSLog(@"Status Code: %d", objectLoader.response.statusCode);
|
||||
// Post notification telling view controllers to reload.
|
||||
// TODO: Generalize this notification
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:kObjectCreatedUpdatedOrDestroyedNotificationName object:objects];
|
||||
// dismiss.
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
}
|
||||
|
||||
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
|
||||
// TODO: TTAlert or create a DBErrorAlert helper?
|
||||
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
- (id)initWithNavigatorURL:(NSURL *)URL query:(NSDictionary *)query {
|
||||
if (self = [super initWithNavigatorURL:URL query:query]) {
|
||||
self.title = @"Topics";
|
||||
_tableTitleHeaderLabel.text = @"Recent Topics";
|
||||
_tableTitleHeaderLabel.text = @"Recent Topics";
|
||||
_resourcePath = [@"/topics" retain];
|
||||
_resourceClass = [DBTopic class];
|
||||
}
|
||||
@@ -56,6 +56,7 @@
|
||||
NSMutableArray* items = [NSMutableArray arrayWithCapacity:[model.objects count]];
|
||||
|
||||
for(DBTopic* topic in model.objects) {
|
||||
// TODO: RKGeneratePathWithObject. Move to postsTTURL method?
|
||||
NSString* url = [NSString stringWithFormat:@"db://topics/%@/posts", topic.topicID];
|
||||
[items addObject:[TTTableTextItem itemWithText:topic.name URL:url]];
|
||||
}
|
||||
@@ -63,6 +64,7 @@
|
||||
NSLog(@"Items: %@", items);
|
||||
// Ensure that the datasource's model is still the RKRequestTTModel;
|
||||
// Otherwise isOutdated will not work.
|
||||
// TODO: Better fix for this?
|
||||
TTListDataSource* dataSource = [TTListDataSource dataSourceWithItems:items];
|
||||
dataSource.model = model;
|
||||
self.dataSource = dataSource;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <RestKit/RestKit.h>
|
||||
|
||||
// TODO: Why not move this into a controller and inherit instead of using categories?
|
||||
@interface UIViewController (RKLoading) <RKRequestDelegate>
|
||||
|
||||
@end
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
@implementation UIViewController (RKLoading)
|
||||
|
||||
- (void)requestDidStartLoad:(RKRequest*)request {
|
||||
UIView* overlayView = [self.view viewWithTag:66];
|
||||
UIView* overlayView = [self.view viewWithTag:66]; // TODO: Need constant for tags...
|
||||
if (overlayView == nil) {
|
||||
overlayView = [[TTActivityLabel alloc] initWithFrame:self.view.bounds style:TTActivityLabelStyleBlackBox text:@"Loading..."];
|
||||
overlayView.backgroundColor = [UIColor blackColor];
|
||||
@@ -31,14 +31,14 @@
|
||||
return;
|
||||
}
|
||||
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 300, 20)];
|
||||
progressView.tag = 67;
|
||||
progressView.tag = 67; // TODO: Need constant for tags...
|
||||
[self.view addSubview:progressView];
|
||||
}
|
||||
progressView.progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
|
||||
}
|
||||
|
||||
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
|
||||
UIView* overlayView = [self.view viewWithTag:66];
|
||||
UIView* overlayView = [self.view viewWithTag:66]; // TODO: Need constant for tags...
|
||||
UIView* progressView = [self.view viewWithTag:67];
|
||||
[overlayView removeFromSuperview];
|
||||
[overlayView release];
|
||||
|
||||
Reference in New Issue
Block a user