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.xcodeproj/project.pbxproj | 6 +++++ 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 +-- Podfile | 3 ++- Podfile.lock | 54 ++++++++++++++++------------------------- 13 files changed, 121 insertions(+), 112 deletions(-) create mode 100644 Juick/APIClient.h create mode 100644 Juick/APIClient.m diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index 443bcc3..fff0dfb 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -42,6 +42,7 @@ 77C67EEA18283C8D00427098 /* buttons.png in Resources */ = {isa = PBXBuildFile; fileRef = 77C67EE918283C8D00427098 /* buttons.png */; }; 77C67EEC18283F2D00427098 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77C67EEB18283F2D00427098 /* QuartzCore.framework */; }; 77E35A82189A5B5A00B2D216 /* LoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77E35A81189A5B5A00B2D216 /* LoginViewController.m */; }; + 77FCADDF1D6A50DA00CBA649 /* APIClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 77FCADDE1D6A50DA00CBA649 /* APIClient.m */; }; 77FFC0191D5FD186003BD81A /* MessageCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 77FFC0171D5FD186003BD81A /* MessageCell.m */; }; 77FFC01A1D5FD186003BD81A /* MessageCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77FFC0181D5FD186003BD81A /* MessageCell.xib */; }; 77FFC01E1D5FE7CE003BD81A /* NavCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 77FFC01C1D5FE7CE003BD81A /* NavCell.m */; }; @@ -108,6 +109,8 @@ 77C67EEB18283F2D00427098 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 77E35A80189A5B5A00B2D216 /* LoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginViewController.h; sourceTree = ""; }; 77E35A81189A5B5A00B2D216 /* LoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginViewController.m; sourceTree = ""; }; + 77FCADDE1D6A50DA00CBA649 /* APIClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APIClient.m; sourceTree = ""; }; + 77FCADE01D6A50EC00CBA649 /* APIClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = APIClient.h; sourceTree = ""; }; 77FFC0161D5FD186003BD81A /* MessageCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MessageCell.h; path = Views/MessageCell.h; sourceTree = ""; }; 77FFC0171D5FD186003BD81A /* MessageCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MessageCell.m; path = Views/MessageCell.m; sourceTree = ""; }; 77FFC0181D5FD186003BD81A /* MessageCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = MessageCell.xib; path = Views/MessageCell.xib; sourceTree = ""; }; @@ -231,6 +234,8 @@ 77B09993189D0B9900A84F59 /* UIImage+Utils.m */, 77B099A4189D57F000A84F59 /* User.h */, 77B099A5189D57F000A84F59 /* User.m */, + 77FCADDE1D6A50DA00CBA649 /* APIClient.m */, + 77FCADE01D6A50EC00CBA649 /* APIClient.h */, ); path = Juick; sourceTree = ""; @@ -486,6 +491,7 @@ buildActionMask = 2147483647; files = ( 77317BBC181BBE8500D60005 /* AppDelegate.m in Sources */, + 77FCADDF1D6A50DA00CBA649 /* APIClient.m in Sources */, 77A0954A181F1F25002BDECD /* Message.m in Sources */, 77B09994189D0B9900A84F59 /* UIImage+Utils.m in Sources */, 77317BC2181BBE8500D60005 /* MessagesViewController.m in Sources */, 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"]]; diff --git a/Podfile b/Podfile index eb20b2c..f33fa1c 100644 --- a/Podfile +++ b/Podfile @@ -2,11 +2,12 @@ platform :ios, "9.0" target "Juick" do + pod 'AFNetworking' pod 'MWFeedParser' pod 'TTTAttributedLabel' pod 'SWRevealViewController' pod 'PDKeychainBindingsController' - pod 'RestKit', '~> 0.26.0' + pod 'YYModel' pod 'YYWebImage' pod 'FontAwesome+iOS' pod 'NSDate+TimeAgo' diff --git a/Podfile.lock b/Podfile.lock index 9f8acf8..399303f 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,8 +1,20 @@ PODS: - - AFNetworking (1.3.4) + - AFNetworking (3.1.0): + - AFNetworking/NSURLSession (= 3.1.0) + - AFNetworking/Reachability (= 3.1.0) + - AFNetworking/Security (= 3.1.0) + - AFNetworking/Serialization (= 3.1.0) + - AFNetworking/UIKit (= 3.1.0) + - AFNetworking/NSURLSession (3.1.0): + - AFNetworking/Reachability + - AFNetworking/Security + - AFNetworking/Serialization + - AFNetworking/Reachability (3.1.0) + - AFNetworking/Security (3.1.0) + - AFNetworking/Serialization (3.1.0) + - AFNetworking/UIKit (3.1.0): + - AFNetworking/NSURLSession - FontAwesome+iOS (0.1.4) - - ISO8601DateFormatterValueTransformer (0.6.1): - - RKValueTransformers (~> 1.1.0) - MWFeedParser (1.0.1): - MWFeedParser/FeedParser (= 1.0.1) - MWFeedParser/NSDate+InternetDateTime (= 1.0.1) @@ -14,71 +26,47 @@ PODS: - MWFeedParser/NSString+HTML (1.0.1) - NSDate+TimeAgo (1.0.6) - PDKeychainBindingsController (0.0.1) - - RestKit (0.26.0): - - RestKit/Core (= 0.26.0) - - RestKit/Core (0.26.0): - - RestKit/CoreData - - RestKit/Network - - RestKit/ObjectMapping - - RestKit/CoreData (0.26.0): - - RestKit/ObjectMapping - - RestKit/Network (0.26.0): - - AFNetworking (~> 1.3.0) - - RestKit/ObjectMapping - - RestKit/Support - - SOCKit - - RestKit/ObjectMapping (0.26.0): - - ISO8601DateFormatterValueTransformer (~> 0.6.1) - - RestKit/Support - - RKValueTransformers (~> 1.1.0) - - RestKit/Support (0.26.0): - - TransitionKit (~> 2.2) - - RKValueTransformers (1.1.3) - SIAlertView (1.3) - - SOCKit (1.1) - SWRevealViewController (2.3.0) - - TransitionKit (2.2.1) - TTTAttributedLabel (2.0.0) - XLForm (3.2.0) - YYCache (1.0.3) - YYImage (1.0.3): - YYImage/Core (= 1.0.3) - YYImage/Core (1.0.3) + - YYModel (1.0.4) - YYWebImage (1.0.4): - YYCache - YYImage DEPENDENCIES: + - AFNetworking - FontAwesome+iOS - MWFeedParser - NSDate+TimeAgo - PDKeychainBindingsController - - RestKit (~> 0.26.0) - SIAlertView - SWRevealViewController - TTTAttributedLabel - XLForm + - YYModel - YYWebImage SPEC CHECKSUMS: - AFNetworking: cf8e418e16f0c9c7e5c3150d019a3c679d015018 + AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67 FontAwesome+iOS: 9070b682a9255ca0fdc34bcfb129ad0ba2b73b87 - ISO8601DateFormatterValueTransformer: 52da467d6ec899d6aedda8e48280ac92e8ee97e6 MWFeedParser: 2cf646014c1baf6ad1b08c480b40a08180079247 NSDate+TimeAgo: 35601c619b2d59290055e4fe76e61d97677a2360 PDKeychainBindingsController: fa8cb3cf99f2ea9240d61066ed0549f34e2cec3c - RestKit: 112fba5bd1f97598aac0db7981863d5fdc0263a2 - RKValueTransformers: e5ed67e3811229b616fe01bddeeafe3bb337b1b9 SIAlertView: 20c794b55eaf44d888773ea51647cf8907723919 - SOCKit: c7376ac262bea9115b8f749358f762522a47d392 SWRevealViewController: 6d3fd97f70112fd7cef9de14df4260eacce4c63a - TransitionKit: 9ceccda4cd0cdc0a05ef85eb235e5a3292c3c250 TTTAttributedLabel: 8cffe8e127e4e82ff3af1e5386d4cd0ad000b656 XLForm: 2250b4e96ab8ffa5f3355838c79edd90de451782 YYCache: 282f35b5b0990833d0b7a06d864a87141a021da2 YYImage: 362f44ffba1fc498f2152060f3bb54ed84600981 + YYModel: 2a7fdd96aaa4b86a824e26d0c517de8928c04b30 YYWebImage: 0431ed108e104527669b86f75efd58958425cca2 -PODFILE CHECKSUM: db78270e2bbf27b53486a73376f9be8ccd0a4c45 +PODFILE CHECKSUM: ef8190ce087dfbc44283358766c0c2e56afd8bdf COCOAPODS: 1.0.1 -- cgit v1.2.3