session-ios/Signal/src/ViewControllers/InboxTableViewCell.m

359 lines
13 KiB
Mathematica
Raw Normal View History

2017-03-09 07:36:09 +01:00
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
2014-10-29 21:58:58 +01:00
#import "InboxTableViewCell.h"
Disappearing Messages * Per thread settings menu accessed by tapping on thread title This removed the toggle-phone behavior. You'll be able to see the phone number in the settings table view. This removed the "add contact" functionality, although it was already broken for ios>=9 (which is basically everybody). The group actions menu was absorbed into this screen * Added a confirm alert to leave group (fixes #938) * New Translation Strings * Extend "Add People" label to fit translations. * resolved issues with translations not fitting in group menu * Fix the long standing type warning where TSCalls were assigned to a TSMessageAdapter. * Can delete info messages Follow the JSQMVC pattern and put UIResponder-able content in the messageBubbleContainer. This gives us more functionality *and* allows us to delete some code. yay! It's still not yet possible to delete phone messages. =( * Fixed some compiler warnings. * xcode8 touching storyboard. So long xcode7! * Fixup multiline info messages. We were seeing info messages like "You set disappearing message timer to 10" instead of "You set disappearing message timer to 10 seconds." Admittedly this isn't a very good fix, as now one liners feel like they have too much padding. If the message is well over one line, we were wrapping properly, but there's a problem when the message is *just barely* two lines, the cell height grows, but the label still thinks it's just one line (as evinced by the one line appearing in the center of the label frame. The result being that the last word of the label is cropped. * Disable group actions after leaving group. // FREEBIE
2016-09-21 14:37:51 +02:00
#import "OWSAvatarBuilder.h"
#import "Signal-Swift.h"
#import "Util.h"
#import "ViewControllerUtils.h"
#import <SignalServiceKit/OWSMessageManager.h>
#import <SignalServiceKit/TSContactThread.h>
#import <SignalServiceKit/TSGroupThread.h>
#import <SignalServiceKit/TSThread.h>
Disappearing Messages * Per thread settings menu accessed by tapping on thread title This removed the toggle-phone behavior. You'll be able to see the phone number in the settings table view. This removed the "add contact" functionality, although it was already broken for ios>=9 (which is basically everybody). The group actions menu was absorbed into this screen * Added a confirm alert to leave group (fixes #938) * New Translation Strings * Extend "Add People" label to fit translations. * resolved issues with translations not fitting in group menu * Fix the long standing type warning where TSCalls were assigned to a TSMessageAdapter. * Can delete info messages Follow the JSQMVC pattern and put UIResponder-able content in the messageBubbleContainer. This gives us more functionality *and* allows us to delete some code. yay! It's still not yet possible to delete phone messages. =( * Fixed some compiler warnings. * xcode8 touching storyboard. So long xcode7! * Fixup multiline info messages. We were seeing info messages like "You set disappearing message timer to 10" instead of "You set disappearing message timer to 10 seconds." Admittedly this isn't a very good fix, as now one liners feel like they have too much padding. If the message is well over one line, we were wrapping properly, but there's a problem when the message is *just barely* two lines, the cell height grows, but the label still thinks it's just one line (as evinced by the one line appearing in the center of the label frame. The result being that the last word of the label is cropped. * Disable group actions after leaving group. // FREEBIE
2016-09-21 14:37:51 +02:00
NS_ASSUME_NONNULL_BEGIN
2014-10-29 21:58:58 +01:00
#define ARCHIVE_IMAGE_VIEW_WIDTH 22.0f
#define DELETE_IMAGE_VIEW_WIDTH 19.0f
#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
const NSUInteger kAvatarViewDiameter = 52;
@interface InboxTableViewCell ()
@property (nonatomic) AvatarImageView *avatarView;
@property (nonatomic) UILabel *nameLabel;
@property (nonatomic) UILabel *snippetLabel;
@property (nonatomic) UILabel *timeLabel;
@property (nonatomic) UIView *unreadBadge;
@property (nonatomic) UILabel *unreadLabel;
2017-08-23 22:41:31 +02:00
@property (nonatomic) TSThread *thread;
@property (nonatomic) OWSContactsManager *contactsManager;
@end
#pragma mark -
@implementation InboxTableViewCell
2014-10-29 21:58:58 +01:00
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self commontInit];
}
return self;
}
// `[UIView init]` invokes `[self initWithFrame:...]`.
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self commontInit];
}
return self;
}
- (void)commontInit
{
OWSAssert(!self.avatarView);
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
self.preservesSuperviewLayoutMargins = YES;
self.contentView.preservesSuperviewLayoutMargins = YES;
self.backgroundColor = [UIColor whiteColor];
self.avatarView = [[AvatarImageView alloc] init];
[self.contentView addSubview:self.avatarView];
2017-07-21 16:36:45 +02:00
[self.avatarView autoSetDimension:ALDimensionWidth toSize:self.avatarSize];
[self.avatarView autoSetDimension:ALDimensionHeight toSize:self.avatarSize];
2017-10-02 20:26:03 +02:00
[self.avatarView autoPinLeadingToSuperview];
[self.avatarView autoVCenterInSuperview];
self.nameLabel = [UILabel new];
self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.nameLabel.font = [UIFont ows_boldFontWithSize:14.0f];
[self.contentView addSubview:self.nameLabel];
[self.nameLabel autoPinLeadingToTrailingOfView:self.avatarView margin:13.f];
[self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.avatarView];
[self.nameLabel setContentHuggingHorizontalLow];
self.snippetLabel = [UILabel new];
self.snippetLabel.font = [UIFont ows_regularFontWithSize:14.f];
self.snippetLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.snippetLabel.textColor = [UIColor colorWithWhite:2 / 3.f alpha:1.f];
self.snippetLabel.numberOfLines = 2;
self.snippetLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[self.contentView addSubview:self.snippetLabel];
[self.snippetLabel autoPinLeadingToTrailingOfView:self.avatarView margin:13.f];
[self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameLabel withOffset:5.f];
[self.snippetLabel autoPinEdge:ALEdgeTrailing toEdge:ALEdgeTrailing ofView:self.nameLabel];
[self.snippetLabel setContentHuggingHorizontalLow];
self.timeLabel = [UILabel new];
self.timeLabel.font = [UIFont ows_lightFontWithSize:14.f];
[self.contentView addSubview:self.timeLabel];
2017-10-02 20:26:03 +02:00
[self.timeLabel autoPinTrailingToSuperview];
[self.timeLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.nameLabel];
[self.timeLabel autoPinLeadingToTrailingOfView:self.nameLabel margin:10.f];
[self.timeLabel setContentHuggingHorizontalHigh];
[self.timeLabel setCompressionResistanceHigh];
const int kunreadBadgeSize = 24;
self.unreadBadge = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kunreadBadgeSize, kunreadBadgeSize)];
self.unreadBadge.layer.cornerRadius = kunreadBadgeSize / 2;
self.unreadBadge.backgroundColor = [UIColor ows_materialBlueColor];
[self.contentView addSubview:self.unreadBadge];
[self.unreadBadge autoSetDimension:ALDimensionWidth toSize:kunreadBadgeSize];
[self.unreadBadge autoSetDimension:ALDimensionHeight toSize:kunreadBadgeSize];
2017-10-02 20:26:03 +02:00
[self.unreadBadge autoPinTrailingToSuperview];
[self.unreadBadge autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.avatarView];
[self.unreadBadge setContentHuggingHorizontalHigh];
[self.unreadBadge setCompressionResistanceHigh];
self.unreadLabel = [UILabel new];
self.unreadLabel.font = [UIFont ows_regularFontWithSize:12.f];
self.unreadLabel.textColor = [UIColor whiteColor];
self.unreadLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.unreadLabel.textAlignment = NSTextAlignmentCenter;
[self.unreadBadge addSubview:self.unreadLabel];
[self.unreadLabel autoVCenterInSuperview];
[self.unreadLabel autoPinWidthToSuperview];
}
+ (NSString *)cellReuseIdentifier
{
return NSStringFromClass([self class]);
iOS 9 Support - Fixing size classes rendering bugs. - Supporting native iOS San Francisco font. - Quick Reply - Settings now slide to the left as suggested in original designed opposed to modal. - Simplification of restraints on many screens. - Full-API compatiblity with iOS 9 and iOS 8 legacy support. - Customized AddressBook Permission prompt when restrictions are enabled. If user installed Signal previously and already approved access to Contacts, don't bugg him again. - Fixes crash in migration for users who installed Signal <2.1.3 but hadn't signed up yet. - Xcode 7 / iOS 9 Travis Support - Bitcode Support is disabled until it is better understood how exactly optimizations are performed. In a first time, we will split out the crypto code into a separate binary to make it easier to optimize the non-sensitive code. Blog post with more details coming. - Partial ATS support. We are running our own Certificate Authority at Open Whisper Systems. Signal is doing certificate pinning to verify that certificates were signed by our own CA. Unfortunately Apple's App Transport Security requires to hand over chain verification to their framework with no control over the trust store. We have filed a radar to get ATS features with pinned certificates. In the meanwhile, ATS is disabled on our domain. We also followed Amazon's recommendations for our S3 domain we use to upload/download attachments. (#891) - Implement a unified `AFSecurityOWSPolicy` pinning strategy accross libraries (AFNetworking RedPhone/TextSecure & SocketRocket).
2015-09-01 19:22:08 +02:00
}
+ (CGFloat)rowHeight
{
return 72.f;
}
2017-07-21 16:36:45 +02:00
- (CGFloat)avatarSize
{
return 52.f;
}
iOS 9 Support - Fixing size classes rendering bugs. - Supporting native iOS San Francisco font. - Quick Reply - Settings now slide to the left as suggested in original designed opposed to modal. - Simplification of restraints on many screens. - Full-API compatiblity with iOS 9 and iOS 8 legacy support. - Customized AddressBook Permission prompt when restrictions are enabled. If user installed Signal previously and already approved access to Contacts, don't bugg him again. - Fixes crash in migration for users who installed Signal <2.1.3 but hadn't signed up yet. - Xcode 7 / iOS 9 Travis Support - Bitcode Support is disabled until it is better understood how exactly optimizations are performed. In a first time, we will split out the crypto code into a separate binary to make it easier to optimize the non-sensitive code. Blog post with more details coming. - Partial ATS support. We are running our own Certificate Authority at Open Whisper Systems. Signal is doing certificate pinning to verify that certificates were signed by our own CA. Unfortunately Apple's App Transport Security requires to hand over chain verification to their framework with no control over the trust store. We have filed a radar to get ATS features with pinned certificates. In the meanwhile, ATS is disabled on our domain. We also followed Amazon's recommendations for our S3 domain we use to upload/download attachments. (#891) - Implement a unified `AFSecurityOWSPolicy` pinning strategy accross libraries (AFNetworking RedPhone/TextSecure & SocketRocket).
2015-09-01 19:22:08 +02:00
- (void)initializeLayout {
self.selectionStyle = UITableViewCellSelectionStyleDefault;
2014-10-29 21:58:58 +01:00
}
Disappearing Messages * Per thread settings menu accessed by tapping on thread title This removed the toggle-phone behavior. You'll be able to see the phone number in the settings table view. This removed the "add contact" functionality, although it was already broken for ios>=9 (which is basically everybody). The group actions menu was absorbed into this screen * Added a confirm alert to leave group (fixes #938) * New Translation Strings * Extend "Add People" label to fit translations. * resolved issues with translations not fitting in group menu * Fix the long standing type warning where TSCalls were assigned to a TSMessageAdapter. * Can delete info messages Follow the JSQMVC pattern and put UIResponder-able content in the messageBubbleContainer. This gives us more functionality *and* allows us to delete some code. yay! It's still not yet possible to delete phone messages. =( * Fixed some compiler warnings. * xcode8 touching storyboard. So long xcode7! * Fixup multiline info messages. We were seeing info messages like "You set disappearing message timer to 10" instead of "You set disappearing message timer to 10 seconds." Admittedly this isn't a very good fix, as now one liners feel like they have too much padding. If the message is well over one line, we were wrapping properly, but there's a problem when the message is *just barely* two lines, the cell height grows, but the label still thinks it's just one line (as evinced by the one line appearing in the center of the label frame. The result being that the last word of the label is cropped. * Disable group actions after leaving group. // FREEBIE
2016-09-21 14:37:51 +02:00
- (nullable NSString *)reuseIdentifier
{
2014-10-29 21:58:58 +01:00
return NSStringFromClass(self.class);
}
- (void)configureWithThread:(TSThread *)thread
contactsManager:(OWSContactsManager *)contactsManager
blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet
Disappearing Messages * Per thread settings menu accessed by tapping on thread title This removed the toggle-phone behavior. You'll be able to see the phone number in the settings table view. This removed the "add contact" functionality, although it was already broken for ios>=9 (which is basically everybody). The group actions menu was absorbed into this screen * Added a confirm alert to leave group (fixes #938) * New Translation Strings * Extend "Add People" label to fit translations. * resolved issues with translations not fitting in group menu * Fix the long standing type warning where TSCalls were assigned to a TSMessageAdapter. * Can delete info messages Follow the JSQMVC pattern and put UIResponder-able content in the messageBubbleContainer. This gives us more functionality *and* allows us to delete some code. yay! It's still not yet possible to delete phone messages. =( * Fixed some compiler warnings. * xcode8 touching storyboard. So long xcode7! * Fixup multiline info messages. We were seeing info messages like "You set disappearing message timer to 10" instead of "You set disappearing message timer to 10 seconds." Admittedly this isn't a very good fix, as now one liners feel like they have too much padding. If the message is well over one line, we were wrapping properly, but there's a problem when the message is *just barely* two lines, the cell height grows, but the label still thinks it's just one line (as evinced by the one line appearing in the center of the label frame. The result being that the last word of the label is cropped. * Disable group actions after leaving group. // FREEBIE
2016-09-21 14:37:51 +02:00
{
OWSAssert([NSThread isMainThread]);
OWSAssert(thread);
OWSAssert(contactsManager);
OWSAssert(blockedPhoneNumberSet);
2017-08-23 22:41:31 +02:00
self.thread = thread;
self.contactsManager = contactsManager;
BOOL isBlocked = NO;
if (!thread.isGroupThread) {
NSString *contactIdentifier = thread.contactIdentifier;
isBlocked = [blockedPhoneNumberSet containsObject:contactIdentifier];
}
2017-08-23 22:41:31 +02:00
NSMutableAttributedString *snippetText = [NSMutableAttributedString new];
if (isBlocked) {
// If thread is blocked, don't show a snippet or mute status.
[snippetText appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"HOME_VIEW_BLOCKED_CONTACT_CONVERSATION",
@"A label for conversations with blocked users.")
attributes:@{
NSFontAttributeName : [UIFont ows_mediumFontWithSize:12],
NSForegroundColorAttributeName : [UIColor ows_blackColor],
}]];
} else {
if ([thread isMuted]) {
[snippetText appendAttributedString:[[NSAttributedString alloc]
initWithString:@"\ue067 "
attributes:@{
NSFontAttributeName : [UIFont ows_elegantIconsFont:9.f],
NSForegroundColorAttributeName : (thread.hasUnreadMessages
? [UIColor colorWithWhite:0.1f alpha:1.f]
: [UIColor lightGrayColor]),
}]];
}
2017-10-26 18:09:36 +02:00
NSString *displayableText = [DisplayableText displayableText:thread.lastMessageLabel];
if (displayableText) {
[snippetText appendAttributedString:[[NSAttributedString alloc]
initWithString:displayableText
attributes:@{
NSFontAttributeName : (thread.hasUnreadMessages
? [UIFont ows_mediumFontWithSize:12]
: [UIFont ows_regularFontWithSize:12]),
NSForegroundColorAttributeName :
(thread.hasUnreadMessages ? [UIColor ows_blackColor]
: [UIColor lightGrayColor]),
}]];
}
}
NSAttributedString *attributedDate = [self dateAttributedString:thread.lastMessageDate];
NSUInteger unreadCount = [[OWSMessageManager sharedManager] unreadMessagesInThread:thread];
2017-08-23 22:41:31 +02:00
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(otherUsersProfileDidChange:)
name:kNSNotificationName_OtherUsersProfileDidChange
object:nil];
[self updateNameLabel];
[self updateAvatarView];
self.snippetLabel.attributedText = snippetText;
self.timeLabel.attributedText = attributedDate;
2017-07-21 16:36:45 +02:00
self.separatorInset = UIEdgeInsetsMake(0, self.avatarSize * 1.5f, 0, 0);
_timeLabel.textColor = thread.hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor];
if (unreadCount > 0) {
self.unreadBadge.hidden = NO;
self.unreadLabel.hidden = NO;
self.unreadLabel.text = [ViewControllerUtils formatInt:MIN(99, (int)unreadCount)];
} else {
self.unreadBadge.hidden = YES;
self.unreadLabel.hidden = YES;
}
}
- (void)updateAvatarView
{
OWSContactsManager *contactsManager = self.contactsManager;
if (contactsManager == nil) {
OWSFail(@"%@ contactsManager should not be nil", self.logTag);
self.avatarView.image = nil;
return;
}
TSThread *thread = self.thread;
if (thread == nil) {
OWSFail(@"%@ thread should not be nil", self.logTag);
self.avatarView.image = nil;
return;
}
self.avatarView.image =
[OWSAvatarBuilder buildImageForThread:thread diameter:kAvatarViewDiameter contactsManager:contactsManager];
2014-10-29 21:58:58 +01:00
}
2017-09-08 16:28:21 +02:00
2014-10-29 21:58:58 +01:00
#pragma mark - Date formatting
- (NSAttributedString *)dateAttributedString:(NSDate *)date {
2015-02-12 23:08:57 +01:00
NSString *timeString;
if ([DateUtil dateIsToday:date]) {
2015-02-12 23:08:57 +01:00
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];
2014-10-29 21:58:58 +01:00
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor ows_darkGrayColor]
2014-10-29 21:58:58 +01:00
range:NSMakeRange(0, timeString.length)];
2014-10-29 21:58:58 +01:00
[attributedString addAttribute:NSFontAttributeName
value:[UIFont ows_regularFontWithSize:TIME_LABEL_SIZE]
2014-10-29 21:58:58 +01:00
range:NSMakeRange(0, timeString.length)];
2014-10-29 21:58:58 +01:00
return attributedString;
}
- (void)prepareForReuse
{
[super prepareForReuse];
2017-11-08 18:56:55 +01:00
[[NSNotificationCenter defaultCenter] removeObserver:self];
2014-10-29 21:58:58 +01:00
}
2017-08-23 22:41:31 +02:00
#pragma mark - Name
- (void)otherUsersProfileDidChange:(NSNotification *)notification
{
OWSAssert([NSThread isMainThread]);
NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
if (recipientId.length == 0) {
return;
}
if (![self.thread isKindOfClass:[TSContactThread class]]) {
return;
}
if (![self.thread.contactIdentifier isEqualToString:recipientId]) {
return;
}
[self updateNameLabel];
[self updateAvatarView];
2017-08-23 22:41:31 +02:00
}
-(void)updateNameLabel
{
AssertIsOnMainThread();
TSThread *thread = self.thread;
if (thread == nil) {
OWSFail(@"%@ thread should not be nil", self.logTag);
self.nameLabel.attributedText = nil;
return;
}
OWSContactsManager *contactsManager = self.contactsManager;
if (contactsManager == nil) {
OWSFail(@"%@ contacts manager should not be nil", self.logTag);
self.nameLabel.attributedText = nil;
return;
}
NSAttributedString *name;
if (thread.isGroupThread) {
if (thread.name.length == 0) {
name = [[NSAttributedString alloc] initWithString:[MessageStrings newGroupDefaultTitle]];
2017-08-23 22:41:31 +02:00
} else {
name = [[NSAttributedString alloc] initWithString:thread.name];
}
} else {
name = [contactsManager attributedStringForConversationTitleWithPhoneIdentifier:thread.contactIdentifier
primaryFont:self.nameLabel.font
secondaryFont:[UIFont ows_footnoteFont]];
}
self.nameLabel.attributedText = name;
}
#pragma mark - Logging
+ (NSString *)logTag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)logTag
{
return self.class.logTag;
}
2014-10-29 21:58:58 +01:00
@end
Disappearing Messages * Per thread settings menu accessed by tapping on thread title This removed the toggle-phone behavior. You'll be able to see the phone number in the settings table view. This removed the "add contact" functionality, although it was already broken for ios>=9 (which is basically everybody). The group actions menu was absorbed into this screen * Added a confirm alert to leave group (fixes #938) * New Translation Strings * Extend "Add People" label to fit translations. * resolved issues with translations not fitting in group menu * Fix the long standing type warning where TSCalls were assigned to a TSMessageAdapter. * Can delete info messages Follow the JSQMVC pattern and put UIResponder-able content in the messageBubbleContainer. This gives us more functionality *and* allows us to delete some code. yay! It's still not yet possible to delete phone messages. =( * Fixed some compiler warnings. * xcode8 touching storyboard. So long xcode7! * Fixup multiline info messages. We were seeing info messages like "You set disappearing message timer to 10" instead of "You set disappearing message timer to 10 seconds." Admittedly this isn't a very good fix, as now one liners feel like they have too much padding. If the message is well over one line, we were wrapping properly, but there's a problem when the message is *just barely* two lines, the cell height grows, but the label still thinks it's just one line (as evinced by the one line appearing in the center of the label frame. The result being that the last word of the label is cropped. * Disable group actions after leaving group. // FREEBIE
2016-09-21 14:37:51 +02:00
NS_ASSUME_NONNULL_END