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 | |
parent | ff23c4eb5d382d25c2e4779e0ea8f4b724651105 (diff) |
using RestKit
-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 | ||||
-rw-r--r-- | Podfile | 2 | ||||
-rw-r--r-- | Podfile.lock | 34 |
6 files changed, 76 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]; @@ -6,6 +6,8 @@ pod 'MWFeedParser' pod 'TTTAttributedLabel' pod 'Masonry' pod 'SWRevealViewController' +pod 'PDKeychainBindingsController' +pod 'RestKit' end target "JuickTests" do diff --git a/Podfile.lock b/Podfile.lock index 9c2ff0f..7c09c02 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,4 +1,7 @@ PODS: + - AFNetworking (1.3.3) + - ISO8601DateFormatterValueTransformer (0.5.0): + - RKValueTransformers (~> 1.0.0) - Masonry (0.2.4) - MWFeedParser (0.0.1): - MWFeedParser/Feed @@ -9,19 +12,50 @@ PODS: - MWFeedParser/NSDate+InternetDateTime (0.0.1) - MWFeedParser/NSString+HTML (0.0.1) - MWFeedParser/NSString+XMLEntities (0.0.1) + - PDKeychainBindingsController (0.0.1) + - RestKit (0.21.0): + - RestKit/Core + - RestKit/Core (0.21.0): + - RestKit/CoreData + - RestKit/Network + - RestKit/ObjectMapping + - RestKit/CoreData (0.21.0) + - RestKit/Network (0.21.0): + - AFNetworking (~> 1.3.0) + - RestKit/ObjectMapping + - RestKit/Support + - SOCKit + - RestKit/ObjectMapping (0.21.0): + - ISO8601DateFormatterValueTransformer (~> 0.5.0) + - RestKit/Support + - RKValueTransformers (~> 1.0.0) + - RestKit/Support (0.21.0): + - TransitionKit (= 1.1.1) + - RKValueTransformers (1.0.0) + - SOCKit (1.1) - SWRevealViewController (1.0.8) + - TransitionKit (1.1.1) - TTTAttributedLabel (1.7.1) DEPENDENCIES: - Masonry - MWFeedParser + - PDKeychainBindingsController + - RestKit - SWRevealViewController - TTTAttributedLabel SPEC CHECKSUMS: + AFNetworking: 0700ec7a58c36ad217173e167f6e4df7270df66b + ISO8601DateFormatterValueTransformer: d0af1f50a9df42db72d7418db31938b8473e0af8 Masonry: 73df2f346bd1a0044dc9de969aa45aea2f8a9bd8 MWFeedParser: 2ca0852fac352f8333d5f46fdd4b583b921e0c4e + PDKeychainBindingsController: 463c207f8726e4aaec1ec20fd1876082ee86cb0a + RestKit: c11a30e7bc73513d9d2c2695672aba7697d24181 + RKValueTransformers: 01dc043fd853f6e40459b2e732dff545298a0f91 + SOCKit: 2f3bc4d07910de12dcc202815e07db68a3802581 SWRevealViewController: 5688d9d017e228c9edd92db359f670d8c47ab8e6 + TransitionKit: d0e3344aac92991395d4c2e72d9c5a8ceeb12910 TTTAttributedLabel: e504133915a9b11de63cb470ffa2790383a3eb23 COCOAPODS: 0.27.1 |