From a293a8e049fea49b8c2b8262fb92a29f73d66989 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 2 Dec 2018 23:23:31 +0300 Subject: Drop YYModel --- Juick/APIClient.m | 10 +++++----- Juick/AppDelegate.m | 2 +- Juick/Model/Attachment.h | 2 ++ Juick/Model/Attachment.m | 17 +++++++++++++++++ Juick/Model/Chat.h | 3 +++ Juick/Model/Chat.m | 11 +++++++++++ Juick/Model/DeviceRegistration.h | 8 ++++++++ Juick/Model/Entity.h | 2 ++ Juick/Model/Entity.m | 10 ++++++++++ Juick/Model/Message.h | 2 ++ Juick/Model/Message.m | 24 +++++++++++++++++++----- Juick/Model/User.h | 2 ++ Juick/Model/User.m | 10 +++++++++- Juick/Supporting Files/Juick-Prefix.pch | 1 - Juick/Views/MessageCell.m | 20 ++++++++++---------- 15 files changed, 101 insertions(+), 23 deletions(-) (limited to 'Juick') diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 6d9878d..6f69e9d 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -46,7 +46,7 @@ [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]]; + [messages addObject:[Message fromJSON:obj]]; }]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(messages, nil); @@ -71,7 +71,7 @@ [formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Success!"); - Message *result = [Message yy_modelWithJSON:responseObject]; + Message *result = [Message fromJSON:responseObject]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(result, nil); }]; @@ -93,7 +93,7 @@ [formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Success!"); - Message *reply = [Message yy_modelWithJSON:responseObject]; + Message *reply = [Message fromJSON:responseObject]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(reply, nil); }]; @@ -131,7 +131,7 @@ NSMutableArray *groups = [NSMutableArray new]; NSArray *pms = [(NSDictionary *)responseObject objectForKey:@"pms"]; [pms enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [groups addObject:[Chat yy_modelWithJSON:obj]]; + [groups addObject:[Chat fromJSON:obj]]; }]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(groups, nil); @@ -152,7 +152,7 @@ NSMutableArray *messages = [NSMutableArray new]; NSArray *messagesList = (NSArray *)responseObject; [messagesList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [messages addObject:[Message yy_modelWithJSON:obj]]; + [messages addObject:[Message fromJSON:obj]]; }]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(messages, nil); diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 950f473..629c6ca 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -65,7 +65,7 @@ registration.type = @"apns"; registration.token = token; AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - [manager PUT:@"/notifications" parameters:[@[registration] yy_modelToJSONObject] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [manager PUT:@"/notifications" parameters:@[[registration toJSON]] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"success %@", token); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"fail %@", [error localizedDescription]); diff --git a/Juick/Model/Attachment.h b/Juick/Model/Attachment.h index 26fc1c0..47c6c3a 100644 --- a/Juick/Model/Attachment.h +++ b/Juick/Model/Attachment.h @@ -15,4 +15,6 @@ @property(nonatomic, strong) Attachment *small; @property(nonatomic, strong) Attachment *medium; @property(nonatomic, strong) Attachment *thumbnail; + ++(Attachment *) fromJSON:(NSDictionary *)jsonData; @end diff --git a/Juick/Model/Attachment.m b/Juick/Model/Attachment.m index a54e462..006b72e 100644 --- a/Juick/Model/Attachment.m +++ b/Juick/Model/Attachment.m @@ -10,4 +10,21 @@ @implementation Attachment ++(Attachment *) fromJSON:(NSDictionary *)jsonData { + Attachment *attachment = [Attachment new]; + attachment.url = jsonData[@"url"]; + attachment.width = jsonData[@"width"]; + attachment.height = jsonData[@"height"]; + if (jsonData[@"small"]) { + attachment.small = [Attachment fromJSON:jsonData[@"small"]]; + } + if (jsonData[@"medium"]) { + attachment.medium = [Attachment fromJSON:jsonData[@"medium"]]; + } + if (jsonData[@"thumbnail"]) { + attachment.thumbnail = [Attachment fromJSON:jsonData[@"thumbnail"]]; + } + return attachment; +} + @end diff --git a/Juick/Model/Chat.h b/Juick/Model/Chat.h index 1e80bdf..4b7ec30 100644 --- a/Juick/Model/Chat.h +++ b/Juick/Model/Chat.h @@ -17,4 +17,7 @@ @property NSNumber *unreadCount; @property NSDate *lastMessageTimestamp; @property NSString *lastMessageText; + ++ (Chat *) fromJSON:(NSDictionary *)jsonData; + @end diff --git a/Juick/Model/Chat.m b/Juick/Model/Chat.m index ff5fb83..66ed394 100644 --- a/Juick/Model/Chat.m +++ b/Juick/Model/Chat.m @@ -10,4 +10,15 @@ @implementation Chat ++(Chat *) fromJSON:(NSDictionary *)jsonData { + Chat *user = [Chat new]; + user.uid = jsonData[@"uid"]; + user.uname = jsonData[@"uname"]; + user.avatar = jsonData[@"avatar"]; + user.unreadCount = jsonData[@"unreadCount"]; + user.lastMessageTimestamp = jsonData[@"lastMessageTimestamp"]; + user.lastMessageText = jsonData[@"lastMessageText"]; + return user; +} + @end diff --git a/Juick/Model/DeviceRegistration.h b/Juick/Model/DeviceRegistration.h index e629637..d5b620c 100644 --- a/Juick/Model/DeviceRegistration.h +++ b/Juick/Model/DeviceRegistration.h @@ -11,7 +11,15 @@ @interface DeviceRegistration : NSObject @property NSString *type; @property NSString *token; + +-(NSDictionary *) toJSON; @end @implementation DeviceRegistration +-(NSDictionary *) toJSON { + return @{ + @"type": self.type, + @"token": self.token + }; +} @end diff --git a/Juick/Model/Entity.h b/Juick/Model/Entity.h index 13142f1..4cdffc0 100644 --- a/Juick/Model/Entity.h +++ b/Juick/Model/Entity.h @@ -16,4 +16,6 @@ @property NSNumber *start; @property NSNumber *end; ++(Entity *) fromJSON:(NSDictionary *)jsonData; + @end diff --git a/Juick/Model/Entity.m b/Juick/Model/Entity.m index ac53a1f..cf23a2a 100644 --- a/Juick/Model/Entity.m +++ b/Juick/Model/Entity.m @@ -10,4 +10,14 @@ @implementation Entity ++(Entity *) fromJSON:(NSDictionary *)jsonData { + Entity *entity = [Entity new]; + entity.start = jsonData[@"start"]; + entity.end = jsonData[@"end"]; + entity.type = jsonData[@"type"]; + entity.text = jsonData[@"text"]; + entity.link = jsonData[@"url"]; + return entity; +} + @end diff --git a/Juick/Model/Message.h b/Juick/Model/Message.h index 8e1b01a..f822fdf 100644 --- a/Juick/Model/Message.h +++ b/Juick/Model/Message.h @@ -26,4 +26,6 @@ @property Attachment *attachment; @property BOOL service; ++(Message *) fromJSON:(NSDictionary *)jsonData; + @end diff --git a/Juick/Model/Message.m b/Juick/Model/Message.m index d00f705..68cc7c9 100644 --- a/Juick/Model/Message.m +++ b/Juick/Model/Message.m @@ -11,10 +11,24 @@ @implementation Message -+ (NSDictionary *)modelCustomPropertyMapper { - return @{@"text" : @"body", - @"attach" : @"photo.small", - @"repliesCount": @"replies", - @"repliesBy": @"repliesby"}; ++ (Message *) fromJSON:(NSDictionary *)jsonData { + Message * message = [Message new]; + message.mid = jsonData[@"mid"]; + message.rid = jsonData[@"rid"]; + message.text = jsonData[@"body"]; + message.attach = jsonData[@"photo"][@"small"]; + message.repliesCount = jsonData[@"replies"]; + message.repliesBy = jsonData[@"repliesby"]; + message.user = [User fromJSON:jsonData[@"user"]]; + message.timestamp = jsonData[@"timestamp"]; + message.service = jsonData[@"service"]; + message.tags = jsonData[@"tags"]; + NSMutableArray *entitiesArray = [NSMutableArray new]; + for (NSDictionary *entityData in jsonData[@"entities"]) { + [entitiesArray addObject:[Entity fromJSON:entityData]]; + } + message.entities = [entitiesArray copy]; + message.attachment = [Attachment fromJSON:jsonData[@"attachment"]]; + return message; } @end diff --git a/Juick/Model/User.h b/Juick/Model/User.h index 8120dec..59b010d 100644 --- a/Juick/Model/User.h +++ b/Juick/Model/User.h @@ -13,6 +13,8 @@ @property (nonatomic, strong) NSString *uid; @property (nonatomic, strong) NSString *avatar; ++ (User *) fromJSON:(NSDictionary *)jsonData; + + (void) get:(NSString *)name callback:(void(^)(User *))callback; + (BOOL) isAuthenticated; diff --git a/Juick/Model/User.m b/Juick/Model/User.m index 36a9b4c..a2f874c 100644 --- a/Juick/Model/User.m +++ b/Juick/Model/User.m @@ -11,6 +11,14 @@ @implementation User ++ (User *) fromJSON:(NSDictionary *)jsonData { + User *user = [User new]; + user.uid = jsonData[@"uid"]; + user.uname = jsonData[@"uname"]; + user.avatar = jsonData[@"avatar"]; + return user; +} + +(BOOL) isAuthenticated { return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil; } @@ -46,7 +54,7 @@ AFHTTPSessionManager *manager = [APIClient sharedClient].manager; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; [manager GET:@"users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { - callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]); + callback([User fromJSON:[(NSArray *)responseObject firstObject]]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { callback(nil); }]; diff --git a/Juick/Supporting Files/Juick-Prefix.pch b/Juick/Supporting Files/Juick-Prefix.pch index 4340a61..a4b3d0f 100644 --- a/Juick/Supporting Files/Juick-Prefix.pch +++ b/Juick/Supporting Files/Juick-Prefix.pch @@ -21,7 +21,6 @@ #import "UIImage+Utils.h" #import #import - #import #import #import #import diff --git a/Juick/Views/MessageCell.m b/Juick/Views/MessageCell.m index faf3492..2686adf 100644 --- a/Juick/Views/MessageCell.m +++ b/Juick/Views/MessageCell.m @@ -77,16 +77,16 @@ initWithString:msg.text attributes:@{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]}]; [txt beginEditing]; - for (NSDictionary *entity in msg.entities) { - NSUInteger start = [entity[@"start"] unsignedIntegerValue]; - NSUInteger end = [entity[@"end"] unsignedIntegerValue]; - NSString *text = entity[@"text"] ? entity[@"text"] : @""; + for (Entity *entity in msg.entities) { + NSUInteger start = entity.start ? [entity.start unsignedIntegerValue] : 0; + NSUInteger end = entity.end ? [entity.end unsignedIntegerValue] : 0; + NSString *text = entity.text ? entity.text : @""; NSRange currentRange = NSMakeRange(start, end - start); [txt addAttribute:@"displayText" value:text range:currentRange]; - if ([entity[@"type"] isEqualToString:@"a"]) { - [txt addAttribute:NSLinkAttributeName value:entity[@"url"] range:currentRange]; + if ([entity.type isEqualToString:@"a"]) { + [txt addAttribute:NSLinkAttributeName value:entity.link range:currentRange]; } - if ([entity[@"type"] isEqualToString:@"q"]) { + if ([entity.type isEqualToString:@"q"]) { [txt addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:currentRange]; NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.firstLineHeadIndent = 12.0f; @@ -94,18 +94,18 @@ paragraphStyle.paragraphSpacing = 12.0f; [txt addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:currentRange]; } - if ([entity[@"type"] isEqualToString:@"u"]) { + if ([entity.type isEqualToString:@"u"]) { [txt addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:currentRange]; } UIFontDescriptor* fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody]; - if ([entity[@"type"] isEqualToString:@"b"]) { + if ([entity.type isEqualToString:@"b"]) { UIFontDescriptor* boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]; UIFont* boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]; [txt addAttribute:NSFontAttributeName value:boldFont range:currentRange]; } - if ([entity[@"type"] isEqualToString:@"i"]) { + if ([entity.type isEqualToString:@"i"]) { UIFontDescriptor* italicFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic]; UIFont* italicFont = [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0]; -- cgit v1.2.3