mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
constant view tags.
move header constant to environment. move setting/clearing of user token header to user model.
This commit is contained in:
@@ -9,37 +9,40 @@
|
||||
#import "UIViewController+RKLoading.h"
|
||||
#import <Three20/Three20.h>
|
||||
|
||||
static NSInteger const kOverlayViewTag = 66;
|
||||
static NSInteger const kProgressViewTag = 67;
|
||||
|
||||
@implementation UIViewController (RKLoading)
|
||||
|
||||
- (void)requestDidStartLoad:(RKRequest*)request {
|
||||
UIView* overlayView = [self.view viewWithTag:66]; // TODO: Need constant for tags...
|
||||
UIView* overlayView = [self.view viewWithTag:kOverlayViewTag]; // TODO: Need constant for tags...
|
||||
if (overlayView == nil) {
|
||||
overlayView = [[TTActivityLabel alloc] initWithFrame:self.view.bounds style:TTActivityLabelStyleBlackBox text:@"Loading..."];
|
||||
overlayView.backgroundColor = [UIColor blackColor];
|
||||
overlayView.alpha = 0.5;
|
||||
overlayView.tag = 66;
|
||||
overlayView.tag = kOverlayViewTag;
|
||||
|
||||
[self.view addSubview:overlayView];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)request:(RKRequest*)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
|
||||
UIProgressView* progressView = (UIProgressView*)[self.view viewWithTag:67];
|
||||
UIProgressView* progressView = (UIProgressView*)[self.view viewWithTag:kProgressViewTag];
|
||||
if (progressView == nil) {
|
||||
if (totalBytesWritten >= totalBytesExpectedToWrite) {
|
||||
// Uploaded all data at once. don't need a progress bar.
|
||||
return;
|
||||
}
|
||||
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 300, 20)];
|
||||
progressView.tag = 67; // TODO: Need constant for tags...
|
||||
progressView.tag = kProgressViewTag;
|
||||
[self.view addSubview:progressView];
|
||||
}
|
||||
progressView.progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
|
||||
}
|
||||
|
||||
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
|
||||
UIView* overlayView = [self.view viewWithTag:66]; // TODO: Need constant for tags...
|
||||
UIView* progressView = [self.view viewWithTag:67];
|
||||
UIView* overlayView = [self.view viewWithTag:kOverlayViewTag];
|
||||
UIView* progressView = [self.view viewWithTag:kProgressViewTag];
|
||||
[overlayView removeFromSuperview];
|
||||
[overlayView release];
|
||||
[progressView removeFromSuperview];
|
||||
|
||||
@@ -101,6 +101,10 @@ NSString* const DBUserDidLogoutNotification = @"DBUserDidLogoutNotification";
|
||||
if ([self.delegate respondsToSelector:@selector(userDidLogin:)]) {
|
||||
[self.delegate userDidLogin:self];
|
||||
}
|
||||
|
||||
RKObjectManager* objectManager = [RKObjectManager sharedManager];
|
||||
[objectManager.client setValue:[DBUser currentUser].singleAccessToken forHTTPHeaderField:kAccessTokenHeaderField];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:DBUserDidLoginNotification object:user];
|
||||
} else if ([objectLoader wasSentToResourcePath:@"/logout"]) {
|
||||
// Logout was successful
|
||||
@@ -113,7 +117,10 @@ NSString* const DBUserDidLogoutNotification = @"DBUserDidLogoutNotification";
|
||||
if ([self.delegate respondsToSelector:@selector(userDidLogout:)]) {
|
||||
[self.delegate userDidLogout:self];
|
||||
}
|
||||
|
||||
|
||||
RKObjectManager* objectManager = [RKObjectManager sharedManager];
|
||||
[objectManager.client setValue:nil forHTTPHeaderField:kAccessTokenHeaderField];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:DBUserDidLogoutNotification object:nil];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,3 +19,5 @@ extern NSString* const DBRestKitBaseURL;
|
||||
|
||||
// TODO: See if we can eliminate or abstract this further
|
||||
extern NSString* const kObjectCreatedUpdatedOrDestroyedNotificationName;
|
||||
|
||||
extern NSString* const kAccessTokenHeaderField;
|
||||
@@ -12,3 +12,5 @@
|
||||
NSString* const DBRestKitBaseURL = @"http://localhost:3000";
|
||||
//NSString* const kDBBaseURLString = @"http://discussionboard.heroku.com";
|
||||
NSString* const kObjectCreatedUpdatedOrDestroyedNotificationName = @"kObjectCreatedUpdatedOrDestroyedNotificationName";
|
||||
|
||||
NSString* const kAccessTokenHeaderField = @"X-USER-ACCESS-TOKEN";
|
||||
@@ -28,9 +28,6 @@
|
||||
#import "DBUser.h"
|
||||
#import "DBPostTableViewController.h"
|
||||
|
||||
// TODO: Move this to the environment file?
|
||||
static NSString* const kAccessTokenHeaderField = @"X-USER-ACCESS-TOKEN";
|
||||
|
||||
@implementation DiscussionBoardAppDelegate
|
||||
|
||||
@synthesize window;
|
||||
@@ -95,29 +92,14 @@ static NSString* const kAccessTokenHeaderField = @"X-USER-ACCESS-TOKEN";
|
||||
TTOpenURL(@"db://topics");
|
||||
[[TTNavigator navigator].window makeKeyAndVisible];
|
||||
|
||||
// Authentication
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoggedIn:) name:DBUserDidLoginNotification object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoggedOut:) name:DBUserDidLogoutNotification object:nil];
|
||||
|
||||
DBUser* user = [DBUser currentUser];
|
||||
NSLog(@"Token: %@", user.singleAccessToken);
|
||||
NSLog(@"User: %@", user);
|
||||
[objectManager.client setValue:[DBUser currentUser].singleAccessToken forHTTPHeaderField:@"USER_ACCESS_TOKEN"];
|
||||
[objectManager.client setValue:[DBUser currentUser].singleAccessToken forHTTPHeaderField:kAccessTokenHeaderField];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
// TODO: Move this login shit into the DBUser model
|
||||
- (void)userLoggedIn:(NSNotification*)note {
|
||||
RKObjectManager* objectManager = [RKObjectManager sharedManager];
|
||||
[objectManager.client setValue:[DBUser currentUser].singleAccessToken forHTTPHeaderField:kAccessTokenHeaderField];
|
||||
}
|
||||
|
||||
- (void)userLoggedOut:(NSNotification*)note {
|
||||
RKObjectManager* objectManager = [RKObjectManager sharedManager];
|
||||
[objectManager.client setValue:nil forHTTPHeaderField:kAccessTokenHeaderField];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[window release];
|
||||
[super dealloc];
|
||||
|
||||
Reference in New Issue
Block a user