ProductView.m 11.6 KB
Newer Older
1
//
2
//  ProductView.m
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//  Mobile Buy SDK
//
//  Created by Shopify.
//  Copyright (c) 2015 Shopify Inc. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

27 28 29 30 31 32 33 34 35 36 37
#import "ProductView.h"
#import "ProductViewHeader.h"
#import "HeaderBackgroundView.h"
#import "ActionableFooterView.h"
#import "GradientView.h"
#import "ProductVariantCell.h"
#import "ProductDescriptionCell.h"
#import "ProductHeaderCell.h"
#import "AsyncImageView.h"
#import "ErrorView.h"
#import "Theme+Additions.h"
38

39
@interface ProductView ()
40

41
@property (nonatomic, strong) ErrorView *errorView;
42
@property (nonatomic, strong) NSLayoutConstraint *topInsetConstraint;
43 44 45

@end

46
@implementation ProductView
47

48
- (instancetype)initWithFrame:(CGRect)rect product:(BUYProduct*)product shouldShowApplePaySetup:(BOOL)showApplePaySetup
49 50 51
{
	self = [super initWithFrame:rect];
	if (self) {
52
		_backgroundImageView = [[HeaderBackgroundView alloc] init];
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		_backgroundImageView.translatesAutoresizingMaskIntoConstraints = NO;
		[self addSubview:_backgroundImageView];
		
		[self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundImageView
														 attribute:NSLayoutAttributeHeight
														 relatedBy:NSLayoutRelationEqual
															toItem:self
														 attribute:NSLayoutAttributeHeight
														multiplier:1.0
														  constant:0.0]];
		[self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundImageView
														 attribute:NSLayoutAttributeWidth
														 relatedBy:NSLayoutRelationEqual
															toItem:self
														 attribute:NSLayoutAttributeWidth
														multiplier:1.0
														  constant:0.0]];
		
		_stickyFooterView = [UIView new];
		_stickyFooterView.translatesAutoresizingMaskIntoConstraints = NO;
		[self addSubview:_stickyFooterView];
		
		_footerHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:_stickyFooterView
																	 attribute:NSLayoutAttributeHeight
																	 relatedBy:NSLayoutRelationEqual
																		toItem:nil
																	 attribute:NSLayoutAttributeNotAnAttribute
																	multiplier:1.0
																	  constant:0.0];
		[self addConstraint:_footerHeightLayoutConstraint];
		
		_footerOffsetLayoutConstraint = [NSLayoutConstraint constraintWithItem:_stickyFooterView
																	 attribute:NSLayoutAttributeTop
																	 relatedBy:NSLayoutRelationEqual
																		toItem:self
																	 attribute:NSLayoutAttributeBottom
																	multiplier:1.0
																	  constant:0.0];
		[self addConstraint:_footerOffsetLayoutConstraint];
		
		[self addConstraint:[NSLayoutConstraint constraintWithItem:_stickyFooterView
														 attribute:NSLayoutAttributeWidth
														 relatedBy:NSLayoutRelationEqual
															toItem:self
														 attribute:NSLayoutAttributeWidth
														multiplier:1.0
														  constant:0.0]];
		
		_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
		_tableView.backgroundColor = [UIColor clearColor];
		_tableView.translatesAutoresizingMaskIntoConstraints = NO;
		_tableView.estimatedRowHeight = 60.0;
		_tableView.rowHeight = UITableViewAutomaticDimension;
		_tableView.tableFooterView = [UIView new];
		_tableView.layoutMargins = UIEdgeInsetsMake(_tableView.layoutMargins.top, kBuyPaddingExtraLarge, _tableView.layoutMargins.bottom, kBuyPaddingMedium);
		[self addSubview:_tableView];
		
110 111 112
		[_tableView registerClass:[ProductHeaderCell class] forCellReuseIdentifier:@"headerCell"];
		[_tableView registerClass:[ProductVariantCell class] forCellReuseIdentifier:@"variantCell"];
		[_tableView registerClass:[ProductDescriptionCell class] forCellReuseIdentifier:@"descriptionCell"];
113 114 115 116 117
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_tableView)]];
118
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_tableView]|"
119 120 121
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_tableView)]];
Rune Madsen committed
122
		
123 124 125 126 127 128 129 130
		_topInsetConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
														   attribute:NSLayoutAttributeTop
														   relatedBy:NSLayoutRelationEqual
															  toItem:self
														   attribute:NSLayoutAttributeTop
														  multiplier:1.0
															constant:0];
		[self addConstraint:_topInsetConstraint];
131 132 133
		
		CGFloat size = MIN(CGRectGetWidth(rect), CGRectGetHeight(rect));
		if ([product.images count] > 0) {
134
			_productViewHeader = [[ProductViewHeader alloc] initWithFrame:CGRectMake(0, 0, size, size)];
135 136 137 138 139
			_tableView.tableHeaderView = self.productViewHeader;
		} else {
			_tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 1)];
		}
		
140
		_productViewFooter = [ActionableFooterView new];
141 142 143 144 145 146
		_productViewFooter.translatesAutoresizingMaskIntoConstraints = NO;
		[self addSubview:_productViewFooter];
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_productViewFooter]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_productViewFooter)]];
147
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_productViewFooter]-|"
148 149 150 151 152
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_productViewFooter)]];
		
		if (_productViewHeader) {
153 154
			_topGradientView = [[GradientView alloc] init];
			_topGradientView.topColor = [Theme topGradientViewTopColor];
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
			_topGradientView.translatesAutoresizingMaskIntoConstraints = NO;
			_topGradientView.userInteractionEnabled = NO;
			[self addSubview:_topGradientView];
			
			[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_topGradientView]|"
																		 options:0
																		 metrics:nil
																		   views:NSDictionaryOfVariableBindings(_topGradientView)]];
			[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_topGradientView(height)]"
																		 options:0
																		 metrics:@{ @"height" : @(kBuyTopGradientViewHeight) }
																		   views:NSDictionaryOfVariableBindings(_topGradientView)]];
		}
	}
	return self;
}

- (void)layoutSubviews
{
	[super layoutSubviews];
175
	[self setInsets:UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, CGRectGetHeight(self.bounds) - CGRectGetMinY(self.productViewFooter.frame), self.tableView.contentInset.right) appendToCurrentInset:NO];
176 177 178 179 180 181 182 183 184 185
}

- (void)updateBackgroundImage:(NSArray *)images
{
	if ([images count] > 0) {
		NSInteger page = 0;
		if (CGSizeEqualToSize(self.productViewHeader.collectionView.contentSize, CGSizeZero) == NO) {
			page = (int)(self.productViewHeader.collectionView.contentOffset.x / self.productViewHeader.collectionView.frame.size.width);
		}
		[self.productViewHeader setCurrentPage:page];
186
		BUYImageLink *image = images[page];
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		[self.backgroundImageView setBackgroundProductImage:image];
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	CGFloat imageHeight = 0;
	if (self.productViewHeader) {
		imageHeight = [self.productViewHeader imageHeightWithScrollViewDidScroll:scrollView];
	}
	CGFloat footerViewHeight = self.bounds.size.height - imageHeight;
	if (scrollView.contentOffset.y > 0) {
		footerViewHeight += scrollView.contentOffset.y;
	}
	if (footerViewHeight <= 0) {
		footerViewHeight = 0;
	}
204 205 206 207
	
	// Add the top inset for the navigation bar (when pushed in a navigation controller stack, not presented)
	footerViewHeight += -self.topInsetConstraint.constant;
	
208 209 210 211 212 213 214 215 216 217
	self.footerHeightLayoutConstraint.constant = footerViewHeight;
	self.footerOffsetLayoutConstraint.constant = -footerViewHeight;
	
	CGFloat opaqueOffset = CGRectGetHeight(self.productViewHeader.bounds);
	CGFloat whiteStartingOffset = opaqueOffset - CGRectGetHeight(self.topGradientView.bounds);
	self.topGradientView.alpha = -(scrollView.contentOffset.y - whiteStartingOffset) / (opaqueOffset - whiteStartingOffset);
}

#pragma mark - Error Handling

218
- (ErrorView *)errorView
219 220
{
	if (_errorView == nil) {
221
		_errorView = [[ErrorView alloc] init];
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		_errorView.alpha = 0;
		_errorView.translatesAutoresizingMaskIntoConstraints = NO;
		[self insertSubview:_errorView belowSubview:self.productViewFooter];
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_errorView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_errorView)]];
		
		_errorView.hiddenConstraint = [NSLayoutConstraint constraintWithItem:_errorView
																   attribute:NSLayoutAttributeTop
																   relatedBy:NSLayoutRelationEqual
																	  toItem:self
																   attribute:NSLayoutAttributeBottom
																  multiplier:1.0
																	constant:0.0];
		[self addConstraint:_errorView.hiddenConstraint];
		
		_errorView.visibleConstraint = [NSLayoutConstraint constraintWithItem:_errorView
																	attribute:NSLayoutAttributeBottom
																	relatedBy:NSLayoutRelationEqual
																	   toItem:self.productViewFooter
																	attribute:NSLayoutAttributeTop
																   multiplier:1.0
																	 constant:0.0];
		
		[NSLayoutConstraint activateConstraints:@[_errorView.hiddenConstraint]];
		[_errorView layoutIfNeeded];
	}
	return _errorView;
}

- (void)showErrorWithMessage:(NSString*)errorMessage
{
256 257 258
	[self.errorView presentErrorViewWithMessage:errorMessage completion:^{
		self.errorView = nil;
	}];
259 260 261 262 263 264 265 266 267 268 269
}

- (void)setInsets:(UIEdgeInsets)edgeInsets appendToCurrentInset:(BOOL)appendToCurrentInset
{
	CGFloat top = appendToCurrentInset ? self.tableView.contentInset.top + edgeInsets.top : edgeInsets.top;
	CGFloat left = appendToCurrentInset ? self.tableView.contentInset.left + edgeInsets.left : edgeInsets.left;
	CGFloat bottom = appendToCurrentInset ? self.tableView.contentInset.bottom + edgeInsets.bottom : edgeInsets.bottom;
	CGFloat right = appendToCurrentInset ? self.tableView.contentInset.right + edgeInsets.right : edgeInsets.right;
	self.tableView.contentInset = self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, left, bottom, right);
}

270 271 272 273 274
- (void)setTopInset:(CGFloat)topInset
{
	self.topInsetConstraint.constant = topInset;
}

275 276 277 278 279 280 281 282 283 284 285 286
#pragma mark - Appearance

- (void)setBackgroundColor:(UIColor *)backgroundColor {
	[super setBackgroundColor:backgroundColor];
	_stickyFooterView.backgroundColor = backgroundColor;
}

- (void)setShowsProductImageBackground:(BOOL)showsProductImageBackground
{
	_backgroundImageView.hidden = showsProductImageBackground == NO;
}

287
@end