From d856b364dea82f7b89e2040075ee32d988707edb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Dec 2017 00:18:52 +0300 Subject: refactor --- Juick/APIClient.m | 28 +++++++++++++++++--------- Juick/Supporting Files/Juick-Info.plist | 2 +- Juick/ViewControllers/MessagesViewController.m | 4 ++-- Juick/ViewControllers/ThreadViewController.m | 1 + 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Juick/APIClient.m b/Juick/APIClient.m index bcc4eec..abefa40 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -9,6 +9,10 @@ #import "PDKeychainBindings.h" #import "Message.h" +@interface APIClient() +@property(nonatomic, strong) NSOperationQueue *backgroundQueue; +@end + @implementation APIClient +(APIClient *) sharedClient { @@ -24,38 +28,38 @@ if (self = [super init]) { self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.juick.com"]]; self.manager.requestSerializer = [AFJSONRequestSerializer new]; + self.backgroundQueue = [NSOperationQueue new]; } return self; } -(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback { AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [self.backgroundQueue addOperationWithBlock:^{ [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]]; }]; - dispatch_async(dispatch_get_main_queue(), ^{ + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(messages, nil); - }); + }]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; if (statusCode == 401) { - dispatch_async(dispatch_get_main_queue(), ^{ + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]); - }); + }]; } }]; - }); + }]; } -(void) postReplyToThread:(NSNumber *)mid inReplyTo:(NSNumber *)rid text:(NSString *)text result:(void(^)(Message *, NSError *))callback { AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - NSOperationQueue *operationQueue = [NSOperationQueue new]; - [operationQueue addOperationWithBlock:^{ + [self.backgroundQueue addOperationWithBlock:^{ [manager POST:@"/comment" parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) { [formData appendPartWithFormData:[[mid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"mid"]; [formData appendPartWithFormData:[[rid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"rid"]; @@ -63,10 +67,14 @@ } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Success!"); Message *reply = [Message yy_modelWithJSON:responseObject]; - callback(reply, nil); + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(reply, nil); + }]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Error: %@", [error localizedDescription]); - callback(nil, error); + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(nil, error); + }]; }]; }]; } diff --git a/Juick/Supporting Files/Juick-Info.plist b/Juick/Supporting Files/Juick-Info.plist index f25b2bc..9a0b1eb 100644 --- a/Juick/Supporting Files/Juick-Info.plist +++ b/Juick/Supporting Files/Juick-Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.0.26 + 1.0.27 ITSAppUsesNonExemptEncryption LSApplicationCategoryType diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index 71f4271..80e8c73 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -29,8 +29,8 @@ NSArray *newMsgs = next; NSUInteger oldCount = [self.messages count]; if ([self.path isEqualToString:[APIClient threadUrl]]) { - NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue]; - NSUInteger count = [next count] - 1; + NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1; + NSUInteger count = [next count]; if (oldCount > 0) { if (lastRid && lastRid < count) { newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)]; diff --git a/Juick/ViewControllers/ThreadViewController.m b/Juick/ViewControllers/ThreadViewController.m index 95c9839..06cbf9a 100644 --- a/Juick/ViewControllers/ThreadViewController.m +++ b/Juick/ViewControllers/ThreadViewController.m @@ -61,6 +61,7 @@ [[APIClient sharedClient] postReplyToThread:msg.mid inReplyTo:self.replyTo text:inputView.textContent.text result:^(Message *msg, NSError *err) { MessageInputView *inputView = (MessageInputView *)self.inputAccessoryView; inputView.quoteText.text = @""; + inputView.textContent.text = @""; [inputView.textContent endEditing:YES]; [self refreshData]; }]; -- cgit v1.2.3