Skip to content

Instantly share code, notes, and snippets.

@amannayak0007
Last active August 29, 2015 14:12
Show Gist options
  • Save amannayak0007/931d5446c1d31ce1886b to your computer and use it in GitHub Desktop.
Save amannayak0007/931d5446c1d31ce1886b to your computer and use it in GitHub Desktop.
#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "DetailViewController.h"
#import "NSDate+NVTimeAgo.h"
#import "UIImageView+AFNetworking.h"
@interface CollectionViewController ()
{
NSUInteger currentIndex;
NSMutableArray *parsedItems;
}
@end
@implementation CollectionViewController
@synthesize itemsToDisplay;
- (IBAction) addToFavorites:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *favoritefeeds;
if ([prefs objectForKey:@"favoritefeeds"] == nil) {
//create the array
NSMutableArray *array = [[NSMutableArray alloc] init];
[prefs setObject:array forKey:@"favoritefeeds"];
}
NSMutableArray *tempArray = [[prefs objectForKey:@"favoritefeeds"] mutableCopy];
favoritefeeds = tempArray;
//add the recipe
[favoritefeeds addObject:[parsedItems objectAtIndex:1]];
//save the array to NSUserDefaults
[prefs setObject:favoritefeeds forKey:@"favoritefeeds"];
}
- (void)viewDidLoad {
[super viewDidLoad];
parsedItems = [[NSMutableArray alloc] init];
self.itemsToDisplay = [NSArray array];
self.title = @"Feeds";
currentIndex = 0;
[self parseNextfeed];
}
- (void) parseNextfeed
{
NSURL *feedURL = [NSURL URLWithString:[_feedurl objectAtIndex:currentIndex++]];
feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
feedParser.delegate = self;
feedParser.feedParseType = ParseTypeFull;
feedParser.connectionType = ConnectionTypeAsynchronously;
[feedParser parse];
}
- (void)updateTableWithParsedItems {
self.itemsToDisplay = [parsedItems sortedArrayUsingDescriptors:
[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:NO]]];
self.collectionView.userInteractionEnabled = YES;
self.collectionView.alpha = 1;
[self.collectionView reloadData];
}
#pragma mark -
#pragma mark MWFeedParserDelegate
- (void)feedParserDidStart:(MWFeedParser *)parser {
NSLog(@"Started Parsing: %@", parser.url);
}
- (void)feedParser:(MWFeedParser *)parser didParseFeedInfo:(MWFeedInfo *)info {
NSLog(@"Parsed Feed Info: “%@”", info.title);
}
- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
NSLog(@"Parsed Feed Item: “%@”", item.title);
if (item) [parsedItems addObject:item];
}
- (void)feedParserDidFinish:(MWFeedParser *)parser {
NSLog(@"Finished Parsing%@", (parser.stopped ? @" (Stopped)" : @""));
[self updateTableWithParsedItems];
if (currentIndex < _feedurl.count) {
[self parseNextfeed];
}
}
- (void)feedParser:(MWFeedParser *)parser didFailWithError:(NSError *)error {
NSLog(@"Finished Parsing With Error: %@", error);
if (parsedItems.count == 0) {
// Show failed message in title
} else {
// Failed but some items parsed, so show and inform of error
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Parsing Incomplete"
message:@"There was an error during the parsing of this feed. Not all of the feed items could parsed."
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}
[self updateTableWithParsedItems];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [itemsToDisplay count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
// Configure the cell
cell.titleLabel.text = [item title];
cell.pubDateLabel.text = [item.date formattedAsTimeAgo];
if ([[item imagesFromContent] count]>0) {
[cell.image setImageWithURL:[NSURL URLWithString:[item.imagesFromContent objectAtIndex:0]]
placeholderImage:[UIImage imageNamed:@"placeholder.jpeg"]];
}
else
{
[cell.image setImageWithURL:[NSURL URLWithString:item.thumbnail] placeholderImage:[UIImage imageNamed:@"placeholder.jpeg"]];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
CollectionViewCell *cell = (CollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.item];
[[segue destinationViewController] setItem:item];
}}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment