Skip to content

Instantly share code, notes, and snippets.

@jstart
Created May 21, 2015 18:52
Show Gist options
  • Save jstart/035d7dcdc47166416512 to your computer and use it in GitHub Desktop.
Save jstart/035d7dcdc47166416512 to your computer and use it in GitHub Desktop.
Jack Flintermann's lovely mug
//
// SNGUserPhotosController.m
// Singles-eHarmony
//
// Created by Jack Flintermann
// Copyright (c) 2012 eHarmony. All rights reserved.
//
//
// 77$Z8DD88O8ZZOZ7II77II?????+++++++====================+=++++++?????7?I77O$$777II
// $7$O8D8O88OZ$$77IIIII????++++++++=======================++++++??????II77$$$77III
// $$O888OOOOZ$$77IIIII?????++++++++==========~~==============+++++?????I777$$777II
// 7ZO8OOOZZZZ777IIIIII???++++++===========~=~~~~~==~~~~~~=====+++++?????777$$$7777
// 7ZOOOOOZZZ$777IIIII???++++++===========~=~~~~~~~~~~~~~~=====+++++?????I777$$7777
// 7$OOOOOOZ$$777IIII????+++++================~~=~~~~~~~~====+=+++++??????I7$$O$I77
// 7$ZOOOOZ$$77777IIII???+++++=~=~===============~~~~~=======+++++++?????II7$$OZ777
// 7$$O8OZ$$77777IIII???+++?++==================~~~~~==========+++++?????77I77ZZ777
// 77$OOOZZ$77777IIII????????+================~=====~=========+++++++????I7$$$ZO77I
// $7$OOOOZ$77777IIIII??????+++==================~=============++++??????I7ZZZZO777
// $7$OOOZZ$$77777IIII??????++++====================~=~~~~====+++++?????III$OOOZ777
// 777O8OZZ$$$77777IIII???++++++==================~~~~~~~====+??????????III$ZOOZ777
// 777Z88ZZZZ$$777IIIIII??+++++=================~~~~~=~~~==+????+?????I?III7ZZZ$777
// 777Z8OZZOZZ$7777IIIIII???++===========~===========++?I7777I+==+++??IIIII7ZOZ$777
// 777$OOOOOOZ$77777IIII??I??+++====~============+++?I777$Z7?+++=++++?IIIII7ZOZ$777
// 7777ZOOOOOZ$77777IIII7I77777IIII??++==+==+++=+???II77$77IIIII???????IIII7ZOZ$$77
// 7I77$OOO8OZ$777777777??I$$$$$$ZZ$7$7???+++++++??I7I$7Z8DDOO8D88O$7I?IIIIZZZZ7777
// 7I77$O8OOOZ$777777777III???I77$$$$7II7?????++???I777ZOOZ$$$?=IOZ$$7$I?$ZZZO$$Z77
// 777I7O8OOOZ$$7777$$7II???I$O88D8Z$77777I?+++++??I7$777=?I8O7I~?7$Z778D8$I$$$Z$77
// 77777Z8O88Z$$$777$$7III7ODDO$ZZOOOZ777II?+====+?IIIII7=+ZND$I??I777I$I?II77$$777
// 777777O888OO$$777$77$$OOOI?$Z8II+$$$777$7$$ZZ$$Z?+==????I??I????I77?7??II$$77I77
// 7777I7ZO8888Z$77777Z$ZOOZ?7$8MZ7=?II??IZ$7?==+I$7??II7I7777I7II77I?7I?II7$$III77
// 777777$7$OO8D8OZ777$7$Z$7II7II???++??+IO$I++=+?I7$I?I?III77777II?77I??II7$$??III
// 777II$OZ7$$ZZ$7$ZOZZ7Z$$77IIIIII77I??IOZ$I?++++?I7I???++++?I77II+++??III777I?III
// 77III7OO777$ZZ$7777Z777$$7777777I???+?$$II??++???II?III????++++++++???II7I?7I?II
// IIIII7$$777$OZ$$$7II$I$777777I??+++I77I7III?+++??++??++++++=+====++???IIII?77III
// IIIIII$$7IIZZO$$$77II7$$$$$77777III???I?III?+++???+++++==========++???II7I?I??II
// IIIIII777II$$$$$$77777I??????????+++????III?++++??????+++=======++++??II7I?+?III
// IIIIIII7II7777$$$7777III????++++=++++?III77?+++++I?????II?++++++++????II7I++??II
// IIIIIIII7I77II$$$7777III???+++===++??IIII77?=====?I++?II777???+??????III7I??IIII
// IIIIIIII77777II$Z$7I77II???++==++?III7III7777II?I77Z8ZI??I77II????IIIIII77I77III
// III?III?I7II7II7$$7II7III???+++??I77I7$7$$$$77$$$ZZZ7??????IIIIIIIIIIIII7777IIII
// I????III?7II?II7$$$77777III?????777III$OZ$$ZZZZZ$II????????IIIIIIII77III77IIIIII
// ?????I?I?I7I???I$$$777777III?III7I?IIIII7777I77I++++++++????II??IIIIIIII77IIIIII
// ?????I?I??I77III7$$$77777777IIIIIIIIIIIIII????+++++=+++++?????+???IIIIII77IIIIII
// ???????????I7$$$7$$777$$$7777I??IIIII?????++++++++===++???I???+???IIII777IIIIIII
// ????????????I????7$$777$$$777II?IIIII????+++++++?????I$$ZODD$I++??IIII7777IIIIII
// ???????????????I?I7$7777777777IIIIIIIIIIIIII7777$$$Z777II$DO7I?+??I7I77$7II??III
// ??????????????I?III$$$7777777IIII7Z$ZOZZOZ$Z$I7$I+~+=~+=7Z$??I++??III7$77III?II?
// ?++?????????I?IIIII$$7$$777IIIIII$DN8$7II?=++====~~=+?888I????++?II777777I?I?III
// ???????+++?IIIIIIII7$$$$$77IIIIII777ODD$II+??~~+I7$O8ZI+?I???++??I777777II?????I
// ???????++??IIIIIIIII7$$$$$77III??II?777$$$$$ZZZZ$7I??++?II??+=+?III77777II??????
// ??????++???IIIIIIIII7$$$$$777III?II?I77I????++???+====+IIII?++?IIII77777I???????
// ?????????IIIIIIIIIIII7$$$$$$77II?++??I7$7II?????IIIII777I??++??IIII777??I???????
// ?????????IIIIIIIIIIIII$$7777777II?++?III7$777IIII7777$7I??+++??IIII77I??I???????
// ?????????I?IIIIIIIIIIII7$$$777777I????III7777$77$7$$7III?++++?IIII77I??IIIII????
// ???????+????IIIIIIIIIIII7$$7777777II????II77$$77777I?????+=++?III77I????II??????
// ????????????IIIIIIIIIIIIII7$777777III?+??????++============+??II77??????II??????
// I??????????IIIIIIIIIIIIIIII$$$77777II??++++++============+++?I777???????IIIII???
// I?????????I?IIIIIIIIII??III7$$$$7777I???++==++++++++++++++??I7$7????????I77$$77I
// ?I????????????IIIIIII??I?III77$$$7777III?++++++???????+???I7$$I?????????IIII77$$
#import <EHTracking/EHTAppTracker.h>
#import <SinglesShared/EHFImageService+Permissions.h>
#import <SinglesShared/SSLoginService.h>
#import <SinglesShared/SSLogin.h>
#import <SinglesShared/SSUserProfileService.h>
#import <SinglesShared/SSPhotoUploadService.h>
#import <SinglesShared/SSUserProfile.h>
#import <SinglesShared/SSUserPhotos.h>
#import <SinglesShared/SSPhotoInfo.h>
#import <SinglesShared/SSPhoto.h>
#import <SinglesShared/SSPhotoNotifications.h>
#import <SinglesShared/UIColor+Malibu.h>
#import <SinglesShared/SSFacebookService.h>
#import <EHKit/UIButton+Resizable.h>
#import <EHKit/NSAttributedString+Attributes.h>
#import <EHKit/EHKCustomRightBarButton.h>
#import "UIViewController+Navigation.h"
#import "SNGUserPhotosController.h"
#import "SNGUserPhotoCell.h"
#import "SNGUserFullScreenPhotoController.h"
#import "SNGFacebookAlbumViewController.h"
#import "SNGRQCompleteViewController.h"
#import "SNGPhotoReviewViewController.h"
#import "SNGPhotoBannerViewController.h"
#import "SNGBannerContainerViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface SNGUserPhotosController ()
@property (nonatomic, strong) SNGUserPhotoCell *sizingPhotoCell;
@end
@implementation SNGUserPhotosController
- (void)dealloc
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *accessibilityLabel = [NSString stringWithFormat:@"Photo Carousel"];
UpdateAccessibilityLabelForAutomation(photoGridView, accessibilityLabel);
// Register for photo upload to update image
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(photoUploadedMain:)
name:PHOTO_UPLOADED
object:nil];
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
dispatch_once(&trackingToken, ^{
// track if user has a camera
NSString *hasCameraValue = hasCamera ? @"yes" : @"no";
NSDictionary *params = [NSDictionary dictionaryWithObject:hasCameraValue forKey:@"camera"];
EHTAppTracker *tracker = [EHTAppTracker sharedInstance];
[tracker provider:EHTFlurryProvider trackPage:SNG_TRACKER_PROFILE_PHOTOS withParams:params];
});
photoGridView.style = EHFCarouselViewStyleGrid;
photoGridView.showsVerticalScrollIndicator = YES;
activityIndicatorContainer.hidden = NO;
[overallActivityIndicator startAnimating];
photoGridView.contentPadding = 8;
coverPhotoCameraButton.hidden = !hasCamera;
[coverPhotoLibraryButton resizeBackgroundImageWithTopInset:20 leftInset:5];
[coverPhotoCameraButton resizeBackgroundImageWithTopInset:20 leftInset:5];
[coverPhotoFacebookButton resizeBackgroundImageWithTopInset:20 leftInset:5];
[coverPhotoFBChooseButton resizeBackgroundImageWithTopInset:20 leftInset:5];
[coverPhotoFBOtherButton resizeBackgroundImageWithTopInset:20 leftInset:5];
coverPhotoOverlayContentView.layer.cornerRadius = 5;
coverPhotoOverlayContentView.layer.masksToBounds = YES;
SSLogin *login = [[SSLoginService sharedInstance] currentLogin];
BOOL male = login.gender == SSGenderMale;
NSString *overlayImageName = male ? @"Img_male_sample" : @"Img_female_sample";
coverPhotoOverlayImageView.image = [UIImage imageNamed:overlayImageName];
//if Registered via FB login, pre-populate cover photo w. profile pic
BOOL isViaFacebook = [SSFacebookService sharedInstance].fbId != nil;
if(isViaFacebook)
{
coverPhotoFacebookImageView.profileID = [SSFacebookService sharedInstance].fbId;
}
}
- (void)configNavBar
{
if (self.questionnaireMode) {
self.navigationItem.title = @"My Cover Photo";
} else {
self.navigationItem.title = @"My Photos";
}
if (leftBarButton) {
// use left bar button from the nib
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
} else {
//need to add a Cancel button to the nav bar in the event
//this view conttroller is being presented modally (ie from What If?)
if([self.navigationController isBeingPresented]) {
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissPhotoManager:)];
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
}
}
if (rightBarButton) {
// use right bar button from the nib
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
[self.navigationItem setRightBarButtonItem:barButtonItem animated:YES];
} else {
UIImage *image = [UIImage imageNamed:@"photoAdd"];
CGRect frame = CGRectMake(0,0,image.size.width,image.size.height);
EHKCustomRightBarButton *barButtonItem = [[EHKCustomRightBarButton alloc]initWithFrame:frame];
[barButtonItem setImage:image forState:UIControlStateNormal];
[barButtonItem addTarget:self action:@selector(selectNewPhoto:) forControlEvents:UIControlEventTouchUpInside];
barButtonItem.borderColor = [UIColor clearColor];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButtonItem];
[self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self configNavBar];
[self didLoadContent];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(photoDidUpload:)
name:PHOTO_UPLOADED
object:nil];
}
- (void)viewDidUnload
{
leftBarButton = nil;
rightBarButton = nil;
[super viewDidUnload];
photoGridView = nil;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
self.editing = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
// reload photos
[photoGridView refresh];
}
#pragma mark - Loading content callback
- (void)didLoadContent
{
userPhotos = [SSUserProfileService sharedInstance].currentUserProfile.photos;
// reset editing flag if the photo count is 0
if (userPhotos.photoCount == 0) {
self.editing = NO;
}
self.editButtonItem.title = self.editing ? @"Done" : @"Edit";
self.editButtonItem.enabled = userPhotos.photoCount;
if (!userPhotos.remainingCount) {
[self.navigationItem setRightBarButtonItem:nil animated:NO];
}
activityIndicatorContainer.hidden = YES;
[overallActivityIndicator stopAnimating];
// calculate how many more photos can be uploaded
NSString *firstName = [SSUserProfileService sharedInstance].currentUserProfile.firstName;
NSString *remainingCount = [NSString stringWithFormat:@"%d", userPhotos.remainingCount];
NSString *uploadText;
if (remainingCount && userPhotos.remainingCount) {
uploadText = [NSString stringWithFormat:@"Upload up to %@ more photos",
remainingCount];
} else {
uploadText = @"You've uploaded the max number of photos!";
}
NSMutableAttributedString *uploadPhotoString = [uploadPhotoPromptLabel.attributedText mutableCopy];
[uploadPhotoString replaceCharactersInRange:NSMakeRange(0, uploadPhotoString.length) withString:uploadText];
[uploadPhotoString setTextColor:[UIColor whiteColor]];
// -1 just to be sure we never get out of bounds by accident
NSRange nameFreeRange = NSMakeRange(firstName.length, uploadText.length - firstName.length - 1);
NSRange numberRange = [uploadText rangeOfString:remainingCount options:0 range:nameFreeRange];
[uploadPhotoString setFontName:@"HelveticaNeue-Bold" size:15.0 range:numberRange];
[uploadPhotoString setTextColor:[UIColor malibuYellowTextColor] range:numberRange];
uploadPhotoPromptLabel.attributedText = uploadPhotoString;
coverPhotoOverlay.hidden = userPhotos.totalPhotoCount;
coverPhotoView.hidden = !userPhotos.photoCount;
activityIndicatorContainer.hidden = (userPhotos.photos.count || !userPhotos.photosPendingUpload.count);
if (activityIndicatorContainer.hidden) {
[overallActivityIndicator stopAnimating];
} else {
[overallActivityIndicator startAnimating];
}
EHFImageCallback imageCallback = ^(UIImage *image) {
coverPhotoImageView.image = image;
};
[EHFImageService imageWithURL:userPhotos.primaryPhoto.cover.urlString
forSize:coverPhotoView.frame.size
bordered:NO allowUpscale:YES
scaledAndCropped:NO
useCache:NO
andCallback:imageCallback];
[photoGridView reload];
}
- (void)updatePhotoAtIndex:(NSInteger)index
{
if (index == 0) {
//reload cover photo
return;
}
[photoGridView updateTileAtIndex:index - 1];
[photoGridView reload];
}
- (void)photoDidUpload:(NSNotification *)notification
{
SSPhotoInfo *photoInfo = [notification.userInfo nullSafeForKey:@"photoInfo"];
if (photoInfo) {
// find the index of the freshly uploaded photo
SSUserPhotos *photos = [SSUserProfileService sharedInstance].currentUserProfile.photos;
NSInteger photoInteger = [photos.photos indexOfObject:photoInfo];
// and update its tile
[self updatePhotoAtIndex:photoInteger];
} else {
[self didLoadContent];
}
// present the cropper if there's only one photo OR
// the user is in RQ (i.e. incomplete or new)
if (userPhotos.photos.count == 1 || self.questionnaireMode) {
[self presentFullScreenPhotoViewForPhotoIndex:0 showCropEditor:YES];
}
}
#pragma mark - Carousel Data source
- (CGSize)carousel:(EHFCarouselView *)carousel sizeForTileAtIndex:(NSUInteger)index
{
dispatch_once(&sizingCellToken, ^{
NSBundle *bundle = [NSBundle mainBundle];
[bundle loadNibNamed:@"SNGUserPhotoCell" owner:self options:nil];
self.sizingPhotoCell = self.photoCell;
self.photoCell = nil;
});
return self.sizingPhotoCell.frame.size;
}
- (NSUInteger)numberOfTilesInCarousel:(EHFCarouselView *)carousel
{
NSInteger totalUploaded = userPhotos.photoCount + [userPhotos.photosPendingUpload count];
// subtract one to account for the cover photo
totalUploaded = MAX(totalUploaded - 1, 0);
return MIN(totalUploaded, 11);
}
- (UIView *)carousel:(EHFCarouselView *)carousel tileForIndex:(NSUInteger)index
{
SNGUserPhotoCell *cell = (SNGUserPhotoCell *)[carousel dequeueResuableTile];
// load cell from nib unless it can be dequeued
if (!cell) {
NSBundle *bundle = [NSBundle mainBundle];
[bundle loadNibNamed:@"SNGUserPhotoCell" owner:self options:nil];
cell = self.photoCell;
self.photoCell = nil;
}
// get model from user non-cover photos
SSPhotoInfo *model = [userPhotos.nonCoverPhotos boundSafeObjectAtIndex:index];
// now inject the content into the cells
[cell updateWithPhotoInfo:model];
// adding accessibility label to cell
NSString *accessibilityLabel = [NSString stringWithFormat:@"Photo_index %@", model.photoId];
UpdateAccessibilityLabelForAutomation(cell, accessibilityLabel);
//show picture for image
EHFImageCallback localImageCallback = ^(UIImage *image) {
SNGUserPhotoCell *theCell = [carousel visibleTileWithIndex:index];
[theCell setPending:YES];
[theCell updateWithImage:image];
};
EHFImageCallback remoteImageCallback = ^(UIImage *image) {
SNGUserPhotoCell *theCell = [carousel visibleTileWithIndex:index];
[theCell setPending:NO];
[theCell updateWithImage:image];
};
BOOL isPending = [userPhotos photoIsPending:model];
[cell setPending:isPending];
if (isPending) {
[EHFImageService imageWithImage:model.normal.image
forSize:cell.photoSize
bordered:NO
withCallBack:localImageCallback];
} else {
// load user photo here
[EHFImageService imageWithURL:model.scaled.urlString
forSize:cell.photoSize
bordered:NO
allowUpscale:YES
scaledAndCropped:YES
useCache:NO
andCallback:remoteImageCallback];
}
return cell;
}
- (IBAction)coverPhotoPressed:(id)sender
{
[self presentFullScreenPhotoViewForPhotoIndex:0 showCropEditor:NO];
}
- (void)carousel:(EHFCarouselView *)carousel didSelectIndex:(NSUInteger)index
{
// Increment the index because of the cover view
NSInteger carouselIndex = index + 1;
SSPhotoInfo *model = [userPhotos.allPhotos boundSafeObjectAtIndex:carouselIndex];
// user tapped an empty spot in edit mode, don't do anything.
if (!model) {
[self selectNewPhoto:nil];
return;
}
// if photo is pending, don't display it.
if ([userPhotos photoIsPending:model]) {
return;
}
// photo has been denied, ask user if he wants to delete it
if (model.status == SSPhotoDenied) {
photoPendingDeletion = nil;
deniedPhotoPendingDeletion = model;
NSString *title = @"Photo Rejected";
NSString *message = @"Please upload a new photo in place of this one.";
UIAlertView *deleteConfirm = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Maybe Later"
otherButtonTitles:@"Upload Now", nil];
[deleteConfirm show];
return;
}
// otherwise, user just tapped a random photo, present the photo controller
[self presentFullScreenPhotoViewForPhotoIndex:carouselIndex showCropEditor:NO];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// user canceled, just bail out
if (buttonIndex == alertView.cancelButtonIndex) {
// and don't forget to clear stored photo models
photoPendingDeletion = nil;
deniedPhotoPendingDeletion = nil;
return;
}
EHTAppTracker *tracker = [EHTAppTracker sharedInstance];
[tracker provider:EHTFlurryProvider trackEvent:SNG_TRACKER_PROFILE_PHOTOS_DELETED];
BOOL deletingDeniedPhoto = deniedPhotoPendingDeletion != nil;
SSPhotoInfo *deletedPhoto = deletingDeniedPhoto ? deniedPhotoPendingDeletion : photoPendingDeletion;
photoPendingDeletion = nil;
deniedPhotoPendingDeletion = nil;
activityIndicatorContainer.hidden = NO;
[overallActivityIndicator startAnimating];
SSUserProfileCallback callback = ^(NSError *error) {
[self didLoadContent];
};
SSUserProfileService *service = [SSUserProfileService sharedInstance];
[service deleteUserPhoto:deletedPhoto withCallback:callback];
// if the photo was reject, now present the upload controller
if (deletingDeniedPhoto) {
[self displayActionSheet];
}
}
- (void) addPendingPhoto:(SSPhotoInfo *) photoInfo
{
[userPhotos addPendingPhoto:photoInfo];
[[SSPhotoUploadService sharedInstance] uploadUserPhotos];
[self didLoadContent];
}
#pragma mark - UIActionSheetDelegate Methods
- (IBAction) selectNewPhoto:(id)sender {
[self displayActionSheet];
}
- (void) displayActionSheet {
UIActionSheet *uploadSheet;
// Check if camera not available on device
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
uploadSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Upload from Facebook", @"Upload from Library", nil];
} else {
uploadSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Upload from Facebook", @"Upload from Library", @"Take a Picture", nil];
}
[uploadSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.cancelButtonIndex == buttonIndex) {
return;
}
// user picked facebook, load/present that view controller and exit
if (buttonIndex == 0) {
[self pickFromFacebook];
return;
}
// somehow, take a photo was picked, but there's no camera:return now
if (buttonIndex == 2 && ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
return;
}
// get the right image picker controller and present it
if (buttonIndex == 1) {
[self pickFromLibrary];
} else {
[self pickFromCamera];
}
}
#pragma mark - UIImagePickerControllerDelegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// get a hold on the selected UIImage instance
NSString *key = picker.allowsEditing ? UIImagePickerControllerEditedImage : UIImagePickerControllerOriginalImage;
UIImage *photo = [info objectForKey:key];
// create the photo info model and add it to uploaded phtos
SSPhoto *photoModel = [[SSPhoto alloc] initWithImage:photo];
SSPhotoInfo *photoInfoModel = [[SSPhotoInfo alloc] initWithNormal:photoModel
withMMP:nil
withCover:nil
withIcon:nil
withScaled:nil
withThumbnail:nil
withCaption:nil];
[self addPendingPhoto:photoInfoModel];
// now we can pop the image picker and refresh the screen
[self dismissViewControllerAnimated:YES completion:nil];
NSMutableDictionary *trackingParams = [NSMutableDictionary dictionaryWithCapacity:5];
NSString *source = picker.sourceType == UIImagePickerControllerSourceTypeCamera ? @"camera" : @"device";
[trackingParams setInteger:1 forKey:source];
EHTAppTracker *tracker = [EHTAppTracker sharedInstance];
[tracker provider:EHTFlurryProvider trackEvent:SNG_TRACKING_PHOTO_UPLOADED withParams:trackingParams];
}
#pragma mark - Photo Upload Methods
- (void)photoUploadedMain:(NSNotification*)notification
{
[self performSelectorOnMainThread:@selector(photoUploaded:) withObject:notification waitUntilDone:NO];
}
- (void)photoUploaded:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"photoUploaded" object:self];
}
- (void)pickFromCamera
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)pickFromLibrary
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)pickFromFacebook
{
SNGFacebookAlbumViewController *avc = [[SNGFacebookAlbumViewController alloc] initWithNibName:@"SNGFacebookAlbumViewController" bundle:nil];
SNGPhotoBannerViewController *banner = [[SNGPhotoBannerViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [UIViewController navigationControllerWithViewController:avc];
SNGPhotoReviewViewController *photoReviewController = [[SNGPhotoReviewViewController alloc] init];
UINavigationController *photoReviewWithNav = [UINavigationController navigationControllerWithViewController:photoReviewController];
SNGBannerContainerViewController *bannerContainer = [[SNGBannerContainerViewController alloc] initWithBannerViewController:banner
contentViewController:nav
bannerDetailViewController:photoReviewWithNav];
banner.bannerContainer = bannerContainer;
photoReviewController.bannerContainer = bannerContainer;
[self presentViewController:bannerContainer animated:YES completion:nil];
}
- (void)pickFacebookProfilePhoto
{
UIImage *photo = nil;
for (NSObject *obj in [coverPhotoFacebookImageView subviews]) {
if ([obj isMemberOfClass:[UIImageView class]]) {
UIImageView *objImg = (UIImageView *)obj;
photo = objImg.image;
break;
}
}
// create the photo info model and add it to uploaded phtos
SSPhoto *photoModel = [[SSPhoto alloc] initWithImage:photo];
SSPhotoInfo *photoInfoModel = [[SSPhotoInfo alloc] initWithNormal:photoModel
withMMP:nil
withCover:nil
withIcon:nil
withScaled:nil
withThumbnail:nil
withCaption:nil];
[self addPendingPhoto:photoInfoModel];
// now we can advance to next screen
[self pushRQProgressViewController:nil];
}
- (IBAction)pickCoverFromCamera:(id)sender
{
[self pickFromCamera];
}
- (IBAction)pickCoverFromLibrary:(id)sender
{
[self pickFromLibrary];
}
- (IBAction)pickCoverFromFacebook:(id)sender
{
[self pickFromFacebook];
}
- (IBAction) pickCoverFromFacebookProfilePhoto:(id)sender
{
[self pickFacebookProfilePhoto];
}
- (IBAction) pickAnotherPhoto:(id)sender;
{
[UIView transitionFromView:self.view
toView:standardUploadView
duration:LONG_ANIMATION
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
- (void)presentFullScreenPhotoViewForPhotoIndex:(NSInteger)index
showCropEditor:(BOOL)showCropEditor
{
SNGUserFullScreenPhotoController *photoController = [[SNGUserFullScreenPhotoController alloc] initWithNibName:@"SNGUserFullScreenPhotoController" bundle:nil];
photoController.photos = userPhotos.photos;
photoController.initialIndex = index;
photoController.showCroppingEditor = showCropEditor;
photoController.questionnaireMode = self.questionnaireMode;
[self.navigationController pushViewController:photoController animated:YES];
}
#pragma mark - Navigation methods
- (IBAction)dismissPhotoManager:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)popPhotoManager:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)pushRQProgressViewController:(id)sender
{
// NOTE: assume that the photo collection is the last question in RQ29
id controller = [[SNGRQCompleteViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment