Skip to content

Commit 1e8c5a2

Browse files
author
fellou89
committed
Merge pull request #36 from cloudspace/67874900_sort_feed_items
67874900 sort feed items
2 parents a77f97b + 31f5fd3 commit 1e8c5a2

8 files changed

Lines changed: 73 additions & 19 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: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@interface CSHomeViewController (){
2020
CSFeedItemCollectionViewDataSource *feedCollectionViewDataSource;
2121
FeedItem *currentFeedItem;
22+
NSString *currentURL;
2223
}
2324

2425
/// The collection view which holds the individual feed items
@@ -80,8 +81,11 @@ - (void) setupFeedItemObserver
8081
}
8182

8283
//redraw the collection with the changes to the feed items
84+
[feedCollectionViewDataSource sortFeedItems];
8385
[_collectionView_feedItems reloadData];
84-
86+
if(currentFeedItem){
87+
[self scrollToCurrentFeedItem];
88+
}
8589
}
8690
insertionBlock:nil
8791
removalBlock:nil
@@ -98,8 +102,9 @@ - (void) setupFeedItemObserver
98102

99103
- (void)setUpCollectionView
100104
{
101-
NSArray *feedItems = [FeedItem MR_findAll];
102-
105+
User *current = [User current];
106+
NSSet *feedItems = current.feedItems;
107+
103108
feedCollectionViewDataSource =
104109
[[CSFeedItemCollectionViewDataSource alloc] initWithFeedItems:feedItems
105110
reusableCellIdentifier:@"feedItemCell"
@@ -160,23 +165,31 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)sender {
160165
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
161166
// If we are scrolling in the collectionView only
162167
if([sender isMemberOfClass:[CSFeedItemCollectionView class]]) {
163-
164168
// unload the webView if we have moved to a new feedItem
165169
if(currentFeedItem != self.collectionView_feedItems.currentFeedItem){
170+
currentFeedItem = self.collectionView_feedItems.currentFeedItem;
166171
[self.feedItemWebView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
167172
}
168173
}
169174
}
170175

176+
// Scroll to the currentFeedItem when the feedItems update
177+
- (void)scrollToCurrentFeedItem
178+
{
179+
NSUInteger index = [feedCollectionViewDataSource.sortedFeedItems indexOfObject:currentFeedItem];
180+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
181+
[_collectionView_feedItems scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
182+
}
183+
171184
-(void)loadFeedItemWebView
172185
{
173186
// Check if this is a new url
174-
if(currentFeedItem != self.collectionView_feedItems.currentFeedItem){
187+
if(currentURL != self.collectionView_feedItems.currentFeedItem.url){
175188
// update the current url
176-
currentFeedItem = self.collectionView_feedItems.currentFeedItem;
189+
currentURL = self.collectionView_feedItems.currentFeedItem.url;
177190

178191
// load the url in the webView
179-
NSURL *url = [NSURL URLWithString:self.collectionView_feedItems.currentFeedItem.url];
192+
NSURL *url = [NSURL URLWithString:currentURL];
180193
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
181194
[self.feedItemWebView loadRequest:requestObj];
182195
}

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: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,37 @@ @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
45+
- (void)sortFeedItems
46+
{
47+
NSArray *sortableArray = [NSArray arrayWithArray:[self.feedItems allObjects]];
48+
49+
NSSortDescriptor *sortDescriptor;
50+
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt"
51+
ascending:NO];
52+
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
53+
self.sortedFeedItems = [sortableArray sortedArrayUsingDescriptors:sortDescriptors];
54+
}
55+
4156
/**
4257
* Determines the number of sections in the collection view (in this case it's always 1)
4358
*
@@ -57,7 +72,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
5772
*/
5873
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
5974
{
60-
return [_feedItems count];
75+
return [_sortedFeedItems count];
6176
}
6277

6378
/**
@@ -71,7 +86,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
7186
CSFeedItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_reusableCellIdentifier
7287
forIndexPath:indexPath];
7388

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

7691
_configureFeedItemCell(cell, item);
7792

EasyReader/Application/Models/User.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
/// The users feeds
2626
@property (nonatomic, retain) NSSet *feeds;
2727

28+
/// The items in a users feeds
29+
@property (nonatomic, readonly) NSSet *feedItems;
30+
2831

2932
#pragma mark - Methods
3033

EasyReader/Application/Models/User.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
@implementation User
1414

1515
@dynamic feeds;
16+
@dynamic feedItems;
17+
18+
/**
19+
* Gathers all the items in a users feeds
20+
*/
21+
- (NSSet *)feedItems
22+
{
23+
NSMutableSet *feedItems = [[NSMutableSet alloc] init];
24+
25+
for (Feed *feed in self.feeds)
26+
{
27+
[feedItems setByAddingObjectsFromSet:feed.feedItems];
28+
}
29+
30+
return feedItems;
31+
}
1632

1733
/**
1834
* Returns the current user.

EasyReader/Application/Views/Main_iPhone.storyboard

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
1818
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1919
<subviews>
20-
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" bounces="NO" alwaysBounceVertical="YES" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="EWA-9Z-pb1">
20+
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" misplaced="YES" bounces="NO" alwaysBounceVertical="YES" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="EWA-9Z-pb1">
2121
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
2222
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
2323
<subviews>
@@ -141,6 +141,10 @@
141141
</constraints>
142142
</pageControl>
143143
</subviews>
144+
<constraints>
145+
<constraint firstAttribute="trailing" secondItem="vdW-Yb-aAb" secondAttribute="trailing" id="Bcm-5I-kx3"/>
146+
<constraint firstItem="vdW-Yb-aAb" firstAttribute="leading" secondItem="EWA-9Z-pb1" secondAttribute="leading" id="ct5-rK-kEl"/>
147+
</constraints>
144148
</scrollView>
145149
</subviews>
146150
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>

EasyReader/Resources/CoreData/EasyReader.xcdatamodeld/api_v2.xcdatamodel/contents

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="12F45" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
2+
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5063" systemVersion="13C64" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
33
<entity name="Feed" representedClassName="Feed" syncable="YES">
44
<attribute name="icon" optional="YES" attributeType="String" syncable="YES"/>
55
<attribute name="id" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
@@ -23,8 +23,8 @@
2323
<relationship name="feeds" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Feed" inverseName="user" inverseEntity="Feed" syncable="YES"/>
2424
</entity>
2525
<elements>
26-
<element name="Feed" positionX="0" positionY="0" width="0" height="0"/>
27-
<element name="FeedItem" positionX="0" positionY="0" width="0" height="0"/>
28-
<element name="User" positionX="0" positionY="0" width="0" height="0"/>
26+
<element name="Feed" positionX="0" positionY="0" width="128" height="135"/>
27+
<element name="FeedItem" positionX="0" positionY="0" width="128" height="178"/>
28+
<element name="User" positionX="0" positionY="0" width="128" height="58"/>
2929
</elements>
3030
</model>

0 commit comments

Comments
 (0)