Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
315 lines (246 sloc) 11.8 KB
#import "ARAddToAlbumViewController.h"
#import "ARTableViewCell.h"
#import "ARSansSerifTextField.h"
#import "ARFlatButton.h"
#import "ARNavigationController.h"
#import "ARTickedTableViewCell.h"
@interface ARAddToAlbumViewController ()
@property (readonly, nonatomic, assign) BOOL showTextField;
@property (readonly, nonatomic, strong) UIButton *createAlbumButton;
@property (readonly, nonatomic, strong) UITextField *createAlbumTextField;
@property (readonly, nonatomic, strong) NSManagedObjectContext *context;
@end
@implementation ARAddToAlbumViewController
static const CGFloat ARTableViewCellSize = 44;
static const CGSize ARNewAlbumButtonSize = {.height = 42};
static const CGSize ARNewAlbumButtonInset = {.height = 15, .width = 15};
static const CGSize ARNewAlbumTextFieldInset = {.height = 10, .width = 16};
static const NSInteger AmountOfCellsToShowBeforeScrollingOnAdd = 6;
static const CGFloat ARSyncMessageHeight = 44;
- (void)dealloc
{
if (self.showTextField) {
[self.createAlbumTextField removeFromSuperview];
}
}
- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext *)context;
{
self = [super initWithStyle:UITableViewStylePlain];
if (!self) return nil;
_albums = [Album editableAlbumsByLastUpdateInContext:context];
_context = context;
return self;
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger albumCount = [self.albums count];
BOOL rowHasButton = (indexPath.row == albumCount && !self.showTextField) || indexPath.row > albumCount;
BOOL showSyncMessage = [self shouldShowSyncedAlbumsMessage];
if (rowHasButton) {
CGFloat buttonHeight = ARNewAlbumButtonSize.height + (ARNewAlbumButtonInset.height * 2);
if (showSyncMessage) buttonHeight += ARSyncMessageHeight + ARNewAlbumButtonInset.height;
return buttonHeight;
} else {
return ARTableViewCellSize;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.showTextField) {
return [self.albums count] + 2;
} else {
return [self.albums count] + 1;
}
}
- (ARSansSerifTextField *)createTextFieldWithFrame:(CGRect)textFieldFrame
{
ARSansSerifTextField *textField = [[ARSansSerifTextField alloc] initWithFrame:textFieldFrame];
textField.text = @"";
textField.delegate = self;
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
textField.font = [UIFont serifFontWithSize:ARFontSerif];
textField.textAlignment = NSTextAlignmentNatural;
textField.tintColor = [UIColor blackColor];
return textField;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ARTableViewCell *cell = nil;
NSInteger albumCount = [self.albums count];
BOOL rowHasTextfield = indexPath.row == albumCount && self.showTextField;
BOOL rowHasButton = (indexPath.row == albumCount && !self.showTextField) || indexPath.row > albumCount;
NSString *cellIdentifier = @"ARArtworkActionViewCell";
if (rowHasButton) {
cellIdentifier = @"ARArtworkActionButtonCell";
}
if (rowHasTextfield) {
cellIdentifier = @"ARArtworkActionTextfieldCell";
}
cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ARTickedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
if (indexPath.row < albumCount) {
Album *rowAlbum = self.albums[indexPath.row];
NSSet *rowAlbumArtworks = rowAlbum.artworks;
NSString *labelString = [NSString stringWithFormat:@"%@ ( %@ )", rowAlbum.name, @(rowAlbumArtworks.count)];
cell.textLabel.text = labelString;
BOOL showTick = NO;
if (rowAlbumArtworks.count >= _artworks.count) {
showTick = ([[NSSet setWithArray:_artworks] isSubsetOfSet:rowAlbumArtworks]);
}
[(ARTickedTableViewCell *)cell setTickSelected:showTick animated:NO];
}
else if (rowHasTextfield) {
CGRect textFieldFrame = CGRectMake(ARNewAlbumTextFieldInset.width, 4, cell.contentView.frame.size.width - (ARNewAlbumTextFieldInset.width * 2), ARNewAlbumButtonSize.height);
_createAlbumTextField = [self createTextFieldWithFrame:textFieldFrame];
cell.accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
[cell.contentView addSubview:self.createAlbumTextField];
[self.createAlbumTextField becomeFirstResponder];
if (self.albums.count >= AmountOfCellsToShowBeforeScrollingOnAdd) {
NSIndexPath *lastCellPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[aTableView scrollToRowAtIndexPath:lastCellPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
else if (rowHasButton) {
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
cell.accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
_createAlbumButton = [self createNewAlbumButton];
[cell.contentView addSubview:self.createAlbumButton];
if ([self shouldShowSyncedAlbumsMessage]) {
CGRect syncFrame = cell.frame;
syncFrame.size.height = ARSyncMessageHeight;
syncFrame.origin.y = CGRectGetMaxY(_createAlbumButton.frame) + ARNewAlbumButtonInset.height;
UILabel *syncMessageLabel = [[UILabel alloc] initWithFrame:syncFrame];
syncMessageLabel.font = [UIFont serifFontWithSize:ARFontSerif];
syncMessageLabel.textColor = [UIColor blackColor];
syncMessageLabel.numberOfLines = 2;
syncMessageLabel.textAlignment = NSTextAlignmentCenter;
syncMessageLabel.text = @"Albums not created on this device\ncan be edited from your Artsy CMS";
[cell.contentView addSubview:syncMessageLabel];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
- (BOOL)shouldShowSyncedAlbumsMessage
{
NSFetchRequest *allAlbums = [[NSFetchRequest alloc] init];
allAlbums.entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:self.context];
allAlbums.predicate = [NSPredicate predicateWithFormat:@"editable = NO AND slug != 'all_artworks' AND slug != 'for_sale_works'"];
return [self.context countForFetchRequest:allAlbums error:nil] > 0;
}
- (ARFlatButton *)createNewAlbumButton
{
ARFlatButton *button = [ARFlatButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"NEW ALBUM" forState:UIControlStateNormal];
[button addTarget:self action:@selector(createNewAlbum) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor artsyGrayMedium];
[button setBackgroundColor:UIColor.blackColor forState:UIControlStateHighlighted];
button.frame = CGRectMake(ARNewAlbumButtonInset.height, ARNewAlbumButtonInset.width, CGRectGetWidth(self.tableView.frame) - (ARNewAlbumButtonInset.width * 2), ARNewAlbumButtonSize.height);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont sansSerifFontWithSize:ARFontSansRegular];
return button;
}
- (CGSize)preferredContentSize
{
// we need to take into account the lost "All Artworks" album,
NSInteger offset = self.showTextField ? 1 : 0;
CGFloat buttonHeight = ARNewAlbumButtonSize.height + (ARNewAlbumButtonInset.height * 2);
if ([self shouldShowSyncedAlbumsMessage]) buttonHeight += ARSyncMessageHeight + ARNewAlbumButtonInset.height;
CGFloat height = ((self.albums.count + offset) * ARTableViewCellSize) + buttonHeight;
return CGSizeMake(320, height);
}
#pragma mark deal with creation of new album
- (void)createNewAlbum
{
if (self.showTextField) {
[self textFieldShouldReturn:self.createAlbumTextField];
} else {
_showTextField = YES;
NSMutableArray *paths = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:self.albums.count inSection:0]];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.albums.count inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[self.createAlbumButton setTitle:@"DONE" forState:UIControlStateNormal];
self.createAlbumButton.enabled = NO;
self.createAlbumButton.alpha = .3;
[self.createAlbumButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
if (self.albums.count < AmountOfCellsToShowBeforeScrollingOnAdd) {
self.container.popoverContentSize = self.preferredContentSize;
}
}
}
#pragma mark TextField delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField.text isEqualToString:@""]) return NO;
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:@""]) return;
Album *album = [Album object];
album.name = textField.text;
album.artworks = [NSSet setWithArray:self.artworks];
[album updateArtists];
[album saveManagedObjectContextLoggingErrors];
_albums = [Album editableAlbumsByLastUpdateInContext:album.managedObjectContext];
_showTextField = NO;
[self.createAlbumButton setTitle:@"NEW ALBUM" forState:UIControlStateNormal];
[[self tableView] reloadData];
[textField removeFromSuperview];
[ARAnalytics event:ARNewAlbumEvent withProperties:@{
@"from" : [ARNavigationController pageID],
@"artworks" : @([self.artworks count])
}];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSInteger length = textField.text.length + string.length - range.length;
if (length == 0) {
self.createAlbumButton.enabled = NO;
self.createAlbumButton.alpha = 0.3;
} else {
self.createAlbumButton.enabled = YES;
self.createAlbumButton.alpha = 1;
}
return YES;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [self.albums count]) {
Album *selectedAlbum = ((Album *)self.albums[indexPath.row]);
ARTickedTableViewCell *cell = (ARTickedTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if ([cell isSelected]) {
[ARAnalytics event:ARRemoveFromAlbumEvent withProperties:@{
@"artworks" : @(self.artworks.count),
@"from" : [ARNavigationController pageID]
}];
[selectedAlbum removeArtworks:[NSSet setWithArray:_artworks]];
if (_documents) {
[selectedAlbum removeDocuments:[NSSet setWithArray:_documents]];
}
[cell setTickSelected:NO animated:YES];
} else {
[ARAnalytics event:ARAddToAlbumEvent withProperties:@{
@"artworks" : @(self.artworks.count),
@"from" : [ARNavigationController pageID]
}];
[selectedAlbum addArtworks:[NSSet setWithArray:_artworks]];
if (_documents) {
[selectedAlbum addDocuments:[NSSet setWithArray:_documents]];
}
[cell setTickSelected:YES animated:YES];
}
[selectedAlbum updateArtists];
[selectedAlbum saveManagedObjectContextLoggingErrors];
NSString *labelString = [NSString stringWithFormat:@"%@ ( %@ )", selectedAlbum.name, @(selectedAlbum.artworks.count)];
cell.textLabel.text = labelString;
}
}
@end