session-ios/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m

701 lines
24 KiB
Mathematica
Raw Normal View History

2017-10-10 22:13:54 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-10-10 22:13:54 +02:00
//
#import "OWSMessageCell.h"
2018-06-25 20:31:09 +02:00
#import "OWSContactAvatarBuilder.h"
2018-04-05 19:26:15 +02:00
#import "OWSMessageBubbleView.h"
2017-10-10 22:13:54 +02:00
#import "Signal-Swift.h"
2018-04-05 19:19:13 +02:00
2017-10-10 22:13:54 +02:00
NS_ASSUME_NONNULL_BEGIN
@interface OWSMessageCell ()
2017-10-12 23:04:35 +02:00
// The nullable properties are created as needed.
// The non-nullable properties are so frequently used that it's easier
// to always keep one around.
2017-11-17 16:49:34 +01:00
2018-04-05 19:19:13 +02:00
@property (nonatomic) OWSMessageBubbleView *messageBubbleView;
2018-06-28 23:28:59 +02:00
@property (nonatomic) UIStackView *dateHeaderView;
@property (nonatomic) UIView *dateStrokeView;
@property (nonatomic) UILabel *dateHeaderLabel;
2018-06-25 20:31:09 +02:00
@property (nonatomic) AvatarImageView *avatarView;
2018-07-05 16:25:30 +02:00
@property (nonatomic, nullable) UIImageView *sendFailureBadgeView;
2017-11-17 16:49:34 +01:00
2018-03-28 16:11:01 +02:00
@property (nonatomic, nullable) NSMutableArray<NSLayoutConstraint *> *viewConstraints;
@property (nonatomic) BOOL isPresentingMenuController;
2017-10-10 22:13:54 +02:00
@end
2018-07-05 16:25:30 +02:00
#pragma mark -
2017-10-10 22:13:54 +02:00
@implementation OWSMessageCell
// `[UIView init]` invokes `[self initWithFrame:...]`.
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self commontInit];
}
return self;
}
- (void)commontInit
{
2018-04-13 21:10:16 +02:00
// Ensure only called once.
2018-04-05 19:19:13 +02:00
OWSAssert(!self.messageBubbleView);
2017-10-10 22:13:54 +02:00
self.layoutMargins = UIEdgeInsetsZero;
self.contentView.layoutMargins = UIEdgeInsetsZero;
2017-10-10 22:13:54 +02:00
2018-06-22 20:53:33 +02:00
_viewConstraints = [NSMutableArray new];
2018-04-05 19:19:13 +02:00
self.messageBubbleView = [OWSMessageBubbleView new];
[self.contentView addSubview:self.messageBubbleView];
2017-11-17 16:49:34 +01:00
self.dateStrokeView = [UIView new];
2018-06-28 23:28:59 +02:00
self.dateStrokeView.backgroundColor = [UIColor ows_light45Color];
[self.dateStrokeView autoSetDimension:ALDimensionHeight toSize:self.dateHeaderStrokeThickness];
[self.dateStrokeView setContentHuggingHigh];
self.dateHeaderLabel = [UILabel new];
2018-06-28 23:28:59 +02:00
self.dateHeaderLabel.font = self.dateHeaderFont;
self.dateHeaderLabel.textAlignment = NSTextAlignmentCenter;
2018-06-28 23:28:59 +02:00
self.dateHeaderLabel.textColor = [UIColor ows_light60Color];
2018-06-28 23:28:59 +02:00
self.dateHeaderView = [[UIStackView alloc] initWithArrangedSubviews:@[
self.dateStrokeView,
self.dateHeaderLabel,
]];
self.dateHeaderView.axis = NSTextLayoutOrientationVertical;
2018-06-25 20:31:09 +02:00
self.avatarView = [[AvatarImageView alloc] init];
[self.avatarView autoSetDimension:ALDimensionWidth toSize:self.avatarSize];
[self.avatarView autoSetDimension:ALDimensionHeight toSize:self.avatarSize];
2018-06-26 00:06:08 +02:00
[self.messageBubbleView autoPinBottomToSuperviewMarginWithInset:0];
2017-10-10 22:13:54 +02:00
2018-03-28 20:46:16 +02:00
self.contentView.userInteractionEnabled = YES;
2018-03-27 19:25:02 +02:00
2018-04-17 19:28:06 +02:00
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self addGestureRecognizer:tap];
2018-03-27 19:25:02 +02:00
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
2018-03-28 20:46:16 +02:00
[self.contentView addGestureRecognizer:longPress];
2018-03-27 19:25:02 +02:00
PanDirectionGestureRecognizer *panGesture = [[PanDirectionGestureRecognizer alloc]
2018-06-29 23:00:22 +02:00
initWithDirection:(CurrentAppContext().isRTL ? PanDirectionLeft : PanDirectionRight)
target:self
2018-03-27 19:25:02 +02:00
action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
2017-11-17 16:49:34 +01:00
}
2018-06-25 20:31:09 +02:00
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2018-06-25 21:20:17 +02:00
- (void)setConversationStyle:(nullable ConversationStyle *)conversationStyle
2018-06-22 19:48:23 +02:00
{
2018-06-25 21:20:17 +02:00
[super setConversationStyle:conversationStyle];
2018-06-22 19:48:23 +02:00
2018-06-25 21:20:17 +02:00
self.messageBubbleView.conversationStyle = conversationStyle;
2018-06-22 19:48:23 +02:00
}
2017-10-17 06:05:29 +02:00
+ (NSString *)cellReuseIdentifier
{
return NSStringFromClass([self class]);
}
2018-04-05 19:19:13 +02:00
#pragma mark - Convenience Accessors
2017-10-10 22:13:54 +02:00
- (OWSMessageCellType)cellType
{
return self.viewItem.messageCellType;
}
2017-10-17 06:05:29 +02:00
- (TSMessage *)message
{
OWSAssert([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
return (TSMessage *)self.viewItem.interaction;
}
2018-04-05 19:19:13 +02:00
- (BOOL)isIncoming
{
return self.viewItem.interaction.interactionType == OWSInteractionType_IncomingMessage;
}
- (BOOL)isOutgoing
{
return self.viewItem.interaction.interactionType == OWSInteractionType_OutgoingMessage;
}
2018-07-05 16:25:30 +02:00
- (BOOL)shouldHavesendFailureBadge
{
if (![self.viewItem.interaction isKindOfClass:[TSOutgoingMessage class]]) {
return NO;
}
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)self.viewItem.interaction;
return outgoingMessage.messageState == TSOutgoingMessageStateFailed;
}
2017-11-17 16:49:34 +01:00
#pragma mark - Load
- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction
2017-10-10 22:13:54 +02:00
{
2018-06-25 21:20:17 +02:00
OWSAssert(self.conversationStyle);
2017-10-10 22:13:54 +02:00
OWSAssert(self.viewItem);
OWSAssert(self.viewItem.interaction);
OWSAssert([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
2018-04-05 19:19:13 +02:00
OWSAssert(self.messageBubbleView);
2018-03-28 19:34:33 +02:00
2018-04-05 19:19:13 +02:00
self.messageBubbleView.viewItem = self.viewItem;
self.messageBubbleView.cellMediaCache = self.delegate.cellMediaCache;
[self.messageBubbleView configureViews];
[self.messageBubbleView loadContent];
2018-03-28 16:01:01 +02:00
2018-04-16 17:46:20 +02:00
// Update label fonts to honor dynamic type size.
2018-06-28 23:28:59 +02:00
self.dateHeaderLabel.font = self.dateHeaderFont;
2018-04-16 17:46:20 +02:00
2018-06-22 20:53:33 +02:00
if (self.isIncoming) {
2018-03-28 16:11:01 +02:00
[self.viewConstraints addObjectsFromArray:@[
2018-06-25 21:20:17 +02:00
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeLeading
withInset:self.conversationStyle.gutterLeading],
2018-06-22 20:53:33 +02:00
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeTrailing
2018-06-25 21:20:17 +02:00
withInset:self.conversationStyle.gutterTrailing
2018-06-22 20:53:33 +02:00
relation:NSLayoutRelationGreaterThanOrEqual],
2018-03-28 16:11:01 +02:00
]];
} else {
2018-07-05 16:25:30 +02:00
if (self.shouldHavesendFailureBadge) {
self.sendFailureBadgeView = [UIImageView new];
self.sendFailureBadgeView.image =
[self.sendFailureBadge imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.sendFailureBadgeView.tintColor = [UIColor ows_destructiveRedColor];
[self.contentView addSubview:self.sendFailureBadgeView];
CGFloat sendFailureBadgeBottomMargin
= round(self.conversationStyle.lastTextLineAxis - self.sendFailureBadgeSize * 0.5f);
[self.viewConstraints addObjectsFromArray:@[
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeLeading
withInset:self.conversationStyle.gutterLeading
relation:NSLayoutRelationGreaterThanOrEqual],
[self.sendFailureBadgeView autoPinLeadingToTrailingEdgeOfView:self.messageBubbleView
offset:self.sendFailureBadgeSpacing],
// V-align the "send failure" badge with the
// last line of the text (if any, or where it
// would be).
[self.messageBubbleView autoPinEdge:ALEdgeBottom
toEdge:ALEdgeBottom
ofView:self.sendFailureBadgeView
withOffset:sendFailureBadgeBottomMargin],
[self.sendFailureBadgeView autoPinEdgeToSuperviewEdge:ALEdgeTrailing
2018-07-05 16:28:47 +02:00
withInset:self.conversationStyle.errorGutterTrailing],
2018-07-05 16:25:30 +02:00
[self.sendFailureBadgeView autoSetDimension:ALDimensionWidth toSize:self.sendFailureBadgeSize],
[self.sendFailureBadgeView autoSetDimension:ALDimensionHeight toSize:self.sendFailureBadgeSize],
]];
} else {
[self.viewConstraints addObjectsFromArray:@[
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeLeading
withInset:self.conversationStyle.gutterLeading
relation:NSLayoutRelationGreaterThanOrEqual],
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeTrailing
withInset:self.conversationStyle.gutterTrailing],
]];
}
}
[self updateDateHeader];
2018-06-25 20:31:09 +02:00
if ([self updateAvatarView]) {
2018-06-25 21:20:17 +02:00
CGFloat avatarBottomMargin = round(self.conversationStyle.lastTextLineAxis - self.avatarSize * 0.5f);
2018-06-25 20:31:09 +02:00
[self.viewConstraints addObjectsFromArray:@[
2018-06-25 21:09:45 +02:00
// V-align the "group sender" avatar with the
// last line of the text (if any, or where it
// would be).
2018-06-25 20:31:09 +02:00
[self.messageBubbleView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:8],
[self.messageBubbleView autoPinEdge:ALEdgeBottom
toEdge:ALEdgeBottom
ofView:self.avatarView
withOffset:avatarBottomMargin],
]];
}
}
2018-07-05 16:25:30 +02:00
- (UIImage *)sendFailureBadge
{
UIImage *image = [UIImage imageNamed:@"message_send_failed"];
OWSAssert(image);
OWSAssert(image.size.width == self.sendFailureBadgeSize && image.size.height == self.sendFailureBadgeSize);
return image;
}
- (CGFloat)sendFailureBadgeSize
{
return 20.f;
}
- (CGFloat)sendFailureBadgeSpacing
{
return 8.f;
}
2017-11-17 16:49:34 +01:00
// * If cell is visible, lazy-load (expensive) view contents.
// * If cell is not visible, eagerly unload view contents.
- (void)ensureMediaLoadState
{
2018-04-05 19:19:13 +02:00
OWSAssert(self.messageBubbleView);
if (!self.isCellVisible) {
2018-04-05 19:19:13 +02:00
[self.messageBubbleView unloadContent];
2017-11-17 16:49:34 +01:00
} else {
2018-04-05 19:19:13 +02:00
[self.messageBubbleView loadContent];
}
}
- (void)updateDateHeader
{
2018-06-25 21:20:17 +02:00
OWSAssert(self.conversationStyle);
static NSDateFormatter *dateHeaderDateFormatter = nil;
static NSDateFormatter *dateHeaderTimeFormatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateHeaderDateFormatter = [NSDateFormatter new];
[dateHeaderDateFormatter setLocale:[NSLocale currentLocale]];
[dateHeaderDateFormatter setDoesRelativeDateFormatting:YES];
[dateHeaderDateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateHeaderDateFormatter setTimeStyle:NSDateFormatterNoStyle];
dateHeaderTimeFormatter = [NSDateFormatter new];
[dateHeaderTimeFormatter setLocale:[NSLocale currentLocale]];
[dateHeaderTimeFormatter setDoesRelativeDateFormatting:YES];
[dateHeaderTimeFormatter setDateStyle:NSDateFormatterNoStyle];
[dateHeaderTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
});
if (self.viewItem.shouldShowDate) {
NSDate *date = self.viewItem.interaction.dateForSorting;
NSString *dateString = [dateHeaderDateFormatter stringFromDate:date];
NSString *timeString = [dateHeaderTimeFormatter stringFromDate:date];
NSAttributedString *attributedText = [NSAttributedString new];
attributedText = [attributedText rtlSafeAppend:dateString.uppercaseString
attributes:@{
2018-06-28 23:28:59 +02:00
NSFontAttributeName : self.dateHeaderFont,
NSForegroundColorAttributeName : [UIColor lightGrayColor],
2018-06-29 23:00:22 +02:00
}];
attributedText = [attributedText rtlSafeAppend:@" "
attributes:@{
2018-06-28 23:28:59 +02:00
NSFontAttributeName : self.dateHeaderFont,
2018-06-29 23:00:22 +02:00
}];
attributedText = [attributedText rtlSafeAppend:timeString
attributes:@{
2018-06-28 23:28:59 +02:00
NSFontAttributeName : self.dateHeaderFont,
NSForegroundColorAttributeName : [UIColor lightGrayColor],
2018-06-29 23:00:22 +02:00
}];
self.dateHeaderLabel.attributedText = attributedText;
[self.contentView addSubview:self.dateHeaderView];
2018-03-28 16:11:01 +02:00
[self.viewConstraints addObjectsFromArray:@[
[self.dateHeaderView autoPinLeadingToSuperviewMarginWithInset:self.conversationStyle.gutterLeading],
[self.dateHeaderView autoPinTrailingToSuperviewMarginWithInset:self.conversationStyle.gutterTrailing],
[self.dateHeaderView autoPinEdgeToSuperviewEdge:ALEdgeTop],
2018-06-29 18:49:23 +02:00
// DO NOT pin to the bottom of dateHeaderView.
//
// Being a UIStackView, it doesn't reflect the spacing below the date
// header contents. Instead pin using dateHeaderHeight which includes
// the spacing.
2018-06-28 23:28:59 +02:00
[self.messageBubbleView autoPinEdge:ALEdgeTop
toEdge:ALEdgeTop
ofView:self.dateHeaderView
withOffset:self.dateHeaderHeight],
2018-03-28 16:11:01 +02:00
]];
} else {
2018-03-28 16:11:01 +02:00
[self.viewConstraints addObjectsFromArray:@[
[self.messageBubbleView autoPinEdgeToSuperviewEdge:ALEdgeTop],
2018-03-28 16:11:01 +02:00
]];
}
}
2018-06-28 23:28:59 +02:00
- (UIFont *)dateHeaderFont
{
return UIFont.ows_dynamicTypeCaption1Font;
}
2018-06-25 20:31:09 +02:00
#pragma mark - Avatar
// Returns YES IFF the avatar view is appropriate and configured.
- (BOOL)updateAvatarView
{
2018-06-26 22:04:09 +02:00
if (!self.viewItem.shouldShowSenderAvatar) {
2018-06-25 20:31:09 +02:00
return NO;
}
2018-06-26 22:04:09 +02:00
if (!self.viewItem.isGroupThread) {
OWSFail(@"%@ not a group thread.", self.logTag);
2018-06-25 20:31:09 +02:00
return NO;
}
2018-06-26 22:04:09 +02:00
if (self.viewItem.interaction.interactionType != OWSInteractionType_IncomingMessage) {
OWSFail(@"%@ not an incoming message.", self.logTag);
2018-06-25 20:31:09 +02:00
return NO;
}
OWSContactsManager *contactsManager = self.delegate.contactsManager;
if (contactsManager == nil) {
OWSFail(@"%@ contactsManager should not be nil", self.logTag);
return NO;
}
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)self.viewItem.interaction;
OWSAvatarBuilder *avatarBuilder = [[OWSContactAvatarBuilder alloc] initWithSignalId:incomingMessage.authorId
2018-06-28 19:28:14 +02:00
color:self.conversationStyle.primaryColor
2018-06-25 20:31:09 +02:00
diameter:self.avatarSize
contactsManager:contactsManager];
self.avatarView.image = [avatarBuilder build];
[self.contentView addSubview:self.avatarView];
2018-06-25 20:31:09 +02:00
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(otherUsersProfileDidChange:)
name:kNSNotificationName_OtherUsersProfileDidChange
object:nil];
return YES;
}
- (NSUInteger)avatarSize
{
return 24.f;
}
- (void)otherUsersProfileDidChange:(NSNotification *)notification
{
OWSAssertIsOnMainThread();
2018-06-26 22:04:09 +02:00
if (!self.viewItem.shouldShowSenderAvatar) {
2018-06-25 20:31:09 +02:00
return;
}
2018-06-26 22:04:09 +02:00
if (!self.viewItem.isGroupThread) {
OWSFail(@"%@ not a group thread.", self.logTag);
2018-06-25 20:31:09 +02:00
return;
}
2018-06-26 22:04:09 +02:00
if (self.viewItem.interaction.interactionType != OWSInteractionType_IncomingMessage) {
OWSFail(@"%@ not an incoming message.", self.logTag);
2018-06-25 20:31:09 +02:00
return;
}
NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
if (recipientId.length == 0) {
return;
}
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)self.viewItem.interaction;
if (![incomingMessage.authorId isEqualToString:recipientId]) {
return;
}
[self updateAvatarView];
}
2017-11-17 16:49:34 +01:00
#pragma mark - Measurement
- (CGSize)cellSizeWithTransaction:(YapDatabaseReadTransaction *)transaction
{
2018-06-25 21:20:17 +02:00
OWSAssert(self.conversationStyle);
OWSAssert(self.conversationStyle.viewWidth > 0);
OWSAssert(self.viewItem);
OWSAssert([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
2018-04-05 19:19:13 +02:00
OWSAssert(self.messageBubbleView);
2018-04-05 19:19:13 +02:00
self.messageBubbleView.viewItem = self.viewItem;
self.messageBubbleView.cellMediaCache = self.delegate.cellMediaCache;
2018-06-22 19:48:23 +02:00
CGSize messageBubbleSize = [self.messageBubbleView measureSize];
2018-04-03 18:35:43 +02:00
2018-04-05 19:19:13 +02:00
CGSize cellSize = messageBubbleSize;
OWSAssert(cellSize.width > 0 && cellSize.height > 0);
cellSize.height += self.dateHeaderHeight;
2018-07-05 16:25:30 +02:00
if (self.shouldHavesendFailureBadge) {
cellSize.width += self.sendFailureBadgeSize + self.sendFailureBadgeSpacing;
}
2018-03-28 19:34:33 +02:00
cellSize = CGSizeCeil(cellSize);
return cellSize;
}
2018-06-28 23:28:59 +02:00
- (CGFloat)dateHeaderStrokeThickness
{
2018-06-29 18:49:23 +02:00
return CGHairlineWidth();
2018-06-28 23:28:59 +02:00
}
2018-06-28 19:55:04 +02:00
- (CGFloat)dateHeaderBottomMargin
{
2018-06-28 23:28:59 +02:00
return 20.f;
}
- (CGFloat)dateHeaderHeight
{
if (self.viewItem.shouldShowDate) {
2018-06-28 23:28:59 +02:00
CGFloat textHeight = self.dateHeaderFont.capHeight;
return (CGFloat)ceil(self.dateHeaderStrokeThickness + textHeight + self.dateHeaderBottomMargin);
} else {
return 0.f;
}
2017-10-10 22:13:54 +02:00
}
2018-06-25 20:31:09 +02:00
#pragma mark - Reuse
2017-11-17 16:49:34 +01:00
2017-10-10 22:13:54 +02:00
- (void)prepareForReuse
{
[super prepareForReuse];
2018-03-28 16:11:01 +02:00
[NSLayoutConstraint deactivateConstraints:self.viewConstraints];
self.viewConstraints = [NSMutableArray new];
2017-10-10 22:13:54 +02:00
2018-04-05 19:19:13 +02:00
[self.messageBubbleView prepareForReuse];
[self.messageBubbleView unloadContent];
[self.dateHeaderView removeFromSuperview];
2018-06-25 20:31:09 +02:00
self.avatarView.image = nil;
[self.avatarView removeFromSuperview];
2017-11-17 16:49:34 +01:00
2018-07-05 16:25:30 +02:00
[self.sendFailureBadgeView removeFromSuperview];
self.sendFailureBadgeView = nil;
[self hideMenuControllerIfNecessary];
2018-06-25 20:31:09 +02:00
[[NSNotificationCenter defaultCenter] removeObserver:self];
2017-10-10 22:13:54 +02:00
}
#pragma mark - Notifications
- (void)setIsCellVisible:(BOOL)isCellVisible {
2017-10-17 06:05:29 +02:00
BOOL didChange = self.isCellVisible != isCellVisible;
[super setIsCellVisible:isCellVisible];
if (!didChange) {
return;
}
2017-10-17 06:05:29 +02:00
2017-11-17 16:49:34 +01:00
[self ensureMediaLoadState];
2018-06-26 00:06:08 +02:00
if (!isCellVisible) {
[self hideMenuControllerIfNecessary];
}
}
2017-10-10 22:13:54 +02:00
#pragma mark - Gesture recognizers
2018-04-17 19:28:06 +02:00
- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
OWSAssert(self.delegate);
if (sender.state != UIGestureRecognizerStateRecognized) {
DDLogVerbose(@"%@ Ignoring tap on message: %@", self.logTag, self.viewItem.interaction.debugDescription);
return;
}
if (self.viewItem.interaction.interactionType == OWSInteractionType_OutgoingMessage) {
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)self.viewItem.interaction;
2018-04-24 21:49:08 +02:00
if (outgoingMessage.messageState == TSOutgoingMessageStateFailed) {
2018-04-17 19:28:06 +02:00
[self.delegate didTapFailedOutgoingMessage:outgoingMessage];
return;
2018-04-24 21:49:08 +02:00
} else if (outgoingMessage.messageState == TSOutgoingMessageStateSending) {
2018-04-17 19:28:06 +02:00
// Ignore taps on outgoing messages being sent.
return;
}
}
[self.messageBubbleView handleTapGesture:sender];
}
2018-03-27 19:25:02 +02:00
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)sender
{
OWSAssert(self.delegate);
if (sender.state != UIGestureRecognizerStateBegan) {
return;
}
2018-04-05 19:19:13 +02:00
if (self.viewItem.interaction.interactionType == OWSInteractionType_OutgoingMessage) {
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)self.viewItem.interaction;
2018-04-23 16:30:51 +02:00
if (outgoingMessage.messageState == TSOutgoingMessageStateFailed) {
2018-04-05 19:19:13 +02:00
// Ignore long press on unsent messages.
return;
2018-04-23 16:30:51 +02:00
} else if (outgoingMessage.messageState == TSOutgoingMessageStateSending) {
2018-04-05 19:19:13 +02:00
// Ignore long press on outgoing messages being sent.
2018-03-27 19:25:02 +02:00
return;
}
}
2018-04-05 19:19:13 +02:00
CGPoint locationInMessageBubble = [sender locationInView:self.messageBubbleView];
switch ([self.messageBubbleView gestureLocationForLocation:locationInMessageBubble]) {
case OWSMessageGestureLocation_Default:
case OWSMessageGestureLocation_OversizeText: {
CGPoint location = [sender locationInView:self];
[self showTextMenuController:location];
break;
}
case OWSMessageGestureLocation_Media: {
CGPoint location = [sender locationInView:self];
[self showMediaMenuController:location];
break;
}
2018-04-11 17:25:28 +02:00
case OWSMessageGestureLocation_QuotedReply: {
CGPoint location = [sender locationInView:self];
[self showDefaultMenuController:location];
2018-04-05 19:19:13 +02:00
break;
2018-04-11 17:25:28 +02:00
}
2017-10-10 22:13:54 +02:00
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)panRecognizer
{
OWSAssert(self.delegate);
[self.delegate didPanWithGestureRecognizer:panRecognizer viewItem:self.viewItem];
}
2017-10-10 22:13:54 +02:00
#pragma mark - UIMenuController
- (void)showTextMenuController:(CGPoint)fromLocation
{
2018-04-11 17:25:28 +02:00
[self showMenuController:fromLocation menuItems:self.viewItem.textMenuControllerItems];
}
2018-04-11 17:25:28 +02:00
- (void)showMediaMenuController:(CGPoint)fromLocation
{
[self showMenuController:fromLocation menuItems:self.viewItem.mediaMenuControllerItems];
}
2018-04-11 17:25:28 +02:00
- (void)showDefaultMenuController:(CGPoint)fromLocation
{
[self showMenuController:fromLocation menuItems:self.viewItem.defaultMenuControllerItems];
}
2018-04-11 17:25:28 +02:00
- (void)showMenuController:(CGPoint)fromLocation menuItems:(NSArray *)menuItems
2017-10-10 22:13:54 +02:00
{
2018-04-11 17:25:28 +02:00
if (menuItems.count < 1) {
OWSFail(@"%@ No menu items to present.", self.logTag);
return;
}
// We don't want taps on messages to hide the keyboard,
// so we only let messages become first responder
// while they are trying to present the menu controller.
self.isPresentingMenuController = YES;
2017-10-10 22:13:54 +02:00
[self becomeFirstResponder];
if ([UIMenuController sharedMenuController].isMenuVisible) {
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}
// We use custom action selectors so that we can control
// the ordering of the actions in the menu.
[UIMenuController sharedMenuController].menuItems = menuItems;
CGRect targetRect = CGRectMake(fromLocation.x, fromLocation.y, 1, 1);
[[UIMenuController sharedMenuController] setTargetRect:targetRect inView:self];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender
{
return [self.viewItem canPerformAction:action];
}
- (void)copyTextAction:(nullable id)sender
{
[self.viewItem copyTextAction];
}
- (void)copyMediaAction:(nullable id)sender
{
[self.viewItem copyMediaAction];
}
- (void)shareTextAction:(nullable id)sender
2017-10-10 22:13:54 +02:00
{
[self.viewItem shareTextAction];
2017-10-10 22:13:54 +02:00
}
- (void)shareMediaAction:(nullable id)sender
2017-10-10 22:13:54 +02:00
{
[self.viewItem shareMediaAction];
2017-10-10 22:13:54 +02:00
}
- (void)saveMediaAction:(nullable id)sender
2017-10-10 22:13:54 +02:00
{
[self.viewItem saveMediaAction];
2017-10-10 22:13:54 +02:00
}
- (void)deleteAction:(nullable id)sender
{
[self.viewItem deleteAction];
}
- (void)metadataAction:(nullable id)sender
{
OWSAssert([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
[self.delegate showMetadataViewForViewItem:self.viewItem];
2017-10-10 22:13:54 +02:00
}
- (void)replyAction:(nullable id)sender
{
OWSAssert([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
[self.delegate conversationCell:self didTapReplyForViewItem:self.viewItem];
}
2017-10-10 22:13:54 +02:00
- (BOOL)canBecomeFirstResponder
{
return self.isPresentingMenuController;
}
- (void)didHideMenuController:(NSNotification *)notification
{
self.isPresentingMenuController = NO;
}
- (void)setIsPresentingMenuController:(BOOL)isPresentingMenuController
{
if (_isPresentingMenuController == isPresentingMenuController) {
return;
}
_isPresentingMenuController = isPresentingMenuController;
if (isPresentingMenuController) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didHideMenuController:)
name:UIMenuControllerDidHideMenuNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIMenuControllerDidHideMenuNotification
object:nil];
}
}
- (void)hideMenuControllerIfNecessary
{
if (self.isPresentingMenuController) {
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}
self.isPresentingMenuController = NO;
}
2017-10-10 22:13:54 +02:00
@end
NS_ASSUME_NONNULL_END