Skip to content

Commit

Permalink
Merge pull request #129 from shyambhat/dev
Browse files Browse the repository at this point in the history
Pagination bug fix and Login/Logout toggle in Example project.
  • Loading branch information
shyambhat committed Jul 13, 2015
2 parents 3ea6e9d + 48b462b commit abfd268
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
</collectionView>
<navigationItem key="navigationItem" id="OeR-Ef-F2w">
<nil key="title"/>
<barButtonItem key="leftBarButtonItem" title="Login" id="9wq-oD-noP" userLabel="Login">
<barButtonItem key="leftBarButtonItem" title="Log in" id="9wq-oD-noP" userLabel="Login">
<connections>
<segue destination="Yqa-wS-RqO" kind="modal" identifier="segue.login" id="e5j-qO-vqy"/>
<action selector="loginTapped:" destination="4al-wR-gxx" id="6xZ-Bf-YC3"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" enabled="NO" title="More" id="3Nd-Tj-wLc">
Expand Down Expand Up @@ -188,7 +188,7 @@
<!--Navigation Controller-->
<scene sceneID="9CP-FP-i2A">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="Yqa-wS-RqO" sceneMemberID="viewController">
<navigationController storyboardIdentifier="LoginNavigationViewController" automaticallyAdjustsScrollViewInsets="NO" id="Yqa-wS-RqO" sceneMemberID="viewController">
<toolbarItems/>
<navigationItem key="navigationItem" id="xgb-Qe-nBg"/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="ER9-XT-p8R">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "InstagramMedia.h"
#import "IKMediaViewController.h"
#import "Constants.h"
#import "IKLoginViewController.h"

#define kNumberOfCellsInARow 3
#define kFetchItemsCount 15
Expand All @@ -47,24 +48,51 @@ - (void)viewDidLoad

self.mediaArray = [[NSMutableArray alloc] init];
self.instagramEngine = [InstagramEngine sharedEngine];
[self setTitle:@"Popular Media"];
[self updateCollectionViewLayout];
[self requestPopularMedia];

[self loadMedia];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userAuthenticated:)
name:kInstagramUserAuthenticatedNotification
object:nil];
}


/**
* Depending on whether the Instagram session is authenticated,
* this method loads either the publicly accessible popular media
* or the authenticated user's feed.
*/
- (void)loadMedia
{
self.currentPaginationInfo = nil;
BOOL isSessionValid = [self.instagramEngine isSessionValid];
if (isSessionValid) {
[self setTitle:@"My Feed"];
[self.navigationItem.leftBarButtonItem setTitle:@"Log out"];
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self.mediaArray removeAllObjects];
[self.collectionView reloadData];
[self requestSelfFeed];
}
else
{
[self setTitle:@"Popular Media"];
[self.navigationItem.leftBarButtonItem setTitle:@"Log in"];
[self.navigationItem.rightBarButtonItem setEnabled:NO];
[self.mediaArray removeAllObjects];
[self.collectionView reloadData];
[self requestPopularMedia];
}
}


#pragma mark - API Requests -

/**
- requestPopularMedia
Calls InstagramKit's Helper method to fetch Popular Instagram Media.
*/

- (void)requestPopularMedia
{
[self.instagramEngine getPopularMediaWithSuccess:^(NSArray *media, InstagramPaginationInfo *paginationInfo)
Expand All @@ -84,7 +112,6 @@ - (void)requestPopularMedia
@discussion The self.currentPaginationInfo object is updated on each successful call
and it's updated nextMaxId is passed as a parameter to the next paginated request.
*/

- (void)requestSelfFeed
{
[self.instagramEngine getSelfFeedWithCount:kFetchItemsCount
Expand All @@ -110,12 +137,43 @@ - (void)requestSelfFeed
Invoked when user taps the 'More' navigation item.
@discussion The requestSelfFeed method is called with updated pagination parameters (nextMaxId).
*/

- (IBAction)moreTapped:(id)sender {
[self requestSelfFeed];
}


/**
- loginTapped:
Invoked when user taps the left navigation item.
@discussion Either directs to the Login ViewController or logs out.
*/
- (IBAction)loginTapped:(id)sender
{
if (![self.instagramEngine isSessionValid]) {
UINavigationController *loginNavigationViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginNavigationViewController"];
[self presentViewController:loginNavigationViewController animated:YES completion:nil];
}
else
{
[self.instagramEngine logout];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged out" message:@"The user is now logged out." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];

[self loadMedia];
}
}


#pragma mark - User Authenticated Notification -


- (void)userAuthenticated:(NSNotification *)notification
{
[self loadMedia];
}


#pragma mark - UIStoryboardSegue -


Expand Down Expand Up @@ -159,21 +217,4 @@ - (void)updateCollectionViewLayout
layout.itemSize = CGSizeMake(size, size);
}


#pragma mark - User Authenticated Notification -


- (void)userAuthenticated:(NSNotification *)notification
{
[self setTitle:@"My Feed"];
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self.mediaArray removeAllObjects];
[self.collectionView reloadData];

[self requestSelfFeed];

}


@end
6 changes: 3 additions & 3 deletions InstagramKit/Models/InstagramPaginationInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ - (instancetype)initWithInfo:(NSDictionary *)info andObjectType:(Class)type
BOOL nextCursorExists = IKNotNull(info[kNextCursor]);
if (nextMaxIdExists)
{
_nextMaxId = kNextMaxId;
_nextMaxId = [[NSString alloc] initWithString:info[kNextMaxId]];
}
else if (nextMaxLikeIdExists)
{
_nextMaxId = kNextMaxLikeId;
_nextMaxId = [[NSString alloc] initWithString:info[kNextMaxLikeId]];
}
else if (nextCursorExists)
{
_nextMaxId = kNextCursor;
_nextMaxId = [[NSString alloc] initWithString:info[kNextCursor]];
}

if (type) {
Expand Down

0 comments on commit abfd268

Please sign in to comment.