ProductHeaderCell.m 6.74 KB
Newer Older
1
//
2
//  ProductHeaderCell.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
#import "ProductHeaderCell.h"
28
#import "UIFont+Additions.h"
29
#import "Theme+Additions.h"
30

31
@interface ProductHeaderCell ()
32 33 34 35 36 37 38 39

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *comparePriceLabel;
@property (nonatomic, strong) BUYProductVariant *productVariant;

@end

40
@implementation ProductHeaderCell
41 42 43 44 45 46 47 48 49 50 51

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		
		self.layoutMargins = UIEdgeInsetsMake(kBuyPaddingExtraLarge, self.layoutMargins.left, kBuyPaddingExtraLarge, self.layoutMargins.right);
		
		_titleLabel = [[UILabel alloc] init];
		_titleLabel.textColor = [UIColor blackColor];
52
		_titleLabel.font = [Theme productTitleFont];
53 54 55 56 57 58 59 60 61
		_titleLabel.numberOfLines = 0;
		_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
		[self.contentView addSubview:_titleLabel];
		
		UIView *priceView = [[UIView alloc] init];
		priceView.translatesAutoresizingMaskIntoConstraints = NO;
		[self.contentView addSubview:priceView];
		
		_priceLabel = [[UILabel alloc] init];
62
		_priceLabel.font = [Theme productPriceFont];
63 64 65 66 67 68 69
		_priceLabel.translatesAutoresizingMaskIntoConstraints = NO;
		_priceLabel.textAlignment = NSTextAlignmentRight;
		[_priceLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
		[_priceLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
		[priceView addSubview:_priceLabel];
		
		_comparePriceLabel = [[UILabel alloc] init];
70
		_comparePriceLabel.textColor = [Theme comparePriceTextColor];
71
		_comparePriceLabel.textAlignment = NSTextAlignmentRight;
72
		_comparePriceLabel.font = [Theme productComparePriceFont];
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
		_comparePriceLabel.translatesAutoresizingMaskIntoConstraints = NO;
		[_comparePriceLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
		[_comparePriceLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
		[priceView addSubview:_comparePriceLabel];
		
		NSDictionary *views = NSDictionaryOfVariableBindings(priceView, _titleLabel);
		[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_titleLabel]-[priceView]-|" options:0 metrics:nil views:views]];
		[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_titleLabel]" options:0 metrics:nil views:views]];
		[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[priceView]" options:0 metrics:nil views:views]];
		[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeFirstBaseline relatedBy:NSLayoutRelationEqual toItem:_priceLabel attribute:NSLayoutAttributeFirstBaseline multiplier:1.0 constant:0.0]];
		
		[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:0.0]];
		[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:priceView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:0.0]];
		
		NSDictionary *priceViews = NSDictionaryOfVariableBindings(_priceLabel, _comparePriceLabel);
		[priceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_priceLabel]|" options:0 metrics:nil views:priceViews]];
		[priceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_comparePriceLabel]|" options:0 metrics:nil views:priceViews]];
		[priceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_priceLabel][_comparePriceLabel]|" options:0 metrics:nil views:priceViews]];
	}
	return self;
}

- (void)setProductVariant:(BUYProductVariant *)productVariant withCurrencyFormatter:(NSNumberFormatter*)currencyFormatter
{
	_productVariant = productVariant;
	
	self.titleLabel.text = productVariant.product.title;
	
	if (currencyFormatter) {
		self.priceLabel.text = [currencyFormatter stringFromNumber:productVariant.price];
	}
	
105
	if (productVariant.availableValue == YES && productVariant.compareAtPrice) {
106 107 108
		NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:[currencyFormatter stringFromNumber:productVariant.compareAtPrice]
																			   attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)}];
		self.comparePriceLabel.attributedText = attributedString;
109
		self.comparePriceLabel.textColor = [Theme comparePriceTextColor];
110
	} else if (productVariant.available == NO) {
111 112
		self.comparePriceLabel.text = NSLocalizedString(@"Sold Out", @"Sold out text displayed on product view");
		self.comparePriceLabel.textColor = [Theme variantSoldOutTextColor];
113 114 115 116 117 118 119 120
	} else {
		self.comparePriceLabel.attributedText = nil;
	}
	
	[self setNeedsLayout];
	[self layoutIfNeeded];
}

121
- (void)setBackgroundColor:(UIColor *)backgroundColor
122
{
123
	[super setBackgroundColor:backgroundColor];
124 125 126 127 128 129 130 131 132
	self.titleLabel.backgroundColor = self.priceLabel.backgroundColor = self.comparePriceLabel.backgroundColor = self.backgroundColor;
}

- (void)tintColorDidChange
{
	[super tintColorDidChange];
	self.priceLabel.textColor = self.tintColor;
}

133 134 135 136 137
- (void)setProductTitleColor:(UIColor*)color
{
	self.titleLabel.textColor = color;
}

138
@end