From a80a5142ca7524bade20bce9c3ddfccf5de92ef6 Mon Sep 17 00:00:00 2001 From: Michael Beattie Date: Wed, 19 Mar 2014 17:03:55 -0400 Subject: [PATCH 1/4] [#67874900] began creating sort method --- .../Controllers/Home/CSHomeViewController.m | 2 +- .../CSFeedItemCollectionViewDataSource.m | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/EasyReader/Application/Controllers/Home/CSHomeViewController.m b/EasyReader/Application/Controllers/Home/CSHomeViewController.m index c8a888f..9694825 100644 --- a/EasyReader/Application/Controllers/Home/CSHomeViewController.m +++ b/EasyReader/Application/Controllers/Home/CSHomeViewController.m @@ -160,9 +160,9 @@ - (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]; } } diff --git a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m index dfd946e..c528955 100644 --- a/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m +++ b/EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m @@ -38,6 +38,18 @@ - (id)initWithFeedItems:(NSArray *)feedItems return self; } +- (void)sortFeedItems +{ + NSMutableArray *sortableArray = [NSMutableArray arrayWithArray:[self.feedItems allObjects]]; + + NSSortDescriptor *sortDescriptor; + sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt" + ascending:NO]; + NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; + NSArray *sortedArray; + sortedArray = [sortableArray sortedArrayUsingDescriptors:sortDescriptors]; +} + /** * Determines the number of sections in the collection view (in this case it's always 1) * From cfd044b7d42f6b82638194ca5c104026bf2f4ebc Mon Sep 17 00:00:00 2001 From: Joey Lorich Date: Wed, 19 Mar 2014 17:06:29 -0400 Subject: [PATCH 2/4] Add feedItems to user --- .../Controllers/Home/CSHomeViewController.m | 9 +++++++-- EasyReader/Application/Models/User.h | 3 +++ EasyReader/Application/Models/User.m | 16 ++++++++++++++++ .../Application/Views/Main_iPhone.storyboard | 6 +++++- .../api_v2.xcdatamodel/contents | 8 ++++---- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/EasyReader/Application/Controllers/Home/CSHomeViewController.m b/EasyReader/Application/Controllers/Home/CSHomeViewController.m index 9694825..c3c8793 100644 --- a/EasyReader/Application/Controllers/Home/CSHomeViewController.m +++ b/EasyReader/Application/Controllers/Home/CSHomeViewController.m @@ -98,8 +98,13 @@ - (void) setupFeedItemObserver - (void)setUpCollectionView { - NSArray *feedItems = [FeedItem MR_findAll]; - +// User *current = [User current]; +// +// NSSet *feedItems = current.feedItems; + + NSArray *feedItems = [FeedItem MR_findAll]; + FeedItem *first = feedItems[0]; + NSLog(@"%@", first.name); feedCollectionViewDataSource = [[CSFeedItemCollectionViewDataSource alloc] initWithFeedItems:feedItems reusableCellIdentifier:@"feedItemCell" 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 From 4964008b90cfce920014ff62750fafd8dfe100b0 Mon Sep 17 00:00:00 2001 From: Michael Beattie Date: Wed, 19 Mar 2014 17:46:40 -0400 Subject: [PATCH 3/4] [#67874900] sorted feedItems by date and auto scroll to currentFeedItem --- .../Controllers/Home/CSHomeViewController.h | 2 +- .../Controllers/Home/CSHomeViewController.m | 21 ++++++++++++------- .../CSFeedItemCollectionViewDataSource.h | 5 ++++- .../CSFeedItemCollectionViewDataSource.m | 19 ++++++++++------- 4 files changed, 30 insertions(+), 17 deletions(-) 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 c3c8793..b7d84ea 100644 --- a/EasyReader/Application/Controllers/Home/CSHomeViewController.m +++ b/EasyReader/Application/Controllers/Home/CSHomeViewController.m @@ -80,8 +80,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,13 +101,9 @@ - (void) setupFeedItemObserver - (void)setUpCollectionView { -// User *current = [User current]; -// -// NSSet *feedItems = current.feedItems; + User *current = [User current]; + NSSet *feedItems = current.feedItems; - NSArray *feedItems = [FeedItem MR_findAll]; - FeedItem *first = feedItems[0]; - NSLog(@"%@", first.name); feedCollectionViewDataSource = [[CSFeedItemCollectionViewDataSource alloc] initWithFeedItems:feedItems reusableCellIdentifier:@"feedItemCell" @@ -173,6 +172,14 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender { } } +// 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 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 c528955..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,24 +30,27 @@ - (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 { - NSMutableArray *sortableArray = [NSMutableArray arrayWithArray:[self.feedItems allObjects]]; + NSArray *sortableArray = [NSArray arrayWithArray:[self.feedItems allObjects]]; NSSortDescriptor *sortDescriptor; sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt" ascending:NO]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; - NSArray *sortedArray; - sortedArray = [sortableArray sortedArrayUsingDescriptors:sortDescriptors]; + self.sortedFeedItems = [sortableArray sortedArrayUsingDescriptors:sortDescriptors]; } /** @@ -69,7 +72,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - return [_feedItems count]; + return [_sortedFeedItems count]; } /** @@ -83,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); From 31f5fd33ad743ee016f8986d723c3eb1c386fb86 Mon Sep 17 00:00:00 2001 From: Michael Beattie Date: Wed, 19 Mar 2014 17:57:11 -0400 Subject: [PATCH 4/4] [#67874900] tracked currentURL --- .../Application/Controllers/Home/CSHomeViewController.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/EasyReader/Application/Controllers/Home/CSHomeViewController.m b/EasyReader/Application/Controllers/Home/CSHomeViewController.m index b7d84ea..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 @@ -183,12 +184,12 @@ - (void)scrollToCurrentFeedItem -(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]; }