77//
88
99#import " CSHomeViewController.h"
10+
11+ // Pods
1012#import < Block-KVO/MTKObserving.h>
11- #import " CSFeedItemCollectionView.h"
12- #import " CSFeedItemCollectionViewDataSource.h"
13- #import " FeedItem.h"
1413#import " MFSideMenu.h"
15- #import " CSFeedItemCell.h"
14+
15+ // Models
16+ #import " FeedItem.h"
1617#import " Feed.h"
1718#import " User.h"
1819
19- @interface CSHomeViewController (){
20+ #import " CSFeedItemCell.h"
21+
22+ @interface CSHomeViewController ()
23+ {
2024 NSString *currentURL;
2125}
22-
23- // / The collection view which holds the individual feed items
24- @property (nonatomic , weak ) IBOutlet CSFeedItemCollectionView *collectionView_feedItems;
25-
2626@end
2727
28-
2928@implementation CSHomeViewController
3029
30+ /* *
31+ * Set up data sctructures, controller views, add observers
32+ */
3133- (void )viewDidLoad
3234{
3335 [super viewDidLoad ];
3436 _feedItems = [[NSMutableSet alloc ] init ];
37+
3538 [_pageControl_itemIndicator setUpFadesOnView: [_pageControl_itemIndicator superview ]];
3639 _pageControl_itemIndicator.controller_owner = self;
40+
41+ [self setUpVerticalScrollView ];
3742 [self setUpCollectionView ];
3843 [self setUpWebView ];
3944 [self setupFeedItemObserver ];
40- // Do any additional setup after loading the view.
41- }
42-
43- - (void )viewDidLayoutSubviews
44- {
45- [self setUpVerticalScrollView ];
4645}
4746
47+ /* *
48+ * Assigns observers for feeds and feed items, puts page controller at start
49+ */
4850- (void ) setupFeedItemObserver
4951{
5052 _currentUser = [User current ];
@@ -109,10 +111,32 @@ - (void) setupFeedItemObserver
109111 removalBlock: nil
110112 replacementBlock: nil
111113 ];
112- NSLog (@" observer added" );
113114}
114115
115- // Sets up collection view on controller start up
116+ #pragma mark - IBActions
117+
118+ // Receives left menu link click
119+ - (IBAction )buttonLeftMenu_touchUpInside_goToMenu : (id )sender {
120+ [[self rootViewController ] toggleLeftSideMenuCompletion: ^{}];
121+ }
122+
123+
124+ /* *
125+ * Sets up vertical scroll view on controller start up
126+ */
127+ -(void )setUpVerticalScrollView {
128+ // Set contentSize to be twice the height of the scrollview
129+ NSInteger width = self.verticalScrollView .frame .size .width ;
130+ NSInteger height = self.verticalScrollView .frame .size .height ;
131+ self.verticalScrollView .contentSize = CGSizeMake (width, height*2 );
132+
133+ self.verticalScrollView .pagingEnabled =YES ;
134+ self.verticalScrollView .delegate = self;
135+ }
136+
137+ /* *
138+ * Sets up collection view on controller start up
139+ */
116140- (void )setUpCollectionView
117141{
118142 User *current = [User current ];
@@ -125,18 +149,26 @@ - (void)setUpCollectionView
125149
126150 self.collectionView_feedItems .dataSource = _feedCollectionViewDataSource;
127151 self.collectionView_feedItems .delegate = self;
128-
129-
130152}
131153
132- #pragma mark - IBActions
133-
134- // Receives left menu link click
135- - (IBAction )buttonLeftMenu_touchUpInside_goToMenu : (id )sender {
136- [[self rootViewController ] toggleLeftSideMenuCompletion: ^{}];
154+ /* *
155+ * Sets up collection view on controller start up
156+ */
157+ -(void )setUpWebView
158+ {
159+ // Create a new webview and place it below the collectionView
160+ self.feedItemWebView = [[UIWebView alloc ] init ];
161+ NSInteger width = self.verticalScrollView .frame .size .width ;
162+ NSInteger height = self.verticalScrollView .frame .size .height ;
163+ self.feedItemWebView .frame = CGRectMake (0 , height, width, height*2 );
164+
165+ // Add it to the bottom of the scrollView
166+ [self .verticalScrollView addSubview: self .feedItemWebView];
137167}
138168
139-
169+ /* *
170+ * Sets up collection cell to given feed item data
171+ */
140172- (configureFeedItemCell)configureFeedItem
141173{
142174 return ^void (CSFeedItemCell *cell, FeedItem *feedItem) {
@@ -147,35 +179,49 @@ - (configureFeedItemCell)configureFeedItem
147179 };
148180}
149181
150- -(void )setUpVerticalScrollView {
151- // Set contentSize to be twice the height of the scrollview
152- NSInteger width = self.verticalScrollView .frame .size .width ;
153- NSInteger height = self.verticalScrollView .frame .size .height ;
154- self.verticalScrollView .contentSize = CGSizeMake (width, height*2 );
182+ - (void )loadFeedItemWebView
183+ {
184+ // Check if this is a new url
185+ if (currentURL != self.collectionView_feedItems .currentFeedItem .url ){
186+ // update the current url
187+ currentURL = self.collectionView_feedItems .currentFeedItem .url ;
155188
156- self.verticalScrollView .pagingEnabled =YES ;
157- self.verticalScrollView .delegate = self;
189+ // load the url in the webView
190+ NSURL *url = [NSURL URLWithString: currentURL];
191+ NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
192+ [self .feedItemWebView loadRequest: requestObj];
193+ }
158194}
159195
160- -(void )setUpWebView
196+ # pragma mark - ScrollView methods
197+
198+ /* *
199+ * Scroll to the currentFeedItem when the feedItems update
200+ */
201+ - (void )scrollToCurrentFeedItem
161202{
162- // Create a new webview and place it below the collectionView
163- self.feedItemWebView = [[UIWebView alloc ] init ];
164- NSInteger width = self.verticalScrollView .frame .size .width ;
165- NSInteger height = self.verticalScrollView .frame .size .height ;
166- self.feedItemWebView .frame = CGRectMake (0 , height, width, height*2 );
167-
168- // Add it to the bottom of the scrollView
169- [self .verticalScrollView addSubview: self .feedItemWebView];
203+ NSUInteger index = [_feedCollectionViewDataSource.sortedFeedItems indexOfObject: _currentFeedItem];
204+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow: index inSection: 0 ];
205+ [_collectionView_feedItems scrollToItemAtIndexPath: indexPath atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated: NO ];
170206}
171207
208+ # pragma mark - ScrollView Delegate methods
209+
210+ /* *
211+ * Scroll view delegate method for dragging view up into webview
212+ */
172213- (void )scrollViewWillBeginDragging : (UIScrollView *)sender {
173214 // If we are scrolling in the scrollView only not a subclass
174215 if ([sender isMemberOfClass: [UIScrollView class ]]) {
175216 [self loadFeedItemWebView ];
176217 }
177218}
178219
220+ # pragma mark - CollectionView Delegate methods
221+
222+ /* *
223+ * Collection view delegate method for updating current feed item webview url
224+ */
179225- (void )scrollViewDidEndDecelerating : (UIScrollView *)sender {
180226 // If we are scrolling in the collectionView only
181227 if ([sender isMemberOfClass: [CSFeedItemCollectionView class ]]) {
@@ -187,15 +233,10 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
187233 }
188234}
189235
190- // Scroll to the currentFeedItem when the feedItems update
191- - (void )scrollToCurrentFeedItem
192- {
193- NSUInteger index = [_feedCollectionViewDataSource.sortedFeedItems indexOfObject: _currentFeedItem];
194- NSIndexPath *indexPath = [NSIndexPath indexPathForRow: index inSection: 0 ];
195- [_collectionView_feedItems scrollToItemAtIndexPath: indexPath atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated: NO ];
196- }
197-
198-
236+ /* *
237+ * Collection view delegate method for when a cell ends display
238+ * Sets page control indicator
239+ */
199240- (void )collectionView : (UICollectionView *)collectionView didEndDisplayingCell : (UICollectionViewCell *)cell forItemAtIndexPath : (NSIndexPath *)indexPath
200241{
201242 NSInteger newIndex = indexPath.row +((indexPath.row -self.collectionCellGoingTo )*-1 );
@@ -204,24 +245,4 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
204245 forCollection: _feedItems];
205246}
206247
207- - (void )loadFeedItemWebView
208- {
209- // Check if this is a new url
210- if (currentURL != self.collectionView_feedItems .currentFeedItem .url ){
211- // update the current url
212- currentURL = self.collectionView_feedItems .currentFeedItem .url ;
213-
214- // load the url in the webView
215- NSURL *url = [NSURL URLWithString: currentURL];
216- NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
217- [self .feedItemWebView loadRequest: requestObj];
218- }
219- }
220-
221- - (void )didReceiveMemoryWarning
222- {
223- [super didReceiveMemoryWarning ];
224- // Dispose of any resources that can be recreated.
225- }
226-
227248@end
0 commit comments