Skip to content

Commit 4964008

Browse files
author
Michael Beattie
committed
[#67874900] sorted feedItems by date and auto scroll to currentFeedItem
1 parent cfd044b commit 4964008

4 files changed

Lines changed: 30 additions & 17 deletions

File tree

EasyReader/Application/Controllers/Home/CSHomeViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@property (weak, nonatomic) IBOutlet UIScrollView *verticalScrollView;
1717
@property (strong, nonatomic) IBOutlet UIButton *button_leftMenu;
1818
@property (nonatomic, strong) UIWebView *feedItemWebView;
19-
@property (nonatomic, strong) NSMutableArray *feedItems;
19+
@property (nonatomic, strong) NSSet *feedItems;
2020
@property User* currentUser;
2121

2222
@end

EasyReader/Application/Controllers/Home/CSHomeViewController.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ - (void) setupFeedItemObserver
8080
}
8181

8282
//redraw the collection with the changes to the feed items
83+
[feedCollectionViewDataSource sortFeedItems];
8384
[_collectionView_feedItems reloadData];
84-
85+
if(currentFeedItem){
86+
[self scrollToCurrentFeedItem];
87+
}
8588
}
8689
insertionBlock:nil
8790
removalBlock:nil
@@ -98,13 +101,9 @@ - (void) setupFeedItemObserver
98101

99102
- (void)setUpCollectionView
100103
{
101-
// User *current = [User current];
102-
//
103-
// NSSet *feedItems = current.feedItems;
104+
User *current = [User current];
105+
NSSet *feedItems = current.feedItems;
104106

105-
NSArray *feedItems = [FeedItem MR_findAll];
106-
FeedItem *first = feedItems[0];
107-
NSLog(@"%@", first.name);
108107
feedCollectionViewDataSource =
109108
[[CSFeedItemCollectionViewDataSource alloc] initWithFeedItems:feedItems
110109
reusableCellIdentifier:@"feedItemCell"
@@ -173,6 +172,14 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
173172
}
174173
}
175174

175+
// Scroll to the currentFeedItem when the feedItems update
176+
- (void)scrollToCurrentFeedItem
177+
{
178+
NSUInteger index = [feedCollectionViewDataSource.sortedFeedItems indexOfObject:currentFeedItem];
179+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
180+
[_collectionView_feedItems scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
181+
}
182+
176183
-(void)loadFeedItemWebView
177184
{
178185
// Check if this is a new url

EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ typedef void (^configureFeedItemCell)(CSFeedItemCell *, FeedItem *);
2323
* @param The identifier to use to dequeue reusable cells for the collection view
2424
* @param configureFeedItemCell A block which will configure the cell based on the given FeedItem
2525
*/
26-
- (id)initWithFeedItems:(NSArray *)feedItems
26+
- (id)initWithFeedItems:(NSSet *)feedItems
2727
reusableCellIdentifier:(NSString *)reusableCellIdentifier
2828
configureBlock:(configureFeedItemCell)configureFeedItemCell;
2929

30+
- (void)sortFeedItems;
31+
3032
/// The FeedItems for this data source
3133
@property (nonatomic, strong) NSMutableSet *feedItems;
34+
@property (nonatomic, strong) NSArray *sortedFeedItems;
3235

3336
@end

EasyReader/Application/Controllers/Home/FeedItemCollectionView/CSFeedItemCollectionViewDataSource.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,35 @@ @implementation CSFeedItemCollectionViewDataSource
2222
/**
2323
* Sets each instance variable to the values in the given parameters
2424
*/
25-
- (id)initWithFeedItems:(NSArray *)feedItems
25+
- (id)initWithFeedItems:(NSSet *)feedItems
2626
reusableCellIdentifier:(NSString *)reusableCellIdentifier
2727
configureBlock:(void (^)(CSFeedItemCell *, FeedItem *))configureFeedItemCell
2828
{
2929
self = [super init];
3030

3131
if (self)
3232
{
33-
_feedItems = [NSMutableSet setWithArray:feedItems];
33+
_feedItems = [NSMutableSet setWithSet:feedItems];
3434
_reusableCellIdentifier = reusableCellIdentifier;
3535
_configureFeedItemCell = configureFeedItemCell;
36+
37+
_sortedFeedItems = [[NSArray alloc] init];
38+
[self sortFeedItems];
3639
}
37-
40+
3841
return self;
3942
}
4043

44+
// Sort feedItems by updatedAt
4145
- (void)sortFeedItems
4246
{
43-
NSMutableArray *sortableArray = [NSMutableArray arrayWithArray:[self.feedItems allObjects]];
47+
NSArray *sortableArray = [NSArray arrayWithArray:[self.feedItems allObjects]];
4448

4549
NSSortDescriptor *sortDescriptor;
4650
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt"
4751
ascending:NO];
4852
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
49-
NSArray *sortedArray;
50-
sortedArray = [sortableArray sortedArrayUsingDescriptors:sortDescriptors];
53+
self.sortedFeedItems = [sortableArray sortedArrayUsingDescriptors:sortDescriptors];
5154
}
5255

5356
/**
@@ -69,7 +72,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
6972
*/
7073
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
7174
{
72-
return [_feedItems count];
75+
return [_sortedFeedItems count];
7376
}
7477

7578
/**
@@ -83,7 +86,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
8386
CSFeedItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_reusableCellIdentifier
8487
forIndexPath:indexPath];
8588

86-
FeedItem *item = [_feedItems allObjects][indexPath.row];
89+
FeedItem *item = [_sortedFeedItems objectAtIndex:indexPath.row];
8790

8891
_configureFeedItemCell(cell, item);
8992

0 commit comments

Comments
 (0)