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#import < AFNetworking/UIImageView+AFNetworking.h>
1920
2021
21- @interface CSHomeViewController (){
22+ #import " CSFeedItemCell.h"
23+
24+ @interface CSHomeViewController ()
25+ {
2226 NSString *currentURL;
2327}
24-
25- // / The collection view which holds the individual feed items
26- @property (nonatomic , weak ) IBOutlet CSFeedItemCollectionView *collectionView_feedItems;
27-
2828@end
2929
30-
3130@implementation CSHomeViewController
3231
32+ /* *
33+ * Set up data sctructures, controller views, add observers
34+ */
3335- (void )viewDidLoad
3436{
3537 [super viewDidLoad ];
3638 _feedItems = [[NSMutableSet alloc ] init ];
39+
3740 [_pageControl_itemIndicator setUpFadesOnView: [_pageControl_itemIndicator superview ]];
3841 _pageControl_itemIndicator.controller_owner = self;
3942
40- self.currentUser = [User current ];
43+ self.currentUser = [User current ];
4144
45+ [self setUpVerticalScrollView ];
4246 [self setUpCollectionView ];
4347 [self setUpWebView ];
4448 [self setupFeedItemObserver ];
45- // Do any additional setup after loading the view.
46- }
47-
48- - (void )viewDidLayoutSubviews
49- {
50- [self setUpVerticalScrollView ];
5149}
5250
51+ /* *
52+ * Assigns observers for feeds and feed items, puts page controller at start
53+ */
5354- (void ) setupFeedItemObserver
5455{
5556 [self observeRelationship: @keypath (self .currentUser.feeds)
@@ -112,11 +113,32 @@ - (void) setupFeedItemObserver
112113 removalBlock: nil
113114 replacementBlock: nil
114115 ];
115-
116- NSLog (@" observer added" );
117116}
118117
119- // Sets up collection view on controller start up
118+ #pragma mark - IBActions
119+
120+ // Receives left menu link click
121+ - (IBAction )buttonLeftMenu_touchUpInside_goToMenu : (id )sender {
122+ [[self rootViewController ] toggleLeftSideMenuCompletion: ^{}];
123+ }
124+
125+
126+ /* *
127+ * Sets up vertical scroll view on controller start up
128+ */
129+ -(void )setUpVerticalScrollView {
130+ // Set contentSize to be twice the height of the scrollview
131+ NSInteger width = self.verticalScrollView .frame .size .width ;
132+ NSInteger height = self.verticalScrollView .frame .size .height ;
133+ self.verticalScrollView .contentSize = CGSizeMake (width, height*2 );
134+
135+ self.verticalScrollView .pagingEnabled =YES ;
136+ self.verticalScrollView .delegate = self;
137+ }
138+
139+ /* *
140+ * Sets up collection view on controller start up
141+ */
120142- (void )setUpCollectionView
121143{
122144// NSArray *feedItems = [FeedItem MR_findAll];
@@ -129,18 +151,26 @@ - (void)setUpCollectionView
129151
130152 self.collectionView_feedItems .dataSource = _feedCollectionViewDataSource;
131153 self.collectionView_feedItems .delegate = self;
132-
133-
134154}
135155
136- #pragma mark - IBActions
137-
138- // Receives left menu link click
139- - (IBAction )buttonLeftMenu_touchUpInside_goToMenu : (id )sender {
140- [[self rootViewController ] toggleLeftSideMenuCompletion: ^{}];
156+ /* *
157+ * Sets up collection view on controller start up
158+ */
159+ -(void )setUpWebView
160+ {
161+ // Create a new webview and place it below the collectionView
162+ self.feedItemWebView = [[UIWebView alloc ] init ];
163+ NSInteger width = self.verticalScrollView .frame .size .width ;
164+ NSInteger height = self.verticalScrollView .frame .size .height ;
165+ self.feedItemWebView .frame = CGRectMake (0 , height, width, height*2 );
166+
167+ // Add it to the bottom of the scrollView
168+ [self .verticalScrollView addSubview: self .feedItemWebView];
141169}
142170
143-
171+ /* *
172+ * Sets up collection cell to given feed item data
173+ */
144174- (configureFeedItemCell)configureFeedItem
145175{
146176 return ^void (CSFeedItemCell *cell, FeedItem *feedItem) {
@@ -153,35 +183,49 @@ - (configureFeedItemCell)configureFeedItem
153183 };
154184}
155185
156- -(void )setUpVerticalScrollView {
157- // Set contentSize to be twice the height of the scrollview
158- NSInteger width = self.verticalScrollView .frame .size .width ;
159- NSInteger height = self.verticalScrollView .frame .size .height ;
160- self.verticalScrollView .contentSize = CGSizeMake (width, height*2 );
186+ - (void )loadFeedItemWebView
187+ {
188+ // Check if this is a new url
189+ if (currentURL != self.collectionView_feedItems .currentFeedItem .url ){
190+ // update the current url
191+ currentURL = self.collectionView_feedItems .currentFeedItem .url ;
161192
162- self.verticalScrollView .pagingEnabled =YES ;
163- self.verticalScrollView .delegate = self;
193+ // load the url in the webView
194+ NSURL *url = [NSURL URLWithString: currentURL];
195+ NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
196+ [self .feedItemWebView loadRequest: requestObj];
197+ }
164198}
165199
166- -(void )setUpWebView
200+ # pragma mark - ScrollView methods
201+
202+ /* *
203+ * Scroll to the currentFeedItem when the feedItems update
204+ */
205+ - (void )scrollToCurrentFeedItem
167206{
168- // Create a new webview and place it below the collectionView
169- self.feedItemWebView = [[UIWebView alloc ] init ];
170- NSInteger width = self.verticalScrollView .frame .size .width ;
171- NSInteger height = self.verticalScrollView .frame .size .height ;
172- self.feedItemWebView .frame = CGRectMake (0 , height, width, height*2 );
173-
174- // Add it to the bottom of the scrollView
175- [self .verticalScrollView addSubview: self .feedItemWebView];
207+ NSUInteger index = [_feedCollectionViewDataSource.sortedFeedItems indexOfObject: _currentFeedItem];
208+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow: index inSection: 0 ];
209+ [_collectionView_feedItems scrollToItemAtIndexPath: indexPath atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated: NO ];
176210}
177211
212+ # pragma mark - ScrollView Delegate methods
213+
214+ /* *
215+ * Scroll view delegate method for dragging view up into webview
216+ */
178217- (void )scrollViewWillBeginDragging : (UIScrollView *)sender {
179218 // If we are scrolling in the scrollView only not a subclass
180219 if ([sender isMemberOfClass: [UIScrollView class ]]) {
181220 [self loadFeedItemWebView ];
182221 }
183222}
184223
224+ # pragma mark - CollectionView Delegate methods
225+
226+ /* *
227+ * Collection view delegate method for updating current feed item webview url
228+ */
185229- (void )scrollViewDidEndDecelerating : (UIScrollView *)sender {
186230 // If we are scrolling in the collectionView only
187231 if ([sender isMemberOfClass: [CSFeedItemCollectionView class ]]) {
@@ -193,15 +237,10 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
193237 }
194238}
195239
196- // Scroll to the currentFeedItem when the feedItems update
197- - (void )scrollToCurrentFeedItem
198- {
199- NSUInteger index = [_feedCollectionViewDataSource.sortedFeedItems indexOfObject: _currentFeedItem];
200- NSIndexPath *indexPath = [NSIndexPath indexPathForRow: index inSection: 0 ];
201- [_collectionView_feedItems scrollToItemAtIndexPath: indexPath atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated: NO ];
202- }
203-
204-
240+ /* *
241+ * Collection view delegate method for when a cell ends display
242+ * Sets page control indicator
243+ */
205244- (void )collectionView : (UICollectionView *)collectionView didEndDisplayingCell : (UICollectionViewCell *)cell forItemAtIndexPath : (NSIndexPath *)indexPath
206245{
207246 NSInteger newIndex = indexPath.row +((indexPath.row -self.collectionCellGoingTo )*-1 );
@@ -210,24 +249,4 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
210249 forCollection: _feedItems];
211250}
212251
213- - (void )loadFeedItemWebView
214- {
215- // Check if this is a new url
216- if (currentURL != self.collectionView_feedItems .currentFeedItem .url ){
217- // update the current url
218- currentURL = self.collectionView_feedItems .currentFeedItem .url ;
219-
220- // load the url in the webView
221- NSURL *url = [NSURL URLWithString: currentURL];
222- NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
223- [self .feedItemWebView loadRequest: requestObj];
224- }
225- }
226-
227- - (void )didReceiveMemoryWarning
228- {
229- [super didReceiveMemoryWarning ];
230- // Dispose of any resources that can be recreated.
231- }
232-
233252@end
0 commit comments