Created
February 27, 2014 14:16
-
-
Save abbood/9250867 to your computer and use it in GitHub Desktop.
EpisodesDataSource.m
This file contains hidden or 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
// | |
// EpisodesDataSource.m | |
// TNN | |
// | |
// Created by Ahmad Mehio on 2/26/14. | |
// Copyright (c) 2014 larochesoft. All rights reserved. | |
// | |
#import <RestKit/RestKit.h> | |
#import <QuartzCore/QuartzCore.h> | |
#import "RKManagedObjectStore.h" | |
#import "TNNRestClient.h" | |
#import "Model.h" | |
#import "NSURL+Addons.h" | |
#import "TableCellSnippetLabel.h" | |
#import "TNNCell.h" | |
#import "UIImage+Addons.h" | |
#import "EpisodesDataSource.h" | |
#import "NSArray+Addons.h" | |
@implementation EpisodesDataSource | |
- (id)initWithProgramId:(NSNumber *)programId { | |
self = [self init]; | |
self.programId = programId; | |
self.parameters = @{@"pid": programId}; | |
self.predicateStr = @"episode"; | |
self.predicateFilter = [NSPredicate predicateWithFormat:@"episode_program_id == %@", [NSString stringWithFormat:@"%@", programId]]; | |
return self; | |
} | |
- (id)init { | |
self = [super init]; | |
self.entityName = @"Episodes"; | |
self.resourcePath = kEpisodesPath; | |
self.sortKey = @"episode_title"; | |
self.keyPath = @"EpisodesList.Episode"; | |
return self; | |
} | |
- (void)addResponseDescriptor { | |
Model *model = [Model getSingleton]; | |
/* | |
NSArray *existingResponseDescriptors = [model.objectManager.responseDescriptors mapObjectsUsingBlock:^(RKResponseDescriptor *descriptor, NSUInteger idx){ | |
return [descriptor keyPath]; | |
}]; | |
if ([existingResponseDescriptors containsObject:self.keyPath]) { | |
NSLog(@"response descriptor %@ already exsits! return", self.keyPath); | |
return; | |
} | |
*/ | |
NSLog(@"adding response descriptor %@", self.keyPath); | |
RKEntityMapping *episodesEntityMapping = [RKEntityMapping mappingForEntityForName:self.entityName inManagedObjectStore:model.managedObjectStore]; | |
// left is http, right is dbase | |
[episodesEntityMapping addAttributeMappingsFromDictionary:@{@"EpisodeId.text": @"episode_id", | |
@"EpisodeProgramId.text": @"episode_program_id", | |
@"EpisodeTitle.text": @"episode_title", | |
@"EpisodeImageUrl.text": @"episode_image_url", | |
@"EpisodeNewsUrl.text": @"episode_news_url", | |
@"EpisodeVideoUrl.text": @"episode_video_url", | |
@"EpisodeVideoId.text": @"episode_video_id", | |
@"EpisodeCaption.text": @"episode_caption", | |
@"EpisodeDetails.text": @"episode_details", | |
}]; | |
episodesEntityMapping.identificationAttributes = @[ @"episode_id" ]; | |
RKResponseDescriptor *episodesResponseDescriptor = | |
[RKResponseDescriptor responseDescriptorWithMapping:episodesEntityMapping | |
method:RKRequestMethodAny | |
pathPattern:self.resourcePath | |
keyPath:self.keyPath | |
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[model.objectManager addResponseDescriptor:episodesResponseDescriptor]; | |
} | |
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath | |
{ | |
NSLog(@"episodesdatasource [%p] configuring cell at indexPath [%d,%d]", self, indexPath.section, indexPath.row); | |
TNNCell *episodeCell = (TNNCell *)cell; | |
NSManagedObject *object=nil; | |
@try{ | |
object = [self.fetchedResultsController objectAtIndexPath:indexPath]; | |
} @catch (NSException*e) { | |
NSLog(@"EXCEPTION %@",e); | |
} | |
NSString *imageUrl = [(NSString *)[[object valueForKey:@"episode_image_url"] description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
if(!imageUrl){ | |
[episodeCell.previewImageView removeFromSuperview]; | |
NSLog(@"removed"); | |
}else{ | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl]]; | |
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; | |
__weak TNNCell *weakCell = episodeCell; | |
[episodeCell.previewImageView setImageWithURLRequest:request placeholderImage:self.placeHolderImg success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { | |
[weakCell.previewImageView setImage:image]; | |
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { | |
// do nothing | |
}]; | |
} | |
episodeCell.titleLabel.text = [[object valueForKey:@"episode_title"] description]; | |
// snippet | |
NSString *contentText =[[object valueForKey:@"episode_caption"] description]; | |
[episodeCell.newsSnippetLabel setText:contentText]; | |
episodeCell.index = [[object valueForKey:@"episode_id"] intValue]; | |
NSLog(@"this is episode program id %@", [[object valueForKey:@"episode_program_id"] description]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment