Created
March 27, 2015 08:12
-
-
Save VincentSit/66e7261779a8d24d2795 to your computer and use it in GitHub Desktop.
VSMedia.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// VSMedia.m | |
// rugu | |
// | |
// Created by Vincent Sit on 15/1/11. | |
// Copyright (c) 2015年 Vincent Sit. All rights reserved. | |
// | |
#import "VSMedia.h" | |
// Models | |
#import "VSEngine.h" | |
#import "VSUser.h" | |
#import "VSPhoto.h" | |
#import "VSVideo.h" | |
#import "VSCaption.h" | |
/// 获取当前最流行的 media 列表. | |
static NSString * const kPopularURI = @"media/popular"; | |
@implementation VSMedia | |
@dynamic type; | |
@dynamic latitude; | |
@dynamic longitude; | |
@dynamic mediaID; | |
@dynamic tags; | |
@dynamic feedUsers; | |
@dynamic photo; | |
@dynamic video; | |
@dynamic caption; | |
@dynamic user; | |
@synthesize coordinate = _coordinate; | |
#pragma mark - Override | |
#pragma mark - MagicalRecord | |
+ (NSString *)MR_entityName { | |
return @"Media"; | |
} | |
#pragma mark - Motis | |
+ (NSDictionary *)mts_mapping { | |
return @{@"created_time": mts_key(createdAt), | |
// @"type": mts_key(type), | |
@"id": mts_key(mediaID), | |
@"location.latitude": mts_key(latitude), | |
@"location.longitude": mts_key(longitude), | |
@"tags": mts_key(tags), | |
@"images": mts_key(photo), | |
@"videos": mts_key(video), | |
@"caption": mts_key(caption), | |
@"user": mts_key(user) | |
}; | |
} | |
+ (NSDictionary*)mts_arrayClassMapping { | |
return @{mts_key(photo): [VSPhoto class], | |
mts_key(video): [VSVideo class], | |
mts_key(caption): [VSCaption class], | |
mts_key(user): [VSUser class]}; | |
} | |
- (void)mappingWithAttributes:(NSDictionary *)attributes { | |
[super mappingWithAttributes:attributes]; | |
NSString *typeString = attributes[@"type"]; | |
if ([typeString isEqualToString:@"video"]) { | |
self.type = VSMediaTypeVideo; | |
} | |
else if ([typeString isEqualToString:@"image"]) { | |
self.type = VSMediaTypeImage; | |
} | |
else { | |
self.type = VSMediaTypeUnknown; | |
} | |
} | |
#pragma mark - Private | |
#pragma mark Custom Accessors | |
- (CLLocationCoordinate2D)coordinate { | |
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(self.latitude, self.longitude); | |
if (CLLocationCoordinate2DIsValid(coord)) { | |
return coord; | |
} | |
else { | |
return kCLLocationCoordinate2DInvalid; | |
} | |
} | |
#pragma mark - Public | |
+ (NSURLSessionDataTask *)loadPopularWithCompletionHandler:(VSArrayBlock)completionHandler { | |
NSDictionary *parameters = nil; | |
if ([VSUser currentUser].accessToken) { | |
parameters = @{@"access_token": [VSUser currentUser].accessToken}; | |
} | |
else { | |
parameters = @{@"client_id": RuguInstagramClientID}; | |
} | |
return [[VSEngine sharedEngine] GET:kPopularURI parameters:parameters | |
success:^(NSURLSessionDataTask *task, id responseObject) { | |
if ([responseObject vs_responseValid]) { | |
NSMutableArray *returnedArray = [NSMutableArray array]; | |
NSArray *array = responseObject[@"data"]; | |
for (NSDictionary *dic in array) { | |
NSString *mediaID = dic[@"id"]; | |
VSMedia *media = [VSMedia MR_findFirstByAttribute:NSStringFromSelector(@selector(mediaID)) withValue:mediaID]; | |
if (!media) { | |
media = [VSMedia MR_createEntity]; | |
} | |
[media mappingWithAttributes:dic]; | |
[returnedArray addObject:media]; | |
} | |
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait]; | |
!completionHandler ?: completionHandler(YES, [returnedArray copy], nil); | |
} | |
else { | |
!completionHandler ?: completionHandler(NO, nil, [VSError errorWithResponseObject:responseObject]); | |
} | |
} failure:^(NSURLSessionDataTask *task, NSError *error) { | |
!completionHandler ?: completionHandler(NO, nil, [VSError errorWithResponseError:error]); | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment