diff options
author | Vitaly Takmazov | 2013-11-07 14:06:59 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2013-11-07 14:06:59 +0400 |
commit | 508ba6b17f37e60af73fe5e5168b3d0b88ddd085 (patch) | |
tree | d4f8f269b95bf7232d09521c02e7920b238a9835 /Juick | |
parent | ff23c4eb5d382d25c2e4779e0ea8f4b724651105 (diff) |
using RestKit
Diffstat (limited to 'Juick')
-rw-r--r-- | Juick/Message.h | 7 | ||||
-rw-r--r-- | Juick/Message.m | 57 | ||||
-rw-r--r-- | Juick/MessagesViewController.h | 1 | ||||
-rw-r--r-- | Juick/MessagesViewController.m | 29 |
4 files changed, 40 insertions, 54 deletions
diff --git a/Juick/Message.h b/Juick/Message.h index 5ec3732..233f5ba 100644 --- a/Juick/Message.h +++ b/Juick/Message.h @@ -7,7 +7,7 @@ // #import <Foundation/Foundation.h> -#import "NSString+HTML.h" +#import <RestKit/RestKit.h> @interface Message : NSObject @@ -16,9 +16,8 @@ @property(nonatomic, copy) NSString *user; @property(nonatomic, copy) NSDecimalNumber *userID; @property(nonatomic, copy) NSString *text; +@property(nonatomic, copy) NSArray *tags; --(id) initWithDictionary:(NSDictionary *)dictionary; - -+(NSArray *) pullNextFromURL:(NSURL *)url; ++(void) pullNextFromURL:(NSURL *)url callback:(void(^)(NSArray *))callback; @end diff --git a/Juick/Message.m b/Juick/Message.m index 2c35ef4..f6fae85 100644 --- a/Juick/Message.m +++ b/Juick/Message.m @@ -10,43 +10,28 @@ @implementation Message --(id)initWithDictionary:(NSDictionary *)dictionary -{ - Message *result = [[Message alloc] init]; - result.MID = [dictionary objectForKey:@"mid"]; - result.RID = [dictionary objectForKey:@"rid"]; - NSString *text = [[dictionary objectForKey:@"body"] stringByDecodingHTMLEntities]; - NSDictionary *userDict = [dictionary objectForKey:@"user"]; - result.user = [userDict objectForKey:@"uname"]; - result.userID = [userDict objectForKey:@"uid"]; - NSArray *tagsArray = [dictionary objectForKey:@"tags"]; - if ([tagsArray count] > 0) { - result.text = [[NSString alloc] initWithFormat:@"%@\n%@", [tagsArray componentsJoinedByString:@", "], text]; - } else { - result.text = text; - } - return result; -} -+(NSArray *) pullNextFromURL:(NSURL *)url { - // Set the gesture - NSError *error; - NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; - if (data == nil) { - NSLog(@"Download Error: %@", error); - return nil; - } - NSArray *resultObject = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; - if (resultObject == nil) { - NSLog(@"JSON Error: %@", error); - return nil; - } - NSMutableArray *result = [NSMutableArray array]; - for (NSDictionary *message in resultObject) { - Message *msg = [[Message alloc] initWithDictionary:message]; - [result addObject:msg]; - } - return [result copy]; ++(void) pullNextFromURL:(NSURL *)url callback:(void(^)(NSArray *))callback { + RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Message class]]; + [mapping addAttributeMappingsFromDictionary:@ + { + @"user.uname": @"user", + @"user.uid" : @"userID", + @"body" : @"text", + @"mid" : @"MID", + @"rid" : @"RID", + @"tags" : @"tags" + }]; + RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request + responseDescriptors:@[responseDescriptor]]; + [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { + callback([mappingResult array]); + } failure:^(RKObjectRequestOperation *operation, NSError *error) { + NSLog(@"REST Error: %@", error); + }]; + [operation start]; } @end diff --git a/Juick/MessagesViewController.h b/Juick/MessagesViewController.h index 3e6af7e..e3391db 100644 --- a/Juick/MessagesViewController.h +++ b/Juick/MessagesViewController.h @@ -7,6 +7,7 @@ // #import <UIKit/UIKit.h> +#import "NSString+HTML.h" @interface MessagesViewController : UITableViewController<UISplitViewControllerDelegate> - (void)loadFromURL:(NSURL *)messagesURL withTitle:(NSString *)title; diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m index 6c594ba..fa53875 100644 --- a/Juick/MessagesViewController.m +++ b/Juick/MessagesViewController.m @@ -42,11 +42,7 @@ static NSString *CellIdentifier = @"MessageCell"; - (void) refreshData:(UIRefreshControl *)refresh { [self.messages removeAllObjects]; [self.tableView reloadData]; - - NSArray *next = [Message pullNextFromURL:self.url]; - int64_t delayInSeconds = 1.0; - dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); - dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ + [Message pullNextFromURL:self.url callback:^(NSArray *next) { [self.tableView beginUpdates]; CGPoint offset = self.tableView.contentOffset; for (int i = 0; i < [next count]; i++) { @@ -56,7 +52,7 @@ static NSString *CellIdentifier = @"MessageCell"; [self.tableView setContentOffset:offset animated:NO]; [self.tableView endUpdates]; [refresh endRefreshing]; - }); + }]; } - (UIStatusBarStyle)preferredStatusBarStyle @@ -71,10 +67,8 @@ static NSString *CellIdentifier = @"MessageCell"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ self.dataLoading = YES; Message * lastMessage = [self.messages lastObject]; - NSArray *next = [Message pullNextFromURL:[self.url URLByAppendingParameters:[NSDictionary dictionaryWithObjectsAndKeys:lastMessage.MID, @"before_mid", nil]]]; - int64_t delayInSeconds = 1.0; - dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); - dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ + NSURL *beforeUrl = [self.url URLByAppendingParameters:[NSDictionary dictionaryWithObjectsAndKeys:lastMessage.MID, @"before_mid", nil]]; + [Message pullNextFromURL:beforeUrl callback:^(NSArray *next) { [self.tableView beginUpdates]; for (int i = 0; i < [next count]; i++) { [self.messages addObject:[next objectAtIndex:i]]; @@ -83,7 +77,7 @@ static NSString *CellIdentifier = @"MessageCell"; [self.tableView setContentOffset:pos animated:NO]; [self.tableView endUpdates]; self.dataLoading = NO; - }); + }]; }); } @@ -167,7 +161,11 @@ static NSString *CellIdentifier = @"MessageCell"; }]; cell.titleLabel.text = msg.user; cell.titleLabel.textColor = [ColorsAndButtons linkColor]; - cell.bodyLabel.text = msg.text; + cell.bodyLabel.text = [msg.text stringByDecodingHTMLEntities]; + if ([msg.tags count] > 0) { + cell.bodyLabel.text = [[NSString alloc] initWithFormat:@"%@\n%@", [msg.tags componentsJoinedByString:@", "], + [msg.text stringByDecodingHTMLEntities]]; + } [cell setBackgroundColor:[UIColor whiteColor]]; [cell setNeedsUpdateConstraints]; return cell; @@ -187,8 +185,11 @@ static NSString *CellIdentifier = @"MessageCell"; }]; cell.titleLabel.text = msg.user; - cell.bodyLabel.text = msg.text; - + cell.bodyLabel.text = [msg.text stringByDecodingHTMLEntities]; + if ([msg.tags count] > 0) { + cell.bodyLabel.text = [[NSString alloc] initWithFormat:@"%@\n%@", [msg.tags componentsJoinedByString:@", "], + [msg.text stringByDecodingHTMLEntities]]; + } cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f); [cell setNeedsUpdateConstraints]; |