Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 20 additions & 7 deletions EasyReader/Application/Controllers/Home/CSHomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@interface CSHomeViewController (){
CSFeedItemCollectionViewDataSource *feedCollectionViewDataSource;
FeedItem *currentFeedItem;
NSString *currentURL;
}

/// The collection view which holds the individual feed items
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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:@"<html><head></head><body></body></html>" 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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,37 @@ @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
{
self = [super init];

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)
*
Expand All @@ -57,7 +72,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_feedItems count];
return [_sortedFeedItems count];
}

/**
Expand All @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions EasyReader/Application/Models/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions EasyReader/Application/Models/User.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion EasyReader/Application/Views/Main_iPhone.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" bounces="NO" alwaysBounceVertical="YES" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="EWA-9Z-pb1">
<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">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
Expand Down Expand Up @@ -141,6 +141,10 @@
</constraints>
</pageControl>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="vdW-Yb-aAb" secondAttribute="trailing" id="Bcm-5I-kx3"/>
<constraint firstItem="vdW-Yb-aAb" firstAttribute="leading" secondItem="EWA-9Z-pb1" secondAttribute="leading" id="ct5-rK-kEl"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="12F45" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5063" systemVersion="13C64" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="Feed" representedClassName="Feed" syncable="YES">
<attribute name="icon" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="id" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
Expand All @@ -23,8 +23,8 @@
<relationship name="feeds" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Feed" inverseName="user" inverseEntity="Feed" syncable="YES"/>
</entity>
<elements>
<element name="Feed" positionX="0" positionY="0" width="0" height="0"/>
<element name="FeedItem" positionX="0" positionY="0" width="0" height="0"/>
<element name="User" positionX="0" positionY="0" width="0" height="0"/>
<element name="Feed" positionX="0" positionY="0" width="128" height="135"/>
<element name="FeedItem" positionX="0" positionY="0" width="128" height="178"/>
<element name="User" positionX="0" positionY="0" width="128" height="58"/>
</elements>
</model>