1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-12 09:29:55 +01:00
Telegram/Share/TGShareChatListCell.m

225 lines
7.5 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGShareChatListCell.h"
#import "TGShareImageView.h"
2016-02-25 01:03:51 +01:00
#import "TGCheckButtonView.h"
2015-10-01 18:19:52 +02:00
#import "TGChatModel.h"
#import "TGPrivateChatModel.h"
#import "TGGroupChatModel.h"
2016-02-25 01:03:51 +01:00
#import "TGChannelChatModel.h"
2015-10-01 18:19:52 +02:00
#import "TGUserModel.h"
#import "TGChatListAvatarSignal.h"
@interface TGShareChatListCell ()
{
2016-02-25 01:03:51 +01:00
TGCheckButtonView *_checkButton;
2015-10-01 18:19:52 +02:00
TGShareImageView *_avatarView;
UILabel *_titleLabel;
2016-02-25 01:03:51 +01:00
bool _selectionEnabled;
2015-10-01 18:19:52 +02:00
}
@end
@implementation TGShareChatListCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self != nil)
2016-02-25 01:03:51 +01:00
{
2015-10-01 18:19:52 +02:00
_avatarView = [[TGShareImageView alloc] init];
[self.contentView addSubview:_avatarView];
_titleLabel = [[UILabel alloc] init];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
[self.contentView addSubview:_titleLabel];
}
return self;
}
- (void)setChatModel:(TGChatModel *)chatModel associatedUsers:(NSArray *)associatedUsers shareContext:(TGShareContext *)shareContext
{
if ([chatModel isKindOfClass:[TGPrivateChatModel class]])
{
TGPrivateChatModel *privateChatModel = (TGPrivateChatModel *)chatModel;
TGUserModel *userModel = nil;
2016-02-25 01:03:51 +01:00
for (id model in associatedUsers)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if ([model isKindOfClass:[TGUserModel class]] && ((TGUserModel *)model).userId == privateChatModel.peerId.peerId)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
userModel = model;
2015-10-01 18:19:52 +02:00
break;
}
}
_titleLabel.text = [userModel displayName];
if (userModel.avatarLocation == nil)
{
NSString *letters = @"";
if (userModel.firstName.length != 0 && userModel.lastName.length != 0)
{
letters = [[NSString alloc] initWithFormat:@"%@%@", [[userModel.firstName substringToIndex:1] uppercaseString], [[userModel.lastName substringToIndex:1] uppercaseString]];
}
else if (userModel.firstName.length != 0)
letters = [[userModel.firstName substringToIndex:1] uppercaseString];
else if (userModel.lastName.length != 0)
letters = [[userModel.lastName substringToIndex:1] uppercaseString];
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext letters:letters peerId:chatModel.peerId]];
}
else
{
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext location:userModel.avatarLocation]];
}
}
else if ([chatModel isKindOfClass:[TGGroupChatModel class]])
{
TGGroupChatModel *groupChatModel = (TGGroupChatModel *)chatModel;
_titleLabel.text = groupChatModel.title;
if (groupChatModel.avatarLocation == nil)
{
NSString *letters = [[groupChatModel.title substringToIndex:1] uppercaseString];
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext letters:letters peerId:chatModel.peerId]];
}
else
{
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext location:groupChatModel.avatarLocation]];
}
}
2016-02-25 01:03:51 +01:00
else if ([chatModel isKindOfClass:[TGChannelChatModel class]])
{
TGChannelChatModel *channelChatModel = (TGChannelChatModel *)chatModel;
_titleLabel.text = channelChatModel.title;
if (channelChatModel.avatarLocation == nil)
{
NSString *letters = [[channelChatModel.title substringFromIndex:1] uppercaseString];
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext letters:letters peerId:channelChatModel.peerId]];
}
else
{
[_avatarView setSignal:[TGChatListAvatarSignal chatListAvatarWithContext:shareContext location:channelChatModel.avatarLocation]];
}
}
2015-10-01 18:19:52 +02:00
[self setNeedsLayout];
}
2016-02-25 01:03:51 +01:00
- (void)setHighlighted:(BOOL)highlighted
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if (_selectionEnabled)
highlighted = false;
[super setHighlighted:highlighted];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (_selectionEnabled)
{
highlighted = false;
animated = false;
}
[super setHighlighted:highlighted animated:animated];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (_selectionEnabled)
return;
[super setSelected:selected animated:animated];
}
- (void)checkButtonPressed
{
[self setChecked:!_checkButton.selected animated:true];
}
- (void)setSelectionEnabled:(bool)enabled animated:(bool)animated
{
if (_selectionEnabled == enabled)
return;
_selectionEnabled = enabled;
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
if (enabled && _checkButton == nil)
{
_checkButton = [[TGCheckButtonView alloc] initWithStyle:TGCheckButtonStyleDefault];
_checkButton.frame = CGRectMake(-_checkButton.frame.size.width, (CGFloat)ceil((self.contentView.frame.size.height - _checkButton.frame.size.height) / 2.0f), _checkButton.frame.size.width, _checkButton.frame.size.height);
_checkButton.userInteractionEnabled = false;
[_checkButton addTarget:self action:@selector(checkButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_checkButton];
}
[self setNeedsLayout];
if (animated)
{
[UIView animateWithDuration:0.25 delay:0.0 options:(7 << 16 | UIViewAnimationOptionLayoutSubviews) animations:^
{
[self layoutIfNeeded];
} completion:^(BOOL finished)
{
if (!enabled)
[_checkButton setSelected:false animated:false];
}];
}
}
- (void)setChecked:(bool)checked animated:(bool)animated
{
if (!_selectionEnabled)
return;
[_checkButton setSelected:checked animated:animated bump:true];
}
- (void)layoutSubviews
{
2015-10-01 18:19:52 +02:00
CGSize size = self.contentView.frame.size;
2016-02-25 01:03:51 +01:00
CGFloat leftPadding = 10.0f;
2015-10-01 18:19:52 +02:00
CGFloat avatarWidth = 40.0f;
2016-02-25 01:03:51 +01:00
CGFloat avatarSpacing = 8.0f;
2015-10-01 18:19:52 +02:00
CGFloat rightPadding = 8.0f;
2016-02-25 01:03:51 +01:00
CGRect checkFrame = CGRectMake(0, (CGFloat)ceil((size.height - _checkButton.frame.size.height) / 2.0f), _checkButton.frame.size.width, _checkButton.frame.size.height);
if (_selectionEnabled)
{
leftPadding += 38;
checkFrame.origin.x = 7;
}
else
{
checkFrame.origin.x = -32;
}
[self setSeparatorInset:UIEdgeInsetsMake(0, leftPadding + avatarWidth + avatarSpacing, 0, 0)];
[super layoutSubviews];
_checkButton.frame = checkFrame;
2015-10-01 18:19:52 +02:00
CGSize titleSize = [_titleLabel.text sizeWithAttributes:@{NSFontAttributeName: _titleLabel.font}];
titleSize.width = MIN(size.width - leftPadding - avatarSpacing - avatarWidth - rightPadding, (CGFloat)ceil(titleSize.width));
titleSize.height = (CGFloat)ceil(titleSize.height);
2016-02-25 01:03:51 +01:00
[UIView performWithoutAnimation:^
{
_titleLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y, titleSize.width, titleSize.height);
}];
_titleLabel.frame = CGRectMake(leftPadding + avatarWidth + avatarSpacing, (CGFloat)ceil((size.height - titleSize.height) / 2.0f), _titleLabel.frame.size.width, _titleLabel.frame.size.height);
2015-10-01 18:19:52 +02:00
_avatarView.frame = CGRectMake(leftPadding, (CGFloat)ceil((size.height - avatarWidth) / 2.0f), avatarWidth, avatarWidth);
}
@end