Skip to content

Commit

Permalink
Refactored models and cleaned up controllers pretty thoroughly. Moved…
Browse files Browse the repository at this point in the history
… to higher level abstractions for permissions and such
  • Loading branch information
blakewatters committed Jan 21, 2011
1 parent 3474eb7 commit ebd2355
Show file tree
Hide file tree
Showing 30 changed files with 288 additions and 250 deletions.
Expand Up @@ -6,13 +6,22 @@
// Copyright 2011 Two Toasters. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#import "DBUser.h"
#import "DBLoginOrSignUpViewController.h"

@interface DBAuthenticatedTableViewController : TTTableViewController <DBLoginOrSignupViewControllerDelegate> {
BOOL _requiresLoggedInUser;
NSNumber* _requiredUserID;
DBUser* _requiredUser;
}

/**
* The User who we must be logged in as to edit the specified content
*/
@property (nonatomic, retain) DBUser* requiredUser;

/**
* Presents the Login controller if the current User is not authenticated
*/
- (void)presentLoginViewControllerIfNecessary;

@end
Expand Up @@ -11,39 +11,25 @@

@implementation DBAuthenticatedTableViewController

@synthesize requiredUser = _requiredUser;

- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self name:DBUserDidLoginNotification object:nil];
}

- (void)loadView {
[super loadView];

// Check if we are authenticated. If not, pop in login view controller.
if (_requiresLoggedInUser) {
BOOL isAuthenticated = NO;
if (_requiredUserID &&
[[DBUser currentUser] isLoggedIn] &&
[[DBUser currentUser].userID isEqualToNumber:_requiredUserID]) {
isAuthenticated = YES;
// TODO: Move this isAuthenticated logic into the model!
} else if (_requiredUserID == nil &&
[[DBUser currentUser] isLoggedIn]) {
isAuthenticated = YES;
}

if (!isAuthenticated) {
// Register for login succeeded notification. populate view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:DBUserDidLoginNotification object:nil];

DBLoginOrSignUpViewController* loginViewController = (DBLoginOrSignUpViewController*)TTOpenURL(@"db://login");
loginViewController.delegate = self;
}
- (void)presentLoginViewControllerIfNecessary {
if (NO == [[DBUser currentUser] isLoggedIn]) {
// Register for login succeeded notification. populate view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:DBUserDidLoginNotification object:nil];

DBLoginOrSignUpViewController* loginViewController = (DBLoginOrSignUpViewController*) TTOpenURL(@"db://login");
loginViewController.delegate = self;
}
}

- (void)userDidLogin:(NSNotification*)note {
// check to ensure the user that logged in is allowed to acces this controller.
if (_requiredUserID && [[DBUser currentUser].userID isEqualToNumber:_requiredUserID]) {
// Check to ensure the User who logged in is allowed to access this controller.
if ([[DBUser currentUser] isEqual:self.requiredUser]) {
[self invalidateModel];
} else {
[self.navigationController popViewControllerAnimated:YES];
Expand Down
Expand Up @@ -18,6 +18,7 @@ - (id)initWithNavigatorURL:(NSURL *)URL query:(NSDictionary *)query {
self.autoresizesForKeyboard = YES;
self.tableViewStyle = UITableViewStyleGrouped;
}

return self;
}

Expand Down Expand Up @@ -139,6 +140,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
} else if (textField == _passwordConfirmationField) {
[self loginOrSignup];
}

return NO;
}

Expand All @@ -153,7 +155,6 @@ - (void)user:(DBUser*)user didFailSignUpWithError:(NSError*)error {
}

- (void)user:(DBUser*)user didFailLoginWithError:(NSError*)error {
// TTAlert([error localizedDescription]);
[[[[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
Expand Down
Expand Up @@ -9,16 +9,25 @@
#import <Foundation/Foundation.h>
#import "DBAuthenticatedTableViewController.h"
#import "DBPost.h"
#import "DBTopic.h"

@interface DBPostTableViewController : DBAuthenticatedTableViewController <RKObjectLoaderDelegate, TTTextEditorDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
DBPost* _post;
DBTopic* _topic;

TTTextEditor* _bodyTextEditor;
TTImageView* _currentAttachmentImageView;

UIImage* _newAttachment;
}

/**
* The Post we are viewing
*/
@property (nonatomic, readonly) DBPost* post;

/**
* Three20 URL dispatched intializer. Used to create a new Post against a Topic with
* the specified primary key value.
*/
- (id)initWithTopicID:(NSString*)topicID;

@end
Expand Up @@ -9,9 +9,12 @@
#import "DBPostTableViewController.h"
#import <Three20/Three20+Additions.h>
#import "DBUser.h"
#import "DBTopic.h"

@implementation DBPostTableViewController

@synthesize post = _post;

- (id)initWithPostID:(NSString*)postID {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
_post = [[DBPost objectWithPrimaryKeyValue:postID] retain];
Expand All @@ -22,62 +25,55 @@ - (id)initWithPostID:(NSString*)postID {

- (id)initWithTopicID:(NSString*)topicID {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
// TODO: Why are we using topic ID here?
_topic = [[DBTopic objectWithPrimaryKeyValue:topicID] retain];
DBTopic* topic = [[DBTopic objectWithPrimaryKeyValue:topicID] retain];
_post = [[DBPost object] retain];
_post.topicID = topic.topicID;
_post.topic = topic;
}

return self;
}

- (void)dealloc {
TT_RELEASE_SAFELY(_post);
TT_RELEASE_SAFELY(_topic);

[super dealloc];
}

// TODO: Move this into the model
- (BOOL)isNewRecord {
return [[_post postID] intValue] == 0;
}

- (void)viewDidUnload {
TT_RELEASE_SAFELY(_bodyTextEditor);

[[TTNavigator navigator].URLMap removeURL:@"db://updateAttachment"];
}

- (void)loadView {
[super loadView];

self.tableViewStyle = UITableViewStyleGrouped;
self.autoresizesForKeyboard = YES;
self.variableHeightRows = YES;

[[TTNavigator navigator].URLMap from:@"db://updateAttachment" toObject:self selector:@selector(updateAttachment)];

if (nil == _post) {
_post = [[DBPost object] retain];
_post.topicID = _topic.topicID;
}

_requiresLoggedInUser = YES;

[super loadView];
[[TTNavigator navigator].URLMap from:@"db://updateAttachment" toObject:self selector:@selector(updateAttachment)];

_bodyTextEditor = [[TTTextEditor alloc] initWithFrame:CGRectMake(0, 0, 300, 120)];
_bodyTextEditor.font = [UIFont systemFontOfSize:12];
_bodyTextEditor.autoresizesToText = NO;
_bodyTextEditor.delegate = self;
_bodyTextEditor.text = _post.body;

// Ensure we are authenticated
NSLog(@"All the users are: %@", [DBUser allObjects]);
[self presentLoginViewControllerIfNecessary];
}

- (void)createModel {
// TODO: Move this into the model: [self.currentUser canModifyObject:_post]
BOOL isAuthorizedUser = [[DBUser currentUser].userID isEqualToNumber:_post.userID] || [self isNewRecord];

BOOL isAuthorizedUser = [[DBUser currentUser] canModifyObject:_post];
NSMutableArray* items = [NSMutableArray array];

// Attachment item.
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"]];
Expand All @@ -96,7 +92,7 @@ - (void)createModel {
[items addObject:[TTTableImageItem itemWithText:@"" imageURL:url URL:nil]];
}

if ([self isNewRecord]) {
if ([self.post isNewRecord]) {
self.title = @"New Post";
[items addObject:[TTTableButton itemWithText:@"Create" delegate:self selector:@selector(createButtonWasPressed:)]];
} else {
Expand All @@ -108,8 +104,9 @@ - (void)createModel {
self.title = @"Post";
}
}

NSString* byLine = @"";
if (![self isNewRecord]) {
if (![self.post isNewRecord]) {
NSString* username = (isAuthorizedUser ? @"me" : _post.username);
byLine = [NSString stringWithFormat:@"posted by %@", username];
}
Expand All @@ -126,19 +123,19 @@ - (void)updateAttachment {
#pragma mark Actions

- (void)createButtonWasPressed:(id)sender {
_post.body = _bodyTextEditor.text;
_post.newAttachment = _newAttachment;
[[RKObjectManager sharedManager] postObject:_post delegate:self];
self.post.body = _bodyTextEditor.text;
self.post.newAttachment = _newAttachment;
[[RKObjectManager sharedManager] postObject:self.post delegate:self];
}

- (void)updateButtonWasPressed:(id)sender {
_post.body = _bodyTextEditor.text;
_post.newAttachment = _newAttachment;
[[RKObjectManager sharedManager] putObject:_post delegate:self];
self.post.body = _bodyTextEditor.text;
self.post.newAttachment = _newAttachment;
[[RKObjectManager sharedManager] putObject:self.post delegate:self];
}

- (void)destroyButtonWasPressed:(id)sender {
[[RKObjectManager sharedManager] deleteObject:_post delegate:self];
[[RKObjectManager sharedManager] deleteObject:self.post delegate:self];
}

#pragma mark UIImagePickerControllerDelegate methods
Expand All @@ -156,18 +153,20 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
#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
// TODO: Generalize this notification
[[NSNotificationCenter defaultCenter] postNotificationName:kObjectCreatedUpdatedOrDestroyedNotificationName object:objects];
// dismiss.
[self.navigationController popViewControllerAnimated:YES];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
[[[[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}

#pragma mark TTTextEditorDelegate methods
Expand Down
Expand Up @@ -14,8 +14,7 @@
* Displays a table of Posts within a given Topic
*/
@interface DBPostsTableViewController : DBResourceListTableViewController {
// TODO: Just use a Topic
NSString* _topicID;
DBTopic* _topic;
}

/**
Expand Down

0 comments on commit ebd2355

Please sign in to comment.