diff --git a/EasyReader/Application/Controllers/Home/CSHomeViewController.h b/EasyReader/Application/Controllers/Home/CSHomeViewController.h index f87736e..fb3243a 100644 --- a/EasyReader/Application/Controllers/Home/CSHomeViewController.h +++ b/EasyReader/Application/Controllers/Home/CSHomeViewController.h @@ -16,7 +16,7 @@ @property (weak, nonatomic) IBOutlet UIScrollView *verticalScrollView; @property (strong, nonatomic) IBOutlet UIButton *button_leftMenu; @property (nonatomic, strong) UIWebView *feedItemWebView; -@property (nonatomic, strong) NSMutableArray *feedItems; +@property (nonatomic, strong) NSSet *feedItems; @property User* currentUser; @end diff --git a/EasyReader/Application/Controllers/Home/CSHomeViewController.m b/EasyReader/Application/Controllers/Home/CSHomeViewController.m index c8a888f..d495948 100644 --- a/EasyReader/Application/Controllers/Home/CSHomeViewController.m +++ b/EasyReader/Application/Controllers/Home/CSHomeViewController.m @@ -19,6 +19,7 @@ @interface CSHomeViewController (){ CSFeedItemCollectionViewDataSource *feedCollectionViewDataSource; FeedItem *currentFeedItem; + NSString *currentURL; } /// The collection view which holds the individual feed items @@ -80,8 +81,11 @@ - (void) setupFeedItemObserver } //redraw the collection with the changes to the feed items + [feedCollectionViewDataSource sortFeedItems]; [_collectionView_feedItems reloadData]; - + if(currentFeedItem){ + [self scrollToCurrentFeedItem]; + } } insertionBlock:nil removalBlock:nil @@ -98,8 +102,9 @@ - (void) setupFeedItemObserver - (void)setUpCollectionView { - NSArray *feedItems = [FeedItem MR_findAll]; - + User *current = [User current]; + NSSet *feedItems = current.feedItems; + feedCollectionViewDataSource = [[CSFeedItemCollectionViewDataSource alloc] initWithFeedItems:feedItems reusableCellIdentifier:@"feedItemCell" @@ -160,23 +165,31 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)sender { - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender { // If we are scrolling in the collectionView only if([sender isMemberOfClass:[CSFeedItemCollectionView class]]) { - // unload the webView if we have moved to a new feedItem if(currentFeedItem != self.collectionView_feedItems.currentFeedItem){ + currentFeedItem = self.collectionView_feedItems.currentFeedItem; [self.feedItemWebView loadHTMLString:@"" baseURL:nil]; } } } +// Scroll to the currentFeedItem when the feedItems update +- (void)scrollToCurrentFeedItem +{ + NSUInteger index = [feedCollectionViewDataSource.sortedFeedItems indexOfObject:currentFeedItem]; + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; + [_collectionView_feedItems scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; +} + -(void)loadFeedItemWebView { // Check if this is a new url - if(currentFeedItem != self.collectionView_feedItems.currentFeedItem){ + if(currentURL != self.collectionView_feedItems.currentFeedItem.url){ // update the current url - currentFeedItem = self.collectionView_feedItems.currentFeedItem; + currentURL = self.collectionView_feedItems.currentFeedItem.url; // load the url in the webView - NSURL *url = [NSURL URLWithString:self.collectionView_feedItems.currentFeedItem.url]; + NSURL *url = [NSURL URLWithString:currentURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [self.feedItemWebView loadRequest:requestObj]; } diff --git a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.h b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.h index 88f9b4c..e5c07e6 100644 --- a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.h +++ b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.h @@ -23,11 +23,14 @@ typedef void (^configureFeedItemCell)(CSFeedItemCell *, FeedItem *); * @param The identifier to use to dequeue reusable cells for the collection view * @param configureFeedItemCell A block which will configure the cell based on the given FeedItem */ -- (id)initWithFeedItems:(NSArray *)feedItems +- (id)initWithFeedItems:(NSSet *)feedItems reusableCellIdentifier:(NSString *)reusableCellIdentifier configureBlock:(configureFeedItemCell)configureFeedItemCell; +- (void)sortFeedItems; + /// The FeedItems for this data source @property (nonatomic, strong) NSMutableSet *feedItems; +@property (nonatomic, strong) NSArray *sortedFeedItems; @end diff --git a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m index dfd946e..8cf35b0 100644 --- a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m +++ b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m @@ -22,7 +22,7 @@ @implementation CSFeedItemCollectionViewDataSource /** * Sets each instance variable to the values in the given parameters */ -- (id)initWithFeedItems:(NSArray *)feedItems +- (id)initWithFeedItems:(NSSet *)feedItems reusableCellIdentifier:(NSString *)reusableCellIdentifier configureBlock:(void (^)(CSFeedItemCell *, FeedItem *))configureFeedItemCell { @@ -30,14 +30,29 @@ - (id)initWithFeedItems:(NSArray *)feedItems if (self) { - _feedItems = [NSMutableSet setWithArray:feedItems]; + _feedItems = [NSMutableSet setWithSet:feedItems]; _reusableCellIdentifier = reusableCellIdentifier; _configureFeedItemCell = configureFeedItemCell; + + _sortedFeedItems = [[NSArray alloc] init]; + [self sortFeedItems]; } - + return self; } +// Sort feedItems by updatedAt +- (void)sortFeedItems +{ + NSArray *sortableArray = [NSArray arrayWithArray:[self.feedItems allObjects]]; + + NSSortDescriptor *sortDescriptor; + sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt" + ascending:NO]; + NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; + self.sortedFeedItems = [sortableArray sortedArrayUsingDescriptors:sortDescriptors]; +} + /** * Determines the number of sections in the collection view (in this case it's always 1) * @@ -57,7 +72,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - return [_feedItems count]; + return [_sortedFeedItems count]; } /** @@ -71,7 +86,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell CSFeedItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_reusableCellIdentifier forIndexPath:indexPath]; - FeedItem *item = [_feedItems allObjects][indexPath.row]; + FeedItem *item = [_sortedFeedItems objectAtIndex:indexPath.row]; _configureFeedItemCell(cell, item); diff --git a/EasyReader/Application/Models/User.h b/EasyReader/Application/Models/User.h index 274abd7..4d3ab43 100644 --- a/EasyReader/Application/Models/User.h +++ b/EasyReader/Application/Models/User.h @@ -25,6 +25,9 @@ /// The users feeds @property (nonatomic, retain) NSSet *feeds; +/// The items in a users feeds +@property (nonatomic, readonly) NSSet *feedItems; + #pragma mark - Methods diff --git a/EasyReader/Application/Models/User.m b/EasyReader/Application/Models/User.m index 37b9755..c558cb9 100644 --- a/EasyReader/Application/Models/User.m +++ b/EasyReader/Application/Models/User.m @@ -13,6 +13,22 @@ @implementation User @dynamic feeds; +@dynamic feedItems; + +/** + * Gathers all the items in a users feeds + */ +- (NSSet *)feedItems +{ + NSMutableSet *feedItems = [[NSMutableSet alloc] init]; + + for (Feed *feed in self.feeds) + { + [feedItems setByAddingObjectsFromSet:feed.feedItems]; + } + + return feedItems; +} /** * Returns the current user. diff --git a/EasyReader/Application/Views/Main_iPhone.storyboard b/EasyReader/Application/Views/Main_iPhone.storyboard index 5fd6b7e..dc3f81e 100644 --- a/EasyReader/Application/Views/Main_iPhone.storyboard +++ b/EasyReader/Application/Views/Main_iPhone.storyboard @@ -17,7 +17,7 @@ - + @@ -141,6 +141,10 @@ + + + + diff --git a/EasyReader/Resources/CoreData/EasyReader.xcdatamodeld/api_v2.xcdatamodel/contents b/EasyReader/Resources/CoreData/EasyReader.xcdatamodeld/api_v2.xcdatamodel/contents index cc481cc..01f0b99 100644 --- a/EasyReader/Resources/CoreData/EasyReader.xcdatamodeld/api_v2.xcdatamodel/contents +++ b/EasyReader/Resources/CoreData/EasyReader.xcdatamodeld/api_v2.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -23,8 +23,8 @@ - - - + + + \ No newline at end of file