Swiftgram/Classes/BITFeedbackListViewCell.m
2014-03-06 15:49:25 +01:00

298 lines
11 KiB
Objective-C

/*
* Author: Andreas Linde <mail@andreaslinde.de>
*
* Copyright (c) 2012-2014 HockeyApp, Bit Stadium GmbH.
* 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.
*/
#import "BITFeedbackListViewCell.h"
#import "HockeySDKPrivate.h"
#import "BITFeedbackMessageAttachment.h"
#define BACKGROUNDCOLOR_DEFAULT BIT_RGBCOLOR(245, 245, 245)
#define BACKGROUNDCOLOR_ALTERNATE BIT_RGBCOLOR(235, 235, 235)
#define BACKGROUNDCOLOR_DEFAULT_OS7 BIT_RGBCOLOR(255, 255, 255)
#define BACKGROUNDCOLOR_ALTERNATE_OS7 BIT_RGBCOLOR(255, 255, 255)
#define TEXTCOLOR_TITLE BIT_RGBCOLOR(75, 75, 75)
#define TEXTCOLOR_DEFAULT BIT_RGBCOLOR(25, 25, 25)
#define TEXTCOLOR_PENDING BIT_RGBCOLOR(75, 75, 75)
#define TITLE_FONTSIZE 12
#define TEXT_FONTSIZE 15
#define FRAME_SIDE_BORDER 10
#define FRAME_TOP_BORDER 8
#define FRAME_BOTTOM_BORDER 5
#define FRAME_LEFT_RESPONSE_BORDER 20
#define LABEL_TITLE_Y 3
#define LABEL_TITLE_HEIGHT 15
#define LABEL_TEXT_Y 25
#define ATTACHMENT_SIZE 45
@interface BITFeedbackListViewCell ()
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) NSDateFormatter *timeFormatter;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) NSMutableArray *attachmentViews;
@end
@implementation BITFeedbackListViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_backgroundStyle = BITFeedbackListViewCellBackgroundStyleNormal;
_style = BITFeedbackListViewCellPresentatationStyleDefault;
_message = nil;
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setLocale:[NSLocale currentLocale]];
[self.dateFormatter setDoesRelativeDateFormatting:YES];
self.timeFormatter = [[NSDateFormatter alloc] init];
[self.timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[self.timeFormatter setDateStyle:NSDateFormatterNoStyle];
[self.timeFormatter setLocale:[NSLocale currentLocale]];
[self.timeFormatter setDoesRelativeDateFormatting:YES];
self.labelTitle = [[UILabel alloc] init];
self.labelTitle.font = [UIFont systemFontOfSize:TITLE_FONTSIZE];
self.labelText = [[BITAttributedLabel alloc] init];
self.labelText.font = [UIFont systemFontOfSize:TEXT_FONTSIZE];
self.labelText.numberOfLines = 0;
self.labelText.textAlignment = kBITTextLabelAlignmentLeft;
self.labelText.dataDetectorTypes = UIDataDetectorTypeAll;
self.attachmentViews = [NSMutableArray new];
}
return self;
}
#pragma mark - Private
- (UIColor *)backgroundColor {
if (self.backgroundStyle == BITFeedbackListViewCellBackgroundStyleNormal) {
if (self.style == BITFeedbackListViewCellPresentatationStyleDefault) {
return BACKGROUNDCOLOR_DEFAULT;
} else {
return BACKGROUNDCOLOR_DEFAULT_OS7;
}
} else {
if (self.style == BITFeedbackListViewCellPresentatationStyleDefault) {
return BACKGROUNDCOLOR_ALTERNATE;
} else {
return BACKGROUNDCOLOR_ALTERNATE_OS7;
}
}
}
- (BOOL)isSameDayWithDate1:(NSDate*)date1 date2:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponent1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents *dateComponent2 = [calendar components:unitFlags fromDate:date2];
return ([dateComponent1 day] == [dateComponent2 day] &&
[dateComponent1 month] == [dateComponent2 month] &&
[dateComponent1 year] == [dateComponent2 year]);
}
#pragma mark - Layout
+ (CGFloat) heightForRowWithMessage:(BITFeedbackMessage *)message tableViewWidth:(CGFloat)width {
CGFloat baseHeight = [self heightForTextInRowWithMessage:message tableViewWidth:width];
CGFloat attachmentsPerRow = floorf(width / (FRAME_SIDE_BORDER + ATTACHMENT_SIZE));
CGFloat calculatedHeight = baseHeight + (FRAME_TOP_BORDER + ATTACHMENT_SIZE) * ceil(message.attachments.count/attachmentsPerRow);
return ceil(calculatedHeight);
}
+ (CGFloat) heightForTextInRowWithMessage:(BITFeedbackMessage *)message tableViewWidth:(CGFloat)width {
CGFloat calculatedHeight;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
if ([message.text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
CGRect calculatedRect = [message.text boundingRectWithSize:CGSizeMake(width - (2 * FRAME_SIDE_BORDER), CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TEXT_FONTSIZE]}
context:nil];
calculatedHeight = calculatedRect.size.height + FRAME_TOP_BORDER + LABEL_TEXT_Y + FRAME_BOTTOM_BORDER;
// added to make space for the images.
} else {
#endif
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
calculatedHeight = [message.text sizeWithFont:[UIFont systemFontOfSize:TEXT_FONTSIZE]
constrainedToSize:CGSizeMake(width - (2 * FRAME_SIDE_BORDER), CGFLOAT_MAX)
].height + FRAME_TOP_BORDER + LABEL_TEXT_Y + FRAME_BOTTOM_BORDER;
#pragma clang diagnostic pop
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
}
#endif
return ceil(calculatedHeight);
}
- (void)setAttachments:(NSArray *)attachments {
for (UIView *view in self.attachmentViews){
[view removeFromSuperview];
}
[self.attachmentViews removeAllObjects];
for (BITFeedbackMessageAttachment *attachment in attachments){
UIButton *imageView = [UIButton buttonWithType:UIButtonTypeCustom];
[imageView setImage:[attachment thumbnailWithSize:CGSizeMake(ATTACHMENT_SIZE, ATTACHMENT_SIZE)] forState:UIControlStateNormal];
[imageView addTarget:self action:@selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.attachmentViews addObject:imageView];
[self addSubview:imageView];
}
}
- (void)layoutSubviews {
UIView *accessoryViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 2, self.frame.size.width * 2, self.frame.size.height - 2)];
accessoryViewBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight;
accessoryViewBackground.clipsToBounds = YES;
// colors
accessoryViewBackground.backgroundColor = [self backgroundColor];
self.contentView.backgroundColor = [self backgroundColor];
self.labelTitle.backgroundColor = [self backgroundColor];
self.labelText.backgroundColor = [self backgroundColor];
self.labelTitle.textColor = TEXTCOLOR_TITLE;
if (_message.status == BITFeedbackMessageStatusSendPending || _message.status == BITFeedbackMessageStatusSendInProgress) {
[self.labelText setTextColor:TEXTCOLOR_PENDING];
} else {
[self.labelText setTextColor:TEXTCOLOR_DEFAULT];
}
// background for deletion accessory view
if (self.style == BITFeedbackListViewCellPresentatationStyleDefault) {
[self addSubview:accessoryViewBackground];
}
// header
NSString *dateString = @"";
if (_message.status == BITFeedbackMessageStatusSendPending || _message.status == BITFeedbackMessageStatusSendInProgress) {
dateString = BITHockeyLocalizedString(@"Pending");
} else if (_message.date) {
if ([self isSameDayWithDate1:[NSDate date] date2:_message.date]) {
dateString = [self.timeFormatter stringFromDate:_message.date];
} else {
dateString = [self.dateFormatter stringFromDate:_message.date];
}
}
[self.labelTitle setText:dateString];
[self.labelTitle setFrame:CGRectMake(FRAME_SIDE_BORDER, FRAME_TOP_BORDER + LABEL_TITLE_Y, self.frame.size.width - (2 * FRAME_SIDE_BORDER), LABEL_TITLE_HEIGHT)];
if (_message.userMessage) {
self.labelTitle.textAlignment = kBITTextLabelAlignmentRight;
self.labelText.textAlignment = kBITTextLabelAlignmentRight;
} else {
self.labelTitle.textAlignment = kBITTextLabelAlignmentLeft;
self.labelText.textAlignment = kBITTextLabelAlignmentLeft;
}
[self addSubview:self.labelTitle];
// text
[self.labelText setText:_message.text];
CGSize sizeForTextLabel = CGSizeMake(self.frame.size.width - (2 * FRAME_SIDE_BORDER),
[[self class] heightForTextInRowWithMessage:_message tableViewWidth:self.frame.size.width] - LABEL_TEXT_Y - FRAME_BOTTOM_BORDER);
[self.labelText setFrame:CGRectMake(FRAME_SIDE_BORDER, LABEL_TEXT_Y, sizeForTextLabel.width, sizeForTextLabel.height)];
[self addSubview:self.labelText];
CGFloat baseOffsetOfText = CGRectGetMaxY(self.labelText.frame);
int i = 0;
CGFloat attachmentsPerRow = ceilf(self.frame.size.width / (FRAME_SIDE_BORDER + ATTACHMENT_SIZE));
for ( UIButton *imageButton in self.attachmentViews){
imageButton.contentMode = UIViewContentModeScaleAspectFit;
if ( !_message.userMessage){
imageButton.frame = CGRectMake(FRAME_SIDE_BORDER + (FRAME_SIDE_BORDER + ATTACHMENT_SIZE) * i , floor(i/attachmentsPerRow) + baseOffsetOfText , ATTACHMENT_SIZE, ATTACHMENT_SIZE);
} else {
imageButton.frame = CGRectMake(self.frame.size.width - FRAME_SIDE_BORDER - ATTACHMENT_SIZE - ((FRAME_SIDE_BORDER + ATTACHMENT_SIZE) * (i) ), floor(i/attachmentsPerRow) + baseOffsetOfText , ATTACHMENT_SIZE, ATTACHMENT_SIZE);
}
i++;
}
[super layoutSubviews];
}
- (void)imageButtonPressed:(id)sender {
if ([self.delegate respondsToSelector:@selector(listCell:didSelectAttachment:)]){
NSInteger index = [self.attachmentViews indexOfObject:sender];
if (index != NSNotFound){
BITFeedbackMessageAttachment *attachment = self.message.attachments[index];
[self.delegate listCell:self didSelectAttachment:attachment];
}
}
}
@end