Skip to content

Instantly share code, notes, and snippets.

@amannayak0007
Last active August 29, 2015 14:12
Show Gist options
  • Save amannayak0007/3c72d6ee08c69cf4bca3 to your computer and use it in GitHub Desktop.
Save amannayak0007/3c72d6ee08c69cf4bca3 to your computer and use it in GitHub Desktop.
#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "NSDate+NVTimeAgo.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface CollectionViewController ()
{
NSUInteger currentIndex;
MWFeedInfo *feedInfo;
}
@end
@implementation CollectionViewController
@synthesize itemsToDisplay;
- (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);
feedInfo = info;
}
- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
NSLog(@"Parsed Feed Item: “%@”", item.title);
if (item) [parsedItems addObject:@{@"feed":feedInfo, @"item":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] objectForKey:@"item"];
// Configure the cell
cell.titleLabel.text = [item title];
cell.pubDateLabel.text = [item.date formattedAsTimeAgo];
MWFeedInfo *info = [[itemsToDisplay objectAtIndex:indexPath.row] objectForKey:@"feed"];
cell.siteLabel.text = [info title];
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