From a74588083c4950e9237dff7a84ccdd129b72c950 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 22 Aug 2016 00:44:28 +0300 Subject: RestKit -> YYModel --- Juick/APIClient.h | 13 ++++++++++++ Juick/APIClient.m | 28 +++++++++++++++++++++++++ Juick/AppDelegate.m | 4 ---- Juick/Juick-Prefix.pch | 1 + Juick/Message.h | 5 ++--- Juick/Message.m | 43 ++++++++++++++++---------------------- Juick/MessagesViewController.m | 20 +++++++++++------- Juick/User.h | 5 ++++- Juick/User.m | 47 +++++++++++------------------------------- Juick/Views/MessageCell.m | 4 ++-- 10 files changed, 92 insertions(+), 78 deletions(-) create mode 100644 Juick/APIClient.h create mode 100644 Juick/APIClient.m (limited to 'Juick') diff --git a/Juick/APIClient.h b/Juick/APIClient.h new file mode 100644 index 0000000..8546d4d --- /dev/null +++ b/Juick/APIClient.h @@ -0,0 +1,13 @@ +// +// APIClient.h +// Juick +// +// Created by Vitaly Takmazov on 22/08/16. +// Copyright © 2016 com.juick. All rights reserved. +// +#import + +@interface APIClient : NSObject + @property (nonatomic, retain) AFHTTPSessionManager *manager; + +(APIClient *) sharedClient; +@end \ No newline at end of file diff --git a/Juick/APIClient.m b/Juick/APIClient.m new file mode 100644 index 0000000..1fc2743 --- /dev/null +++ b/Juick/APIClient.m @@ -0,0 +1,28 @@ +// +// APIClient.m +// Juick +// +// Created by Vitaly Takmazov on 22/08/16. +// Copyright © 2016 com.juick. All rights reserved. +// +#import "APIClient.h" + +@implementation APIClient + ++(APIClient *) sharedClient { + static APIClient *sharedAPIClient = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedAPIClient = [[self alloc] init]; + }); + return sharedAPIClient; +} + +-(id)init { + if (self = [super init]) { + self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.juick.com"]]; + } + return self; +} + +@end diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 5fe48fe..9dc5362 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -27,10 +27,6 @@ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; - NSURL *baseURL = [NSURL URLWithString:@"https://api.juick.com"]; - RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL]; - [RKObjectManager setSharedManager:objectManager]; - MessagesViewController *messages = [[MessagesViewController alloc] init]; RevealPanelViewController *rear = [[RevealPanelViewController alloc] init]; diff --git a/Juick/Juick-Prefix.pch b/Juick/Juick-Prefix.pch index c48dc95..c80380f 100644 --- a/Juick/Juick-Prefix.pch +++ b/Juick/Juick-Prefix.pch @@ -21,4 +21,5 @@ #import "NSDate+TimeAgo.h" #import "TTTAttributedLabel.h" #import + #import #endif diff --git a/Juick/Message.h b/Juick/Message.h index 3f11c0e..39d3c38 100644 --- a/Juick/Message.h +++ b/Juick/Message.h @@ -7,14 +7,13 @@ // #import -#import +#import "User.h" @interface Message : NSObject @property NSNumber *MID; @property NSNumber *RID; -@property NSString *user; -@property NSNumber *userID; +@property User *user; @property NSString *text; @property NSArray *tags; @property NSString *timestamp; diff --git a/Juick/Message.m b/Juick/Message.m index 5ba12d2..de08e98 100644 --- a/Juick/Message.m +++ b/Juick/Message.m @@ -11,37 +11,30 @@ @implementation Message ++ (NSDictionary *)modelCustomPropertyMapper { + return @{@"text" : @"body", + @"MID" : @"mid", + @"RID" : @"rid", + @"attach" : @"photo.small", + @"repliesCount": @"replies", + @"repliesBy": @"repliesby"}; +} + +(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) { - RKObjectManager *manager = [RKObjectManager sharedManager]; - [manager.HTTPClient setAuthorizationHeaderWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; } - RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Message class]]; - [mapping addAttributeMappingsFromDictionary:@ - { - @"user.uname": @"user", - @"user.uid" : @"userID", - @"body" : @"text", - @"mid" : @"MID", - @"rid" : @"RID", - @"tags" : @"tags", - @"timestamp" : @"timestamp", - @"photo.small" : @"attach", - @"replies" : @"repliesCount", - @"repliesby": @"repliesBy" - - }]; - RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; - NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodGET path:path parameters:params]; - RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request - responseDescriptors:@[responseDescriptor]]; - [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { - callback([mappingResult array]); - } failure:^(RKObjectRequestOperation *operation, NSError *error) { + [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSMutableArray *messages = [NSMutableArray new]; + [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [messages addObject:[Message yy_modelWithJSON:obj]]; + }]; + callback(messages); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); }]; - [operation start]; } +(NSString *) messagesUrl { diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m index 343bb2c..28e7613 100644 --- a/Juick/MessagesViewController.m +++ b/Juick/MessagesViewController.m @@ -48,10 +48,12 @@ static NSString *CellIdentifier = @"MessageCell"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ self.dataLoading = YES; [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { - [self.messages addObjectsFromArray:next]; - [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; - [self.refreshControl performSelectorOnMainThread:@selector(endRefreshing) withObject:nil waitUntilDone:NO]; - self.dataLoading = NO; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.messages addObjectsFromArray:next]; + [self.tableView reloadData]; + [self.refreshControl endRefreshing]; + self.dataLoading = NO; + }); }]; }); } @@ -131,10 +133,12 @@ static NSString *CellIdentifier = @"MessageCell"; if ([msg.attach length] > 0) { __weak MessagesViewController * weakSelf = self; [cell.attach yy_setImageWithURL:[NSURL URLWithString:msg.attach] placeholder:[UIImage imageNamed:@"AttachPlaceholder"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { - [cell setNeedsUpdateConstraints]; - [cell updateConstraintsIfNeeded]; - [cell setNeedsLayout]; - [cell layoutIfNeeded]; + dispatch_async(dispatch_get_main_queue(), ^{ + [cell setNeedsUpdateConstraints]; + [cell updateConstraintsIfNeeded]; + [cell setNeedsLayout]; + [cell layoutIfNeeded]; + }); }]; } else { diff --git a/Juick/User.h b/Juick/User.h index e4c46f3..84300f3 100644 --- a/Juick/User.h +++ b/Juick/User.h @@ -7,7 +7,10 @@ // #import -#import +#import "PDKeychainBindings.h" +#import "SIAlertView.h" +#import "APIClient.h" +#import "YYModel.h" @interface User : NSObject @property (nonatomic, strong) NSString *uname; diff --git a/Juick/User.m b/Juick/User.m index 97f4232..f4b1e00 100644 --- a/Juick/User.m +++ b/Juick/User.m @@ -7,8 +7,7 @@ // #import "User.h" -#import "PDKeychainBindings.h" -#import "SIAlertView.h" + @implementation User @@ -25,48 +24,26 @@ } +(void) checkIsValid:(void (^)(BOOL))callback { - RKObjectManager *manager = [RKObjectManager sharedManager]; - [manager.HTTPClient setAuthorizationHeaderWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; - NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodPOST path:@"/post" parameters:nil]; - RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; - - RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; - - RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request - responseDescriptors:@[responseDescriptor]]; - [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { - // this will never happens - } failure:^(RKObjectRequestOperation *operation, NSError *error) { - if (operation.HTTPRequestOperation.response.statusCode == 400) { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + [manager POST:@"/post" parameters:nil progress:nil success:nil failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; + if (statusCode == 400) { callback(YES); } else { - [manager.HTTPClient setAuthorizationHeaderWithUsername:nil password:nil]; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:nil password:nil]; callback(NO); } - }]; - [operation start]; - -} + }];} +(void) get:(NSString *) name callback:(void(^)(User *))callback { - RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; - [mapping addAttributeMappingsFromDictionary:@ - { - @"uname": @"uname", - @"uid" : @"uid", - }]; - NSString *path = @"/users"; + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; - RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; - NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodGET path:path parameters:params]; - RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request - responseDescriptors:@[responseDescriptor]]; - [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { - callback([[mappingResult array] firstObject]); - } failure:^(RKObjectRequestOperation *operation, NSError *error) { + [manager GET:@"/users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { callback(nil); }]; - [operation start]; } @end diff --git a/Juick/Views/MessageCell.m b/Juick/Views/MessageCell.m index 7117052..1ffc1c9 100644 --- a/Juick/Views/MessageCell.m +++ b/Juick/Views/MessageCell.m @@ -23,8 +23,8 @@ - (void) setMessage:(Message *)msg { _message = msg; - self.avatar.yy_imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://i.juick.com/a/%d.png", [msg.userID intValue]]]; - self.title.text = msg.user; + self.avatar.yy_imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://i.juick.com/a/%d.png", [msg.user.uid intValue]]]; + self.title.text = msg.user.uname; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; -- cgit v1.2.3