From 1871f3d5298efb978be825b03fd9c20ff4c204a8 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 21 Mar 2019 01:23:22 +0300 Subject: APIClient fetchImageWithURL --- Juick/APIClient.h | 2 ++ Juick/APIClient.m | 13 +++++++++++++ Juick/Views/MessageCell.m | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'Juick') diff --git a/Juick/APIClient.h b/Juick/APIClient.h index 078b91f..0f4c9d1 100644 --- a/Juick/APIClient.h +++ b/Juick/APIClient.h @@ -24,6 +24,8 @@ -(void) authenticate:(void(^)(BOOL))callback; -(void) refreshDeviceRegistration:(DeviceRegistration *)registrationData callback:(void(^)(BOOL))callback; -(void) getUserByName:(NSString *) name callback:(void(^)(User *))callback; +-(BOOL) isAuthenticated; +-(void) fetchImageWithURL:(NSURL *) url callback:(void(^)(NSData *))callback; +(NSString *) messagesUrl; +(NSString *) threadUrl; diff --git a/Juick/APIClient.m b/Juick/APIClient.m index d3bd248..fdc4005 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -12,6 +12,7 @@ @interface APIClient() @property(nonatomic, readwrite) AFHTTPSessionManager *manager; @property(nonatomic, strong) NSOperationQueue *backgroundQueue; +@property(nonatomic, strong) NSURLSession *urlSession; @end @implementation APIClient @@ -36,6 +37,7 @@ self.dateFormatter = [[NSDateFormatter alloc] init]; self.dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; [self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; + _urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; } return self; } @@ -203,6 +205,17 @@ } +- (void)fetchImageWithURL:(NSURL *)url callback:(void (^)(NSData *))callback { + NSURLSessionDataTask *dataTask = [self.urlSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(data); + }]; + }]; + [self.backgroundQueue addOperationWithBlock:^{ + [dataTask resume]; + }]; +} + +(NSString *) messagesUrl { return @"messages"; } diff --git a/Juick/Views/MessageCell.m b/Juick/Views/MessageCell.m index fb6e831..55e6054 100644 --- a/Juick/Views/MessageCell.m +++ b/Juick/Views/MessageCell.m @@ -32,15 +32,15 @@ self.message = msg; self.avatar.image = nil; __weak UIImageView *weakAvatar = self.avatar; - [self.avatar setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:msg.user.avatar]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { + [[APIClient sharedClient] fetchImageWithURL:[NSURL URLWithString:msg.user.avatar] callback:^(NSData *data) { [UIView transitionWithView:weakAvatar duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ - weakAvatar.image = image; + weakAvatar.image = [UIImage imageWithData:data]; } completion:nil]; - } failure:nil]; + }]; if ([msg.attach length] > 0) { CGFloat imageHeight = [msg.attachment.small.height floatValue] / [[UIScreen mainScreen] scale]; self.attachmentHeight.constant = imageHeight; -- cgit v1.2.3