2014-10-29 21:58:58 +01:00
|
|
|
//
|
|
|
|
// TableViewCell.m
|
|
|
|
// Signal
|
|
|
|
//
|
|
|
|
// Created by Dylan Bourgeois on 27/10/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2014-11-21 00:51:23 +01:00
|
|
|
#import "InboxTableViewCell.h"
|
2015-02-09 17:31:22 +01:00
|
|
|
#import "Environment.h"
|
|
|
|
#import "PreferencesUtil.h"
|
2014-10-29 21:58:58 +01:00
|
|
|
#import "Util.h"
|
2014-12-06 19:26:01 +01:00
|
|
|
#import "UIImage+JSQMessages.h"
|
2014-12-24 02:25:10 +01:00
|
|
|
#import "TSGroupThread.h"
|
2015-02-07 19:41:13 +01:00
|
|
|
#import "TSContactThread.h"
|
2015-01-14 10:51:47 +01:00
|
|
|
#import "JSQMessagesAvatarImageFactory.h"
|
2014-10-29 21:58:58 +01:00
|
|
|
#define ARCHIVE_IMAGE_VIEW_WIDTH 22.0f
|
|
|
|
#define DELETE_IMAGE_VIEW_WIDTH 19.0f
|
2014-12-06 19:26:01 +01:00
|
|
|
#define TIME_LABEL_SIZE 11
|
2014-10-29 21:58:58 +01:00
|
|
|
#define DATE_LABEL_SIZE 13
|
2015-01-24 04:26:04 +01:00
|
|
|
#define SWIPE_ARCHIVE_OFFSET -50
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2014-11-21 00:51:23 +01:00
|
|
|
@implementation InboxTableViewCell
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
|
|
|
self = [NSBundle.mainBundle loadNibNamed:NSStringFromClass(self.class)
|
|
|
|
owner:self
|
|
|
|
options:nil][0];
|
|
|
|
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
_scrollView.contentSize = CGSizeMake(CGRectGetWidth(_contentContainerView.bounds),
|
|
|
|
CGRectGetHeight(_scrollView.frame));
|
2014-12-06 19:26:01 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
_scrollView.contentOffset = CGPointMake(CGRectGetWidth(_archiveView.frame), 0);
|
|
|
|
_archiveImageView.image = [_archiveImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)reuseIdentifier {
|
|
|
|
return NSStringFromClass(self.class);
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
-(void)configureWithThread:(TSThread*)thread {
|
|
|
|
_nameLabel.text = thread.name;
|
2014-11-25 19:06:09 +01:00
|
|
|
_snippetLabel.text = thread.lastMessageLabel;
|
2014-11-21 14:38:37 +01:00
|
|
|
_timeLabel.attributedText = [self dateAttributedString:thread.lastMessageDate];
|
2015-01-27 02:20:11 +01:00
|
|
|
if([thread isKindOfClass:[TSGroupThread class]]) {
|
2015-01-25 01:19:17 +01:00
|
|
|
_contactPictureView.image = ((TSGroupThread*)thread).groupModel.groupImage!=nil ? ((TSGroupThread*)thread).groupModel.groupImage : [UIImage imageNamed:@"empty-group-avatar"];
|
2015-01-27 02:20:11 +01:00
|
|
|
if([_nameLabel.text length]==0) {
|
|
|
|
_nameLabel.text = @"New Group";
|
|
|
|
}
|
2015-01-30 04:31:35 +01:00
|
|
|
if(_contactPictureView.image!=nil) {
|
|
|
|
[UIUtil applyRoundedBorderToImageView:&_contactPictureView];
|
|
|
|
}
|
2015-01-14 10:51:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-01-18 09:38:26 +01:00
|
|
|
NSMutableString *initials = [NSMutableString string];
|
2015-01-14 10:51:47 +01:00
|
|
|
if([thread.name length]>0) {
|
2015-01-18 09:38:26 +01:00
|
|
|
NSArray *words = [thread.name componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
for (NSString * word in words) {
|
|
|
|
if ([word length] > 0) {
|
|
|
|
NSString *firstLetter = [word substringToIndex:1];
|
|
|
|
[initials appendString:[firstLetter uppercaseString]];
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 10:51:47 +01:00
|
|
|
}
|
2015-02-07 19:41:13 +01:00
|
|
|
|
|
|
|
UIColor *backgroundColor = thread.isGroupThread ? [UIColor whiteColor] : [UIColor backgroundColorForContact:((TSContactThread*)thread).contactIdentifier];
|
|
|
|
UIImage* image = [[JSQMessagesAvatarImageFactory avatarImageWithUserInitials:initials backgroundColor:backgroundColor textColor:[UIColor whiteColor] font:[UIFont ows_boldFontWithSize:36.0] diameter:100] avatarImage];
|
2015-01-14 10:51:47 +01:00
|
|
|
_contactPictureView.image = thread.image!=nil ? thread.image : image;
|
2015-02-07 19:41:13 +01:00
|
|
|
if(thread.image!=nil) {
|
2015-01-27 02:20:11 +01:00
|
|
|
[UIUtil applyRoundedBorderToImageView:&_contactPictureView];
|
|
|
|
}
|
2015-01-14 10:51:47 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 23:08:57 +01:00
|
|
|
self.separatorInset = UIEdgeInsetsMake(0,_contactPictureView.frame.size.width*1.5f, 0, 0);
|
2015-02-16 19:48:31 +01:00
|
|
|
|
|
|
|
if (thread.hasUnreadMessages) {
|
|
|
|
[self updateCellForUnreadMessage];
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)configureForState:(CellState)state
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case kArchiveState:
|
2015-01-27 02:20:11 +01:00
|
|
|
_archiveImageView.image = [[UIImage imageNamed:@"cellBtnMoveToInbox--blue"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
|
2014-10-29 21:58:58 +01:00
|
|
|
break;
|
|
|
|
case kInboxState:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 19:26:01 +01:00
|
|
|
-(void)updateCellForUnreadMessage
|
|
|
|
{
|
2015-02-12 23:08:57 +01:00
|
|
|
_nameLabel.font = [UIFont ows_boldFontWithSize:14.0f];
|
2015-01-14 22:30:01 +01:00
|
|
|
_snippetLabel.textColor = [UIColor ows_blackColor];
|
2015-02-12 23:08:57 +01:00
|
|
|
_timeLabel.textColor = [UIColor ows_materialBlueColor];
|
2014-12-06 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)updateCellForReadMessage
|
|
|
|
{
|
2015-02-12 23:08:57 +01:00
|
|
|
_nameLabel.font = [UIFont ows_regularFontWithSize:14.0f];
|
2014-12-06 19:26:01 +01:00
|
|
|
_snippetLabel.textColor = [UIColor lightGrayColor];
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
#pragma mark - Date formatting
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
- (NSAttributedString *)dateAttributedString:(NSDate *)date {
|
2015-02-12 23:08:57 +01:00
|
|
|
NSString *timeString;
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2015-02-12 23:08:57 +01:00
|
|
|
if ([date timeIntervalSinceNow] > (- 60 * 60 * 24)) {
|
|
|
|
timeString = [[DateUtil timeFormatter] stringFromDate:date];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
timeString = [[DateUtil dateFormatter] stringFromDate:date];
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
|
|
|
|
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:timeString];
|
|
|
|
|
|
|
|
[attributedString addAttribute:NSForegroundColorAttributeName
|
2014-11-29 19:54:33 +01:00
|
|
|
value:[UIColor ows_darkGrayColor]
|
2014-10-29 21:58:58 +01:00
|
|
|
range:NSMakeRange(0, timeString.length)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[attributedString addAttribute:NSFontAttributeName
|
2015-01-14 22:30:01 +01:00
|
|
|
value:[UIFont ows_regularFontWithSize:TIME_LABEL_SIZE]
|
2014-10-29 21:58:58 +01:00
|
|
|
range:NSMakeRange(0, timeString.length)];
|
|
|
|
|
|
|
|
|
|
|
|
return attributedString;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
|
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
|
|
|
|
|
|
|
if (_scrollView.contentOffset.x < 0) {
|
2015-01-14 22:30:01 +01:00
|
|
|
_archiveImageView.image = [_archiveImageView.image jsq_imageMaskedWithColor:[UIColor ows_materialBlueColor]];
|
2014-10-29 21:58:58 +01:00
|
|
|
_archiveImageView.bounds = CGRectMake(_archiveImageView.bounds.origin.x,
|
|
|
|
_archiveImageView.bounds.origin.y,
|
|
|
|
ARCHIVE_IMAGE_VIEW_WIDTH,
|
|
|
|
_archiveImageView.bounds.size.height);
|
|
|
|
} else {
|
|
|
|
|
2014-12-06 19:26:01 +01:00
|
|
|
_archiveImageView.image = [_archiveImageView.image jsq_imageMaskedWithColor:[UIColor ows_darkGrayColor]];
|
2014-10-29 21:58:58 +01:00
|
|
|
double ratio = (_archiveView.frame.size.width/2.0f - _scrollView.contentOffset.x) / (_archiveView.frame.size.width/2.0f);
|
|
|
|
double newWidth = ARCHIVE_IMAGE_VIEW_WIDTH/2.0f + (ARCHIVE_IMAGE_VIEW_WIDTH * ratio)/2.0f;
|
|
|
|
_archiveImageView.bounds = CGRectMake(_archiveImageView.bounds.origin.x,
|
|
|
|
_archiveImageView.bounds.origin.y,
|
|
|
|
(CGFloat)newWidth,
|
|
|
|
_archiveImageView.bounds.size.height);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
|
|
|
|
withVelocity:(CGPoint)velocity
|
|
|
|
targetContentOffset:(inout CGPoint *)targetContentOffset {
|
2015-01-24 04:26:04 +01:00
|
|
|
if (_scrollView.contentOffset.x < SWIPE_ARCHIVE_OFFSET) {
|
|
|
|
// archive the thread
|
2014-10-29 21:58:58 +01:00
|
|
|
[_delegate tableViewCellTappedArchive:self];
|
2015-02-09 17:31:22 +01:00
|
|
|
[Environment.preferences setHasArchivedAMessage:YES];
|
|
|
|
}
|
|
|
|
else {
|
2015-01-24 04:26:04 +01:00
|
|
|
// don't do anything
|
2014-10-29 21:58:58 +01:00
|
|
|
*targetContentOffset = CGPointMake(CGRectGetWidth(_archiveView.frame), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Animation
|
|
|
|
|
|
|
|
-(void)animateDisappear
|
|
|
|
{
|
|
|
|
[UIView animateWithDuration:1.0f animations:^(){
|
|
|
|
self.alpha = 0;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|