2014-10-29 21:58:58 +01:00
|
|
|
//
|
|
|
|
// MessagesViewController.m
|
|
|
|
// Signal
|
|
|
|
//
|
|
|
|
// Created by Dylan Bourgeois on 28/10/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
2015-02-17 00:14:50 +01:00
|
|
|
#import <AddressBookUI/AddressBookUI.h>
|
2015-12-22 12:45:09 +01:00
|
|
|
#import <AssetsLibrary/AssetsLibrary.h>
|
2015-10-31 16:53:32 +01:00
|
|
|
#import <ContactsUI/CNContactViewController.h>
|
2015-12-22 12:45:09 +01:00
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
2016-04-08 09:47:41 +02:00
|
|
|
#import <SignalServiceKit/TSAccountManager.h>
|
2015-12-22 12:45:09 +01:00
|
|
|
#import <YapDatabase/YapDatabaseView.h>
|
2016-06-27 23:32:35 +02:00
|
|
|
#import "OWSContactsManager.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "DJWActionSheet+OWS.h"
|
|
|
|
#import "Environment.h"
|
2014-12-04 00:23:36 +01:00
|
|
|
#import "FingerprintViewController.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "FullImageViewController.h"
|
|
|
|
#import "JSQCallCollectionViewCell.h"
|
2016-07-09 00:25:28 +02:00
|
|
|
#import "JSQDisplayedMessageCollectionViewCell.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "MessagesViewController.h"
|
|
|
|
#import "NSDate+millisecondTimeStamp.h"
|
2014-12-17 06:44:36 +01:00
|
|
|
#import "NewGroupViewController.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "PhoneManager.h"
|
|
|
|
#import "PreferencesUtil.h"
|
2014-12-24 02:25:10 +01:00
|
|
|
#import "ShowGroupMembersViewController.h"
|
|
|
|
#import "SignalKeyingStorage.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSAttachmentPointer.h"
|
|
|
|
#import "TSContentAdapters.h"
|
2014-11-25 16:38:33 +01:00
|
|
|
#import "TSDatabaseView.h"
|
2016-07-09 00:25:28 +02:00
|
|
|
#import "OWSMessagesBubblesSizeCalculator.h"
|
|
|
|
//TODO should JSQInfoMessage be rolled into JSQDisplayedMessageCollectionViewCell?
|
|
|
|
#import "JSQInfoMessage.h"
|
|
|
|
#import "TSInfoMessage.h"
|
|
|
|
//TODO should JSQErrorMessage be rolled into JSQDisplayedMessageCollectionViewCell?
|
|
|
|
#import "JSQErrorMessage.h"
|
2014-12-11 00:05:41 +01:00
|
|
|
#import "TSErrorMessage.h"
|
2016-07-09 00:25:28 +02:00
|
|
|
//TODO should JSQCall be rolled into JSQCallCollectionViewCell?
|
|
|
|
#import "JSQCall.h"
|
|
|
|
#import "TSCall.h"
|
2014-12-06 17:45:42 +01:00
|
|
|
#import "TSIncomingMessage.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSInvalidIdentityKeyErrorMessage.h"
|
2014-12-22 00:40:15 +01:00
|
|
|
#import "TSMessagesManager+attachments.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSMessagesManager+sendMessages.h"
|
|
|
|
#import "UIButton+OWS.h"
|
2015-12-26 17:27:27 +01:00
|
|
|
#import "UIFont+OWS.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "UIUtil.h"
|
2014-12-31 13:22:40 +01:00
|
|
|
|
|
|
|
#define kYapDatabaseRangeLength 50
|
|
|
|
#define kYapDatabaseRangeMaxLength 300
|
|
|
|
#define kYapDatabaseRangeMinLength 20
|
2015-01-27 21:17:49 +01:00
|
|
|
#define JSQ_TOOLBAR_ICON_HEIGHT 22
|
|
|
|
#define JSQ_TOOLBAR_ICON_WIDTH 22
|
|
|
|
#define JSQ_IMAGE_INSET 5
|
2014-12-31 13:22:40 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
static NSTimeInterval const kTSMessageSentDateShowTimeInterval = 5 * 60;
|
2015-12-22 12:45:09 +01:00
|
|
|
static NSString *const kUpdateGroupSegueIdentifier = @"updateGroupSegue";
|
|
|
|
static NSString *const kFingerprintSegueIdentifier = @"fingerprintSegue";
|
|
|
|
static NSString *const kShowGroupMembersSegue = @"showGroupMembersSegue";
|
2014-12-17 06:44:36 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
typedef enum : NSUInteger {
|
|
|
|
kMediaTypePicture,
|
|
|
|
kMediaTypeVideo,
|
|
|
|
} kMediaTypes;
|
|
|
|
|
|
|
|
@interface MessagesViewController () {
|
2015-12-22 12:45:09 +01:00
|
|
|
UIImage *tappedImage;
|
2014-10-29 21:58:58 +01:00
|
|
|
BOOL isGroupConversation;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
UIView *_unreadContainer;
|
|
|
|
UIImageView *_unreadBackground;
|
|
|
|
UILabel *_unreadLabel;
|
|
|
|
NSUInteger _unreadCount;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
@property (nonatomic, readwrite) TSThread *thread;
|
2015-12-22 12:45:09 +01:00
|
|
|
@property (nonatomic, weak) UIView *navView;
|
|
|
|
@property (nonatomic, strong) YapDatabaseConnection *editingDatabaseConnection;
|
|
|
|
@property (nonatomic, strong) YapDatabaseConnection *uiDatabaseConnection;
|
2014-11-25 16:38:33 +01:00
|
|
|
@property (nonatomic, strong) YapDatabaseViewMappings *messageMappings;
|
2015-12-22 12:45:09 +01:00
|
|
|
@property (nonatomic, retain) JSQMessagesBubbleImage *outgoingBubbleImageData;
|
|
|
|
@property (nonatomic, retain) JSQMessagesBubbleImage *incomingBubbleImageData;
|
|
|
|
@property (nonatomic, retain) JSQMessagesBubbleImage *currentlyOutgoingBubbleImageData;
|
|
|
|
@property (nonatomic, retain) JSQMessagesBubbleImage *outgoingMessageFailedImageData;
|
2015-01-22 05:08:12 +01:00
|
|
|
@property (nonatomic, strong) NSTimer *audioPlayerPoller;
|
2015-01-25 00:48:40 +01:00
|
|
|
@property (nonatomic, strong) TSVideoAttachmentAdapter *currentMediaAdapter;
|
2014-12-06 17:45:42 +01:00
|
|
|
|
|
|
|
@property (nonatomic, retain) NSTimer *readTimer;
|
2015-01-27 02:20:11 +01:00
|
|
|
@property (nonatomic, retain) UIButton *messageButton;
|
|
|
|
@property (nonatomic, retain) UIButton *attachButton;
|
2014-11-25 16:38:33 +01:00
|
|
|
|
2014-12-06 23:21:15 +01:00
|
|
|
@property (nonatomic, retain) NSIndexPath *lastDeliveredMessageIndexPath;
|
2015-01-27 21:17:49 +01:00
|
|
|
@property (nonatomic, retain) UIGestureRecognizer *showFingerprintDisplay;
|
|
|
|
@property (nonatomic, retain) UITapGestureRecognizer *toggleContactPhoneDisplay;
|
|
|
|
@property (nonatomic) BOOL displayPhoneAsTitle;
|
2014-12-06 23:21:15 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
@property NSUInteger page;
|
2015-12-22 12:45:09 +01:00
|
|
|
@property (nonatomic) BOOL composeOnOpen;
|
|
|
|
@property (nonatomic) BOOL peek;
|
2015-01-31 12:00:58 +01:00
|
|
|
|
2016-04-13 19:05:09 +02:00
|
|
|
@property NSCache *messageAdapterCache;
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@end
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@interface UINavigationItem () {
|
2015-05-23 15:54:50 +02:00
|
|
|
UIView *backButtonView;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@implementation MessagesViewController
|
|
|
|
|
2016-06-17 19:45:48 +02:00
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
- (void)peekSetup {
|
|
|
|
_peek = YES;
|
|
|
|
[self setComposeOnOpen:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)popped {
|
|
|
|
_peek = NO;
|
|
|
|
[self hideInputIfNeeded];
|
|
|
|
}
|
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
- (void)configureForThread:(TSThread *)thread keyboardOnViewAppearing:(BOOL)keyboardAppearing {
|
|
|
|
_thread = thread;
|
|
|
|
isGroupConversation = [self.thread isKindOfClass:[TSGroupThread class]];
|
|
|
|
_composeOnOpen = keyboardAppearing;
|
|
|
|
_lastDeliveredMessageIndexPath = nil;
|
2014-11-26 16:00:10 +01:00
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
[self.uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
self.messageMappings =
|
|
|
|
[[YapDatabaseViewMappings alloc] initWithGroups:@[ thread.uniqueId ] view:TSMessageDatabaseViewExtensionName];
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
[self.messageMappings updateWithTransaction:transaction];
|
|
|
|
self.page = 0;
|
|
|
|
[self updateRangeOptionsForPage:self.page];
|
|
|
|
[self markAllMessagesAsRead];
|
|
|
|
[self.collectionView reloadData];
|
|
|
|
}];
|
2016-04-27 03:57:55 +02:00
|
|
|
[self updateLoadEarlierVisible];
|
2015-05-23 15:54:50 +02:00
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
|
2015-03-01 00:04:39 +01:00
|
|
|
- (void)hideInputIfNeeded {
|
2015-10-31 16:53:32 +01:00
|
|
|
if (_peek) {
|
|
|
|
[self inputToolbar].hidden = YES;
|
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if ([_thread isKindOfClass:[TSGroupThread class]] &&
|
|
|
|
![((TSGroupThread *)_thread).groupModel.groupMemberIds containsObject:[TSAccountManager localNumber]]) {
|
2016-05-17 18:38:49 +02:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
[self inputToolbar].hidden = YES; // user has requested they leave the group. further sends disallowed
|
2015-02-10 17:26:12 +01:00
|
|
|
self.navigationItem.rightBarButtonItem = nil; // further group action disallowed
|
2015-03-01 00:04:39 +01:00
|
|
|
} else {
|
2015-12-22 12:45:09 +01:00
|
|
|
[self inputToolbar].hidden = NO;
|
2015-03-01 00:04:39 +01:00
|
|
|
[self loadDraftInCompose];
|
2015-01-27 02:20:11 +01:00
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2015-01-31 12:00:58 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
- (void)viewDidLoad {
|
2015-01-22 05:08:12 +01:00
|
|
|
[super viewDidLoad];
|
2016-07-09 00:25:28 +02:00
|
|
|
// JSQMVC width is 375px at this point (as specified by the xib), but this causes
|
|
|
|
// our initial bubble calculations to be off since they happen before the containing
|
|
|
|
// view is layed out. https://github.com/jessesquires/JSQMessagesViewController/issues/1257
|
|
|
|
// Resetting here makes sure we've got a good initial width.
|
|
|
|
[self resetFrame];
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
[self.navigationController.navigationBar setTranslucent:NO];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-04-13 19:05:09 +02:00
|
|
|
self.messageAdapterCache = [[NSCache alloc] init];
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
_showFingerprintDisplay =
|
|
|
|
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showFingerprint)];
|
|
|
|
|
|
|
|
_toggleContactPhoneDisplay =
|
|
|
|
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleContactPhone)];
|
2015-01-27 21:17:49 +01:00
|
|
|
_toggleContactPhoneDisplay.numberOfTapsRequired = 1;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
_messageButton = [UIButton ows_blueButtonWithTitle:NSLocalizedString(@"SEND_BUTTON_TITLE", @"")];
|
2015-12-26 17:27:27 +01:00
|
|
|
_messageButton.enabled = NO;
|
2015-03-20 14:41:00 +01:00
|
|
|
_messageButton.titleLabel.adjustsFontSizeToFitWidth = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-27 02:20:11 +01:00
|
|
|
_attachButton = [[UIButton alloc] init];
|
2015-12-22 12:45:09 +01:00
|
|
|
[_attachButton setFrame:CGRectMake(0,
|
|
|
|
0,
|
|
|
|
JSQ_TOOLBAR_ICON_WIDTH + JSQ_IMAGE_INSET * 2,
|
|
|
|
JSQ_TOOLBAR_ICON_HEIGHT + JSQ_IMAGE_INSET * 2)];
|
|
|
|
_attachButton.imageEdgeInsets =
|
|
|
|
UIEdgeInsetsMake(JSQ_IMAGE_INSET, JSQ_IMAGE_INSET, JSQ_IMAGE_INSET, JSQ_IMAGE_INSET);
|
2015-01-27 21:17:49 +01:00
|
|
|
[_attachButton setImage:[UIImage imageNamed:@"btnAttachments--blue"] forState:UIControlStateNormal];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
[self initializeBubbles];
|
2015-01-14 22:30:01 +01:00
|
|
|
[self initializeTextView];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-03-11 18:51:37 +01:00
|
|
|
[JSQMessagesCollectionViewCell registerMenuAction:@selector(delete:)];
|
2016-07-09 00:25:28 +02:00
|
|
|
self.collectionView.collectionViewLayout.bubbleSizeCalculator = [[OWSMessagesBubblesSizeCalculator alloc] init];
|
2016-03-11 18:51:37 +01:00
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
[self initializeCollectionViewLayout];
|
2016-07-09 00:25:28 +02:00
|
|
|
[self registerCustomMessageNibs];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
self.senderId = ME_MESSAGE_IDENTIFIER;
|
|
|
|
self.senderDisplayName = ME_MESSAGE_IDENTIFIER;
|
2016-07-09 00:25:28 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
- (void)registerCustomMessageNibs
|
|
|
|
{
|
|
|
|
[self.collectionView registerNib:[JSQCallCollectionViewCell nib]
|
|
|
|
forCellWithReuseIdentifier:[JSQCallCollectionViewCell cellReuseIdentifier]];
|
2016-06-17 19:45:48 +02:00
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
[self.collectionView registerNib:[JSQDisplayedMessageCollectionViewCell nib]
|
|
|
|
forCellWithReuseIdentifier:[JSQDisplayedMessageCollectionViewCell cellReuseIdentifier]];
|
2016-06-17 19:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)toggleObservers:(BOOL)shouldObserve {
|
|
|
|
if (shouldObserve) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
name:YapDatabaseModifiedNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(startReadTimer)
|
|
|
|
name:UIApplicationWillEnterForegroundNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(cancelReadTimer)
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
object:nil];
|
|
|
|
} else {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:YapDatabaseModifiedNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:UIApplicationWillEnterForegroundNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
2014-12-09 20:11:14 +01:00
|
|
|
}
|
|
|
|
|
2015-03-01 00:04:39 +01:00
|
|
|
- (void)initializeTextView {
|
2015-12-26 17:27:27 +01:00
|
|
|
[self.inputToolbar.contentView.textView setFont:[UIFont ows_dynamicTypeBodyFont]];
|
2015-09-01 10:27:28 +02:00
|
|
|
self.inputToolbar.contentView.leftBarButtonItem = _attachButton;
|
|
|
|
self.inputToolbar.contentView.rightBarButtonItem = _messageButton;
|
|
|
|
self.inputToolbar.contentView.textView.layer.cornerRadius = 4.f;
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2014-12-24 11:50:07 +01:00
|
|
|
[super viewWillAppear:animated];
|
2016-06-17 19:45:48 +02:00
|
|
|
|
|
|
|
[self toggleObservers:YES];
|
2015-02-10 12:02:58 +01:00
|
|
|
[self initializeToolbars];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-05 03:15:18 +01:00
|
|
|
NSInteger numberOfMessages = (NSInteger)[self.messageMappings numberOfItemsInGroup:self.thread.uniqueId];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-05 03:15:18 +01:00
|
|
|
if (numberOfMessages > 0) {
|
2015-12-22 12:45:09 +01:00
|
|
|
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForRow:numberOfMessages - 1 inSection:0];
|
|
|
|
[self.collectionView scrollToItemAtIndexPath:lastCellIndexPath
|
|
|
|
atScrollPosition:UICollectionViewScrollPositionBottom
|
|
|
|
animated:NO];
|
2015-01-05 03:15:18 +01:00
|
|
|
}
|
2014-12-24 11:50:07 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)startReadTimer {
|
2015-12-22 12:45:09 +01:00
|
|
|
self.readTimer = [NSTimer scheduledTimerWithTimeInterval:1
|
|
|
|
target:self
|
|
|
|
selector:@selector(markAllMessagesAsRead)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
2014-12-09 20:11:14 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)cancelReadTimer {
|
2014-12-09 20:11:14 +01:00
|
|
|
[self.readTimer invalidate];
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
2014-12-09 20:11:14 +01:00
|
|
|
[super viewDidAppear:animated];
|
2015-12-26 17:27:27 +01:00
|
|
|
[self dismissKeyBoard];
|
2014-12-09 20:11:14 +01:00
|
|
|
[self startReadTimer];
|
2015-12-26 17:27:27 +01:00
|
|
|
|
2015-02-10 12:02:58 +01:00
|
|
|
[self initializeTitleLabelGestureRecognizer];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
[self updateBackButtonAsync];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
[self.inputToolbar.contentView.textView endEditing:YES];
|
|
|
|
|
|
|
|
self.inputToolbar.contentView.textView.editable = YES;
|
2015-04-14 21:49:00 +02:00
|
|
|
if (_composeOnOpen) {
|
|
|
|
[self popKeyBoard];
|
|
|
|
}
|
2014-12-09 20:11:14 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
- (void)updateBackButtonAsync {
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
2015-12-22 12:45:09 +01:00
|
|
|
NSUInteger count = [[TSMessagesManager sharedManager] unreadMessagesCountExcept:self.thread];
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (self) {
|
|
|
|
[self setUnreadCount:count];
|
|
|
|
}
|
|
|
|
});
|
2015-10-31 16:53:32 +01:00
|
|
|
});
|
2015-05-23 15:54:50 +02:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
2015-12-26 17:27:27 +01:00
|
|
|
[super viewWillDisappear:animated];
|
2016-06-17 19:45:48 +02:00
|
|
|
[self toggleObservers:NO];
|
2015-12-26 17:27:27 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
|
2015-01-14 22:30:01 +01:00
|
|
|
// back button was pressed.
|
|
|
|
[self.navController hideDropDown:self];
|
|
|
|
}
|
2015-12-26 17:27:27 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
[_unreadContainer removeFromSuperview];
|
|
|
|
_unreadContainer = nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
[_audioPlayerPoller invalidate];
|
|
|
|
[_audioPlayer stop];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
// reset all audio bars to 0
|
|
|
|
JSQMessagesCollectionView *collectionView = self.collectionView;
|
2015-12-22 12:45:09 +01:00
|
|
|
NSInteger num_bubbles = [self collectionView:collectionView numberOfItemsInSection:0];
|
|
|
|
for (NSInteger i = 0; i < num_bubbles; i++) {
|
2015-01-14 22:30:01 +01:00
|
|
|
NSIndexPath *index_path = [NSIndexPath indexPathForRow:i inSection:0];
|
2015-12-22 12:45:09 +01:00
|
|
|
TSMessageAdapter *msgAdapter =
|
|
|
|
[collectionView.dataSource collectionView:collectionView messageDataForItemAtIndexPath:index_path];
|
|
|
|
if (msgAdapter.messageType == TSIncomingMessageAdapter && msgAdapter.isMediaMessage &&
|
|
|
|
[msgAdapter isKindOfClass:[TSVideoAttachmentAdapter class]]) {
|
|
|
|
TSVideoAttachmentAdapter *msgMedia = (TSVideoAttachmentAdapter *)[msgAdapter media];
|
2015-01-14 22:30:01 +01:00
|
|
|
if ([msgMedia isAudio]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
msgMedia.isPaused = NO;
|
2015-01-14 22:30:01 +01:00
|
|
|
msgMedia.isAudioPlaying = NO;
|
|
|
|
[msgMedia setAudioProgressFromFloat:0];
|
|
|
|
[msgMedia setAudioIconToPlay];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-09 20:11:14 +01:00
|
|
|
[self cancelReadTimer];
|
2015-02-17 00:14:50 +01:00
|
|
|
[self removeTitleLabelGestureRecognizer];
|
2015-03-01 00:04:39 +01:00
|
|
|
[self saveDraft];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)viewDidDisappear:(BOOL)animated {
|
2015-12-26 17:27:27 +01:00
|
|
|
[super viewDidDisappear:animated];
|
|
|
|
self.inputToolbar.contentView.textView.editable = NO;
|
2015-01-31 12:00:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
#pragma mark - Initiliazers
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
|
|
|
|
- (IBAction)didSelectShow:(id)sender {
|
2015-01-27 02:20:11 +01:00
|
|
|
if (isGroupConversation) {
|
2015-12-22 12:45:09 +01:00
|
|
|
UIBarButtonItem *spaceEdge =
|
|
|
|
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
|
|
|
|
|
2015-01-27 02:20:11 +01:00
|
|
|
spaceEdge.width = 40;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
UIBarButtonItem *spaceMiddleIcons =
|
|
|
|
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
|
2015-01-27 02:20:11 +01:00
|
|
|
spaceMiddleIcons.width = 61;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
UIBarButtonItem *spaceMiddleWords =
|
|
|
|
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
|
|
|
|
target:nil
|
|
|
|
action:nil];
|
|
|
|
|
|
|
|
NSDictionary *buttonTextAttributes = @{
|
|
|
|
NSFontAttributeName : [UIFont ows_regularFontWithSize:15.0f],
|
|
|
|
NSForegroundColorAttributeName : [UIColor ows_materialBlueColor]
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
UIButton *groupUpdateButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 24)];
|
|
|
|
NSMutableAttributedString *updateTitle =
|
|
|
|
[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"UPDATE_BUTTON_TITLE", @"")];
|
2015-01-30 04:31:35 +01:00
|
|
|
[updateTitle setAttributes:buttonTextAttributes range:NSMakeRange(0, [updateTitle length])];
|
|
|
|
[groupUpdateButton setAttributedTitle:updateTitle forState:UIControlStateNormal];
|
|
|
|
[groupUpdateButton addTarget:self action:@selector(updateGroup) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[groupUpdateButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
|
2015-03-21 15:19:42 +01:00
|
|
|
[groupUpdateButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
UIBarButtonItem *groupUpdateBarButton =
|
|
|
|
[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
|
|
|
|
groupUpdateBarButton.customView = groupUpdateButton;
|
2015-01-30 04:31:35 +01:00
|
|
|
groupUpdateBarButton.customView.userInteractionEnabled = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
UIButton *groupLeaveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
|
|
|
|
NSMutableAttributedString *leaveTitle =
|
|
|
|
[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"LEAVE_BUTTON_TITLE", @"")];
|
2015-01-30 04:31:35 +01:00
|
|
|
[leaveTitle setAttributes:buttonTextAttributes range:NSMakeRange(0, [leaveTitle length])];
|
|
|
|
[groupLeaveButton setAttributedTitle:leaveTitle forState:UIControlStateNormal];
|
|
|
|
[groupLeaveButton addTarget:self action:@selector(leaveGroup) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[groupLeaveButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
|
2015-12-22 12:45:09 +01:00
|
|
|
UIBarButtonItem *groupLeaveBarButton =
|
|
|
|
[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
|
|
|
|
groupLeaveBarButton.customView = groupLeaveButton;
|
2015-01-30 04:31:35 +01:00
|
|
|
groupLeaveBarButton.customView.userInteractionEnabled = YES;
|
2015-03-21 15:19:42 +01:00
|
|
|
[groupLeaveButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
UIButton *groupMembersButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 24)];
|
|
|
|
NSMutableAttributedString *membersTitle =
|
|
|
|
[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"MEMBERS_BUTTON_TITLE", @"")];
|
2015-01-30 04:31:35 +01:00
|
|
|
[membersTitle setAttributes:buttonTextAttributes range:NSMakeRange(0, [membersTitle length])];
|
|
|
|
[groupMembersButton setAttributedTitle:membersTitle forState:UIControlStateNormal];
|
2015-12-22 12:45:09 +01:00
|
|
|
[groupMembersButton addTarget:self
|
|
|
|
action:@selector(showGroupMembers)
|
|
|
|
forControlEvents:UIControlEventTouchUpInside];
|
2015-01-30 04:31:35 +01:00
|
|
|
[groupMembersButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
|
2015-12-22 12:45:09 +01:00
|
|
|
UIBarButtonItem *groupMembersBarButton =
|
|
|
|
[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
|
|
|
|
groupMembersBarButton.customView = groupMembersButton;
|
2015-01-30 04:31:35 +01:00
|
|
|
groupMembersBarButton.customView.userInteractionEnabled = YES;
|
2015-03-21 15:19:42 +01:00
|
|
|
[groupMembersButton.titleLabel setAdjustsFontSizeToFitWidth:YES];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
self.navController.dropDownToolbar.items = @[
|
|
|
|
spaceEdge,
|
|
|
|
groupUpdateBarButton,
|
|
|
|
spaceMiddleWords,
|
|
|
|
groupLeaveBarButton,
|
|
|
|
spaceMiddleWords,
|
|
|
|
groupMembersBarButton,
|
|
|
|
spaceEdge
|
|
|
|
];
|
|
|
|
|
|
|
|
for (UIButton *button in self.navController.dropDownToolbar.items) {
|
2015-01-27 02:20:11 +01:00
|
|
|
[button setTintColor:[UIColor ows_materialBlueColor]];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
if (self.navController.isDropDownVisible) {
|
2015-01-27 02:20:11 +01:00
|
|
|
[self.navController hideDropDown:sender];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2015-01-27 02:20:11 +01:00
|
|
|
[self.navController showDropDown:sender];
|
|
|
|
}
|
|
|
|
// Can also toggle toolbar from current state
|
|
|
|
// [self.navController toggleToolbar:sender];
|
|
|
|
[self setNavigationTitle];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)setNavigationTitle {
|
|
|
|
NSString *navTitle = self.thread.name;
|
|
|
|
if (isGroupConversation && [navTitle length] == 0) {
|
2015-02-18 23:21:03 +01:00
|
|
|
navTitle = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"");
|
2015-01-27 02:20:11 +01:00
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
self.navController.activeNavigationBarTitle = nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
self.title = navTitle;
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)initializeToolbars {
|
|
|
|
self.navController = (APNavigationController *)self.navigationController;
|
|
|
|
|
|
|
|
if ([self canCall]) {
|
|
|
|
self.navigationItem.rightBarButtonItem =
|
|
|
|
[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btnPhone--white"]
|
|
|
|
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
|
|
|
|
style:UIBarButtonItemStylePlain
|
|
|
|
target:self
|
|
|
|
action:@selector(callAction)];
|
2015-02-17 00:14:50 +01:00
|
|
|
self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0, -10, 0, 10);
|
2016-06-28 04:51:57 +02:00
|
|
|
} else if ([self.thread isGroupThread]) {
|
2015-12-26 17:27:27 +01:00
|
|
|
self.navigationItem.rightBarButtonItem =
|
|
|
|
[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"contact-options-action"]
|
|
|
|
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
|
|
|
|
style:UIBarButtonItemStylePlain
|
|
|
|
target:self
|
|
|
|
action:@selector(didSelectShow:)];
|
2016-05-07 18:29:10 +02:00
|
|
|
self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(10, 20, 10, 0);
|
2016-06-28 04:51:57 +02:00
|
|
|
} else {
|
|
|
|
self.navigationItem.rightBarButtonItem = nil;
|
|
|
|
DDLogError(@"Thread was neither group thread nor callable");
|
2015-01-27 02:20:11 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-10 12:02:58 +01:00
|
|
|
[self hideInputIfNeeded];
|
|
|
|
[self setNavigationTitle];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initializeTitleLabelGestureRecognizer {
|
2015-12-22 12:45:09 +01:00
|
|
|
if (isGroupConversation) {
|
2015-02-10 12:02:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-10 12:02:58 +01:00
|
|
|
for (UIView *view in self.navigationController.navigationBar.subviews) {
|
|
|
|
if ([view isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
|
2015-02-17 00:14:50 +01:00
|
|
|
self.navView = view;
|
|
|
|
for (UIView *aView in self.navView.subviews) {
|
2015-02-10 12:02:58 +01:00
|
|
|
if ([aView isKindOfClass:[UILabel class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
UILabel *label = (UILabel *)aView;
|
2015-02-17 00:14:50 +01:00
|
|
|
if ([label.text isEqualToString:self.title]) {
|
|
|
|
[self.navView setUserInteractionEnabled:YES];
|
|
|
|
[aView setUserInteractionEnabled:YES];
|
|
|
|
[aView addGestureRecognizer:_showFingerprintDisplay];
|
|
|
|
[aView addGestureRecognizer:_toggleContactPhoneDisplay];
|
|
|
|
return;
|
|
|
|
}
|
2015-02-10 12:02:58 +01:00
|
|
|
}
|
2015-02-17 00:14:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeTitleLabelGestureRecognizer {
|
2015-12-22 12:45:09 +01:00
|
|
|
if (isGroupConversation) {
|
2015-02-17 00:14:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-17 00:14:50 +01:00
|
|
|
for (UIView *aView in self.navView.subviews) {
|
|
|
|
if ([aView isKindOfClass:[UILabel class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
UILabel *label = (UILabel *)aView;
|
2015-02-17 00:14:50 +01:00
|
|
|
if ([label.text isEqualToString:self.title]) {
|
|
|
|
[self.navView setUserInteractionEnabled:NO];
|
|
|
|
[aView setUserInteractionEnabled:NO];
|
|
|
|
[aView removeGestureRecognizer:_showFingerprintDisplay];
|
|
|
|
[aView removeGestureRecognizer:_toggleContactPhoneDisplay];
|
2015-02-10 12:02:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)initializeBubbles {
|
2014-11-29 19:54:33 +01:00
|
|
|
JSQMessagesBubbleImageFactory *bubbleFactory = [[JSQMessagesBubbleImageFactory alloc] init];
|
2016-07-09 00:25:28 +02:00
|
|
|
self.incomingBubbleImageData = [bubbleFactory incomingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleLightGrayColor]];
|
2015-12-22 12:45:09 +01:00
|
|
|
self.outgoingBubbleImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor ows_materialBlueColor]];
|
2016-07-09 00:25:28 +02:00
|
|
|
self.currentlyOutgoingBubbleImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor ows_fadedBlueColor]];
|
|
|
|
self.outgoingMessageFailedImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor grayColor]];
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)initializeCollectionViewLayout {
|
|
|
|
if (self.collectionView) {
|
2015-12-26 17:27:27 +01:00
|
|
|
[self.collectionView.collectionViewLayout setMessageBubbleFont:[UIFont ows_dynamicTypeBodyFont]];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
self.collectionView.showsVerticalScrollIndicator = NO;
|
2014-12-09 22:18:39 +01:00
|
|
|
self.collectionView.showsHorizontalScrollIndicator = NO;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self updateLoadEarlierVisible];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-09 22:18:39 +01:00
|
|
|
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero;
|
|
|
|
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;
|
|
|
|
}
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
#pragma mark - Fingerprints
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)showFingerprint {
|
2014-12-06 17:45:42 +01:00
|
|
|
[self markAllMessagesAsRead];
|
2014-12-17 06:44:36 +01:00
|
|
|
[self performSegueWithIdentifier:kFingerprintSegueIdentifier sender:self];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 00:23:36 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)toggleContactPhone {
|
2015-01-27 21:17:49 +01:00
|
|
|
_displayPhoneAsTitle = !_displayPhoneAsTitle;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-17 00:14:50 +01:00
|
|
|
if (!_thread.isGroupThread) {
|
2015-12-22 12:45:09 +01:00
|
|
|
Contact *contact =
|
|
|
|
[[[Environment getCurrent] contactsManager] latestContactForPhoneNumber:[self phoneNumberForThread]];
|
2015-02-17 00:14:50 +01:00
|
|
|
if (!contact) {
|
2015-10-31 16:53:32 +01:00
|
|
|
if (!(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(NSFoundationVersionNumber_iOS_9))) {
|
|
|
|
ABUnknownPersonViewController *view = [[ABUnknownPersonViewController alloc] init];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
ABRecordRef aContact = ABPersonCreate();
|
2015-12-22 12:45:09 +01:00
|
|
|
CFErrorRef anError = NULL;
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
ABMultiValueAddValueAndLabel(
|
|
|
|
phone, (__bridge CFTypeRef)[self phoneNumberForThread].toE164, kABPersonPhoneMainLabel, NULL);
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);
|
|
|
|
CFRelease(phone);
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
if (!anError && aContact) {
|
2015-12-22 12:45:09 +01:00
|
|
|
view.displayedPerson = aContact; // Assume person is already defined.
|
2015-10-31 16:53:32 +01:00
|
|
|
view.allowsAddingToAddressBook = YES;
|
|
|
|
[self.navigationController pushViewController:view animated:YES];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CNContactStore *contactStore = [Environment getCurrent].contactsManager.contactStore;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
CNMutableContact *cncontact = [[CNMutableContact alloc] init];
|
2015-12-22 12:45:09 +01:00
|
|
|
cncontact.phoneNumbers = @[
|
|
|
|
[CNLabeledValue
|
|
|
|
labeledValueWithLabel:nil
|
|
|
|
value:[CNPhoneNumber
|
|
|
|
phoneNumberWithStringValue:[self phoneNumberForThread].toE164]]
|
|
|
|
];
|
|
|
|
|
|
|
|
CNContactViewController *controller =
|
|
|
|
[CNContactViewController viewControllerForUnknownContact:cncontact];
|
2015-10-31 16:53:32 +01:00
|
|
|
controller.allowsActions = NO;
|
|
|
|
controller.allowsEditing = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
controller.contactStore = contactStore;
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
[self.navigationController pushViewController:controller animated:YES];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
// The "Add to existing contacts" is known to be destroying the view controller stack on iOS 9
|
|
|
|
// http://stackoverflow.com/questions/32973254/cncontactviewcontroller-forunknowncontact-unusable-destroys-interface
|
|
|
|
|
|
|
|
// Warning the user
|
|
|
|
UIAlertController *alertController = [UIAlertController
|
|
|
|
alertControllerWithTitle:@"iOS 9 Bug"
|
|
|
|
message:@"iOS 9 introduced a bug that prevents us from adding this number to an "
|
|
|
|
@"existing contact from the app. You can still create a new contact for "
|
|
|
|
@"this number or copy-paste it into an existing contact sheet."
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
|
|
|
|
[alertController
|
|
|
|
addAction:[UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleCancel handler:nil]];
|
|
|
|
|
|
|
|
[alertController
|
|
|
|
addAction:[UIAlertAction actionWithTitle:@"Copy number"
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
|
|
|
pasteboard.string = [self phoneNumberForThread].toE164;
|
|
|
|
[controller.navigationController popViewControllerAnimated:YES];
|
|
|
|
}]];
|
|
|
|
|
2015-10-31 16:53:32 +01:00
|
|
|
[controller presentViewController:alertController animated:YES completion:nil];
|
2015-02-17 00:14:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (_displayPhoneAsTitle) {
|
|
|
|
self.title = [PhoneNumber
|
|
|
|
bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:[[self phoneNumberForThread] toE164]];
|
|
|
|
} else {
|
2015-01-27 21:17:49 +01:00
|
|
|
[self setNavigationTitle];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)showGroupMembers {
|
2015-01-14 22:30:01 +01:00
|
|
|
[self.navController hideDropDown:self];
|
2014-12-24 02:25:10 +01:00
|
|
|
[self performSegueWithIdentifier:kShowGroupMembersSegue sender:self];
|
|
|
|
}
|
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
#pragma mark - Calls
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (SignalRecipient *)signalRecipient {
|
2015-11-29 01:14:49 +01:00
|
|
|
__block SignalRecipient *recipient;
|
|
|
|
[self.editingDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
recipient = [SignalRecipient recipientWithTextSecureIdentifier:[self phoneNumberForThread].toE164
|
|
|
|
withTransaction:transaction];
|
2015-11-29 01:14:49 +01:00
|
|
|
}];
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)isTextSecureReachable {
|
2016-06-28 04:51:57 +02:00
|
|
|
return isGroupConversation || [self signalRecipient];
|
2015-01-27 02:20:11 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (PhoneNumber *)phoneNumberForThread {
|
|
|
|
NSString *contactId = [(TSContactThread *)self.thread contactIdentifier];
|
2014-12-29 11:43:11 +01:00
|
|
|
return [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:contactId];
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)callAction {
|
2016-06-28 04:51:57 +02:00
|
|
|
if ([self canCall]) {
|
2014-12-29 11:43:11 +01:00
|
|
|
PhoneNumber *number = [self phoneNumberForThread];
|
|
|
|
Contact *contact = [[Environment.getCurrent contactsManager] latestContactForPhoneNumber:number];
|
|
|
|
[Environment.phoneManager initiateOutgoingCallToContact:contact atRemoteNumber:number];
|
2014-11-29 19:54:33 +01:00
|
|
|
} else {
|
2016-06-28 04:51:57 +02:00
|
|
|
DDLogWarn(@"Tried to initiate a call but thread is not callable.");
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)canCall {
|
2016-06-28 04:51:57 +02:00
|
|
|
return !(isGroupConversation || [((TSContactThread *)self.thread).contactIdentifier isEqualToString:[TSAccountManager localNumber]]);
|
2015-01-27 21:17:49 +01:00
|
|
|
}
|
2015-02-17 00:14:50 +01:00
|
|
|
|
2015-01-27 02:20:11 +01:00
|
|
|
- (void)textViewDidChange:(UITextView *)textView {
|
2015-12-22 12:45:09 +01:00
|
|
|
if ([textView.text length] > 0) {
|
2015-01-27 02:20:11 +01:00
|
|
|
self.inputToolbar.contentView.rightBarButtonItem.enabled = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2015-01-27 02:20:11 +01:00
|
|
|
self.inputToolbar.contentView.rightBarButtonItem.enabled = NO;
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
#pragma mark - JSQMessagesViewController method overrides
|
|
|
|
|
|
|
|
- (void)didPressSendButton:(UIButton *)button
|
|
|
|
withMessageText:(NSString *)text
|
|
|
|
senderId:(NSString *)senderId
|
|
|
|
senderDisplayName:(NSString *)senderDisplayName
|
2015-12-22 12:45:09 +01:00
|
|
|
date:(NSDate *)date {
|
2014-12-04 11:27:45 +01:00
|
|
|
if (text.length > 0) {
|
2014-10-29 21:58:58 +01:00
|
|
|
[JSQSystemSoundPlayer jsq_playMessageSentSound];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:self.thread
|
|
|
|
messageBody:text
|
|
|
|
attachments:nil];
|
|
|
|
|
2015-09-01 19:22:08 +02:00
|
|
|
[[TSMessagesManager sharedManager] sendMessage:message inThread:self.thread success:nil failure:nil];
|
2014-10-29 21:58:58 +01:00
|
|
|
[self finishSendingMessage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - JSQMessages CollectionView DataSource
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
|
2014-11-25 21:33:29 +01:00
|
|
|
return [self messageAtIndexPath:indexPath];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView
|
2016-07-09 00:25:28 +02:00
|
|
|
messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
TSInteraction *message = [self interactionAtIndexPath:indexPath];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
if ([message isKindOfClass:[TSOutgoingMessage class]]) {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)message;
|
|
|
|
switch (outgoingMessage.messageState) {
|
2015-06-07 19:04:24 +02:00
|
|
|
case TSOutgoingMessageStateUnsent:
|
|
|
|
return self.outgoingMessageFailedImageData;
|
|
|
|
case TSOutgoingMessageStateAttemptingOut:
|
|
|
|
return self.currentlyOutgoingBubbleImageData;
|
|
|
|
default:
|
|
|
|
return self.outgoingBubbleImageData;
|
2014-12-09 22:18:39 +01:00
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
return self.incomingBubbleImageData;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (id<JSQMessageAvatarImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
|
2014-10-29 21:58:58 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionView DataSource
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView
|
2016-07-09 00:25:28 +02:00
|
|
|
cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
TSMessageAdapter *message = [self messageAtIndexPath:indexPath];
|
|
|
|
NSParameterAssert(message != nil);
|
|
|
|
|
|
|
|
JSQMessagesCollectionViewCell *cell;
|
|
|
|
switch (message.messageType) {
|
|
|
|
case TSCallAdapter: {
|
|
|
|
DDLogDebug(@"building cell for Call");
|
|
|
|
JSQCall *call = (JSQCall *)message;
|
|
|
|
cell = [self loadCallCellForCall:call atIndexPath:indexPath];
|
|
|
|
} break;
|
|
|
|
case TSInfoMessageAdapter: {
|
|
|
|
DDLogDebug(@"building cell for InfoMessage");
|
|
|
|
JSQInfoMessage *infoMessage = (JSQInfoMessage *)message;
|
|
|
|
cell = [self loadInfoMessageCellForMessage:infoMessage atIndexPath:indexPath];
|
|
|
|
} break;
|
|
|
|
case TSErrorMessageAdapter: {
|
|
|
|
DDLogDebug(@"building cell for ErrorMessage");
|
|
|
|
JSQErrorMessage *errorMessage = (JSQErrorMessage *)message;
|
|
|
|
cell = [self loadErrorMessageCellForMessage:errorMessage atIndexPath:indexPath];
|
|
|
|
} break;
|
|
|
|
case TSIncomingMessageAdapter: {
|
|
|
|
DDLogDebug(@"building cell for incoming message: %@", message);
|
|
|
|
cell = [self loadIncomingMessageCellForMessage:message atIndexPath:indexPath];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
} break;
|
|
|
|
case TSOutgoingMessageAdapter: {
|
|
|
|
DDLogDebug(@"building cell for incoming message: %@", message);
|
|
|
|
cell = [self loadOutgoingCellForMessage:message atIndexPath:indexPath];
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
DDLogDebug(@"using default cell constructor for message: %@", message);
|
|
|
|
cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
|
|
|
|
} break;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
2016-07-09 00:25:28 +02:00
|
|
|
cell.delegate = collectionView;
|
|
|
|
|
|
|
|
return cell;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Loading message cells
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (JSQMessagesCollectionViewCell *)loadIncomingMessageCellForMessage:(id<JSQMessageData>)message
|
|
|
|
atIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
JSQMessagesCollectionViewCell *cell =
|
|
|
|
(JSQMessagesCollectionViewCell *)[super collectionView:self.collectionView cellForItemAtIndexPath:indexPath];
|
2014-12-06 17:45:42 +01:00
|
|
|
if (!message.isMediaMessage) {
|
2015-01-14 22:30:01 +01:00
|
|
|
cell.textView.textColor = [UIColor ows_blackColor];
|
2015-12-22 12:45:09 +01:00
|
|
|
cell.textView.linkTextAttributes = @{
|
|
|
|
NSForegroundColorAttributeName : cell.textView.textColor,
|
|
|
|
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid)
|
|
|
|
};
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
return cell;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (JSQMessagesCollectionViewCell *)loadOutgoingCellForMessage:(id<JSQMessageData>)message
|
|
|
|
atIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
JSQMessagesCollectionViewCell *cell =
|
|
|
|
(JSQMessagesCollectionViewCell *)[super collectionView:self.collectionView cellForItemAtIndexPath:indexPath];
|
|
|
|
if (!message.isMediaMessage) {
|
2014-12-26 23:18:54 +01:00
|
|
|
cell.textView.textColor = [UIColor whiteColor];
|
2015-12-22 12:45:09 +01:00
|
|
|
cell.textView.linkTextAttributes = @{
|
|
|
|
NSForegroundColorAttributeName : cell.textView.textColor,
|
|
|
|
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid)
|
|
|
|
};
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
- (JSQCallCollectionViewCell *)loadCallCellForCall:(JSQCall *)call
|
|
|
|
atIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
|
|
|
|
JSQCallCollectionViewCell *callCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:[JSQCallCollectionViewCell cellReuseIdentifier]
|
|
|
|
forIndexPath:indexPath];
|
|
|
|
|
|
|
|
NSString *text = call.date != nil ? [call text] : call.senderDisplayName;
|
|
|
|
NSString *allText = call.date != nil ? [text stringByAppendingString:[call dateText]] : text;
|
|
|
|
|
|
|
|
UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0f];
|
|
|
|
UIFont *regularFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
|
|
|
|
|
|
|
|
//TODO declarative dict
|
|
|
|
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];
|
|
|
|
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:allText
|
|
|
|
attributes:attrs];
|
|
|
|
|
|
|
|
if([call date]!=nil) {
|
|
|
|
// Not a group meta message
|
|
|
|
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
regularFont, NSFontAttributeName, nil];
|
|
|
|
|
|
|
|
const NSRange range = NSMakeRange([text length],[[call dateText] length]);
|
|
|
|
[attributedText setAttributes:subAttrs range:range];
|
|
|
|
|
|
|
|
BOOL isOutgoing = [self.senderId isEqualToString:call.senderId];
|
|
|
|
if (isOutgoing)
|
|
|
|
{
|
|
|
|
callCell.outgoingCallImageView.image = [call thumbnailImage];
|
|
|
|
} else {
|
|
|
|
callCell.incomingCallImageView.image = [call thumbnailImage];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO wrt comment, does it make sense to receive a group meta message in a *call* or was this copy/paste misfire?
|
|
|
|
// A group meta message
|
|
|
|
callCell.incomingCallImageView.image = [call thumbnailImage];
|
|
|
|
}
|
|
|
|
callCell.cellLabel.attributedText = attributedText;
|
|
|
|
callCell.cellLabel.numberOfLines = 0; // uses as many lines as it needs
|
|
|
|
|
|
|
|
// TODO is this a constant somewhere else already?
|
|
|
|
callCell.cellLabel.textColor = [UIColor colorWithRed:32.f/255.f green:144.f/255.f blue:234.f/255.f alpha:1.f];
|
|
|
|
|
|
|
|
callCell.layer.shouldRasterize = YES;
|
|
|
|
callCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
|
|
|
|
return callCell;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
- (JSQDisplayedMessageCollectionViewCell *)loadInfoMessageCellForMessage:(JSQInfoMessage *)infoMessage
|
|
|
|
atIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
JSQDisplayedMessageCollectionViewCell *infoCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:[JSQDisplayedMessageCollectionViewCell cellReuseIdentifier]
|
|
|
|
forIndexPath:indexPath];
|
|
|
|
infoCell.cellLabel.text = [infoMessage text];
|
|
|
|
infoCell.cellLabel.textColor = [UIColor darkGrayColor];
|
|
|
|
|
|
|
|
// TODO is this a constant somewhere else already?
|
|
|
|
infoCell.textContainer.layer.borderColor = [[UIColor colorWithRed:239.f/255.f green:189.f/255.f blue:88.f/255.f alpha:1.0f] CGColor];
|
|
|
|
infoCell.headerImageView.image = [UIImage imageNamed:@"warning_white"];
|
|
|
|
infoCell.layer.shouldRasterize = YES;
|
|
|
|
infoCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
|
|
|
|
return infoCell;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
- (JSQDisplayedMessageCollectionViewCell *)loadErrorMessageCellForMessage:(JSQErrorMessage *)errorMessage
|
2015-12-22 12:45:09 +01:00
|
|
|
atIndexPath:(NSIndexPath *)indexPath {
|
2016-07-09 00:25:28 +02:00
|
|
|
JSQDisplayedMessageCollectionViewCell *errorCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:[JSQDisplayedMessageCollectionViewCell cellReuseIdentifier]
|
|
|
|
forIndexPath:indexPath];
|
|
|
|
errorCell.cellLabel.text = [errorMessage text];
|
|
|
|
errorCell.cellLabel.textColor = [UIColor darkGrayColor];
|
|
|
|
|
|
|
|
// TODO is this a constant somewhere else already?
|
|
|
|
errorCell.textContainer.layer.borderColor = [[UIColor colorWithRed:195.f/255.f green:0 blue:22.f/255.f alpha:1.0f] CGColor];
|
|
|
|
errorCell.headerImageView.image = [UIImage imageNamed:@"error_white"];
|
|
|
|
errorCell.layer.shouldRasterize = YES;
|
|
|
|
errorCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
|
|
|
|
return errorCell;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Adjusting cell label heights
|
|
|
|
|
|
|
|
- (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
|
2015-12-22 12:45:09 +01:00
|
|
|
layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout
|
|
|
|
heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath {
|
2014-11-25 16:38:33 +01:00
|
|
|
if ([self showDateAtIndexPath:indexPath]) {
|
2014-10-29 21:58:58 +01:00
|
|
|
return kJSQMessagesCollectionViewCellLabelHeightDefault;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)showDateAtIndexPath:(NSIndexPath *)indexPath {
|
2014-11-25 16:38:33 +01:00
|
|
|
BOOL showDate = NO;
|
|
|
|
if (indexPath.row == 0) {
|
|
|
|
showDate = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
|
|
|
TSMessageAdapter *currentMessage = [self messageAtIndexPath:indexPath];
|
|
|
|
|
|
|
|
TSMessageAdapter *previousMessage =
|
|
|
|
[self messageAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row - 1 inSection:indexPath.section]];
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
NSTimeInterval timeDifference = [currentMessage.date timeIntervalSinceDate:previousMessage.date];
|
|
|
|
if (timeDifference > kTSMessageSentDateShowTimeInterval) {
|
|
|
|
showDate = YES;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-25 16:38:33 +01:00
|
|
|
return showDate;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
attributedTextForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath {
|
2014-12-17 06:44:36 +01:00
|
|
|
if ([self showDateAtIndexPath:indexPath]) {
|
|
|
|
TSMessageAdapter *currentMessage = [self messageAtIndexPath:indexPath];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:currentMessage.date];
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)shouldShowMessageStatusAtIndexPath:(NSIndexPath *)indexPath {
|
2014-12-17 06:44:36 +01:00
|
|
|
TSMessageAdapter *currentMessage = [self messageAtIndexPath:indexPath];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-06-07 19:04:24 +02:00
|
|
|
// If message failed, say that message should be tapped to retry;
|
2016-07-09 00:25:28 +02:00
|
|
|
if (currentMessage.messageType == TSOutgoingMessageAdapter) {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)currentMessage;
|
|
|
|
if(outgoingMessage.messageState == TSOutgoingMessageStateUnsent) {
|
|
|
|
return YES;
|
|
|
|
}
|
2015-06-07 19:04:24 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if ([self.thread isKindOfClass:[TSGroupThread class]]) {
|
2014-12-17 06:44:36 +01:00
|
|
|
return currentMessage.messageType == TSIncomingMessageAdapter;
|
2015-06-07 19:04:24 +02:00
|
|
|
} else {
|
2015-12-22 12:45:09 +01:00
|
|
|
if (indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
|
2014-12-17 06:44:36 +01:00
|
|
|
return [self isMessageOutgoingAndDelivered:currentMessage];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
if (![self isMessageOutgoingAndDelivered:currentMessage]) {
|
|
|
|
return NO;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
TSMessageAdapter *nextMessage = [self nextOutgoingMessage:indexPath];
|
|
|
|
return ![self isMessageOutgoingAndDelivered:nextMessage];
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
2014-12-04 15:01:05 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (TSMessageAdapter *)nextOutgoingMessage:(NSIndexPath *)indexPath {
|
|
|
|
TSMessageAdapter *nextMessage =
|
|
|
|
[self messageAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]];
|
2014-12-04 15:01:05 +01:00
|
|
|
int i = 1;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
while (indexPath.item + i < [self.collectionView numberOfItemsInSection:indexPath.section] - 1 &&
|
|
|
|
![self isMessageOutgoingAndDelivered:nextMessage]) {
|
2014-12-04 15:01:05 +01:00
|
|
|
i++;
|
2015-12-22 12:45:09 +01:00
|
|
|
nextMessage =
|
|
|
|
[self messageAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row + i inSection:indexPath.section]];
|
2014-12-04 15:01:05 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-04 15:01:05 +01:00
|
|
|
return nextMessage;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)isMessageOutgoingAndDelivered:(TSMessageAdapter *)message {
|
2016-07-09 00:25:28 +02:00
|
|
|
if (message.messageType == TSOutgoingMessageAdapter) {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)message;
|
|
|
|
if(outgoingMessage.messageState == TSOutgoingMessageStateDelivered) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
attributedTextForCellBottomLabelAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
TSMessageAdapter *msg = [self messageAtIndexPath:indexPath];
|
2015-06-07 19:04:24 +02:00
|
|
|
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
|
2015-12-22 12:45:09 +01:00
|
|
|
textAttachment.bounds = CGRectMake(0, 0, 11.0f, 10.0f);
|
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
if ([self shouldShowMessageStatusAtIndexPath:indexPath]) {
|
2016-07-09 00:25:28 +02:00
|
|
|
if (msg.messageType == TSOutgoingMessageAdapter) {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)msg;
|
|
|
|
if(outgoingMessage.messageState == TSOutgoingMessageStateUnsent) {
|
|
|
|
NSMutableAttributedString *attrStr =
|
2015-12-22 12:45:09 +01:00
|
|
|
[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"FAILED_SENDING_TEXT", nil)];
|
2016-07-09 00:25:28 +02:00
|
|
|
[attrStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
|
|
|
|
return attrStr;
|
|
|
|
}
|
2015-06-07 19:04:24 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if ([self.thread isKindOfClass:[TSGroupThread class]]) {
|
2014-12-17 06:44:36 +01:00
|
|
|
NSString *name = [[Environment getCurrent].contactsManager nameStringForPhoneIdentifier:msg.senderId];
|
2015-12-22 12:45:09 +01:00
|
|
|
name = name ? name : msg.senderId;
|
|
|
|
|
2015-06-19 14:31:50 +02:00
|
|
|
if (!name) {
|
|
|
|
name = @"";
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:name];
|
2014-12-17 06:44:36 +01:00
|
|
|
[attrStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-06-07 19:04:24 +02:00
|
|
|
return attrStr;
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2014-12-17 06:44:36 +01:00
|
|
|
_lastDeliveredMessageIndexPath = indexPath;
|
2015-12-22 12:45:09 +01:00
|
|
|
NSMutableAttributedString *attrStr =
|
|
|
|
[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"DELIVERED_MESSAGE_TEXT", @"")];
|
2014-12-17 06:44:36 +01:00
|
|
|
[attrStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-06-07 19:04:24 +02:00
|
|
|
return attrStr;
|
2014-12-17 06:44:36 +01:00
|
|
|
}
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
- (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
|
2015-12-22 12:45:09 +01:00
|
|
|
layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout
|
|
|
|
heightForCellBottomLabelAtIndexPath:(NSIndexPath *)indexPath {
|
2015-03-19 01:59:44 +01:00
|
|
|
if ([self shouldShowMessageStatusAtIndexPath:indexPath]) {
|
2014-11-29 19:54:33 +01:00
|
|
|
return 16.0f;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-29 19:54:33 +01:00
|
|
|
return 0.0f;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapCellAtIndexPath:(NSIndexPath *)indexPath touchLocation:(CGPoint)touchLocation
|
|
|
|
{
|
|
|
|
// Pass info/error message tapping to bubble tapping handler
|
|
|
|
[self collectionView:collectionView didTapMessageBubbleAtIndexPath:indexPath];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
TSMessageAdapter *messageItem =
|
|
|
|
[collectionView.dataSource collectionView:collectionView messageDataForItemAtIndexPath:indexPath];
|
|
|
|
TSInteraction *interaction = [self interactionAtIndexPath:indexPath];
|
|
|
|
|
2014-12-11 00:05:41 +01:00
|
|
|
switch (messageItem.messageType) {
|
2016-07-09 00:25:28 +02:00
|
|
|
case TSOutgoingMessageAdapter: {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)messageItem;
|
|
|
|
if (outgoingMessage.messageState == TSOutgoingMessageStateUnsent) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[self handleUnsentMessageTap:(TSOutgoingMessage *)interaction];
|
2014-12-11 00:05:41 +01:00
|
|
|
}
|
2016-07-09 00:25:28 +02:00
|
|
|
}
|
|
|
|
// No `break` as we want to fall through to capture tapping on media items
|
2015-12-22 12:45:09 +01:00
|
|
|
case TSIncomingMessageAdapter: {
|
2014-12-11 00:05:41 +01:00
|
|
|
BOOL isMediaMessage = [messageItem isMediaMessage];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-11 00:05:41 +01:00
|
|
|
if (isMediaMessage) {
|
2015-12-22 12:45:09 +01:00
|
|
|
if ([[messageItem media] isKindOfClass:[TSPhotoAdapter class]]) {
|
|
|
|
TSPhotoAdapter *messageMedia = (TSPhotoAdapter *)[messageItem media];
|
|
|
|
|
2016-04-13 20:38:42 +02:00
|
|
|
tappedImage = ((UIImageView *)[messageMedia mediaView]).image;
|
|
|
|
if(tappedImage == nil) {
|
|
|
|
DDLogWarn(@"tapped TSPhotoAdapter with nil image");
|
|
|
|
} else {
|
2015-12-22 12:45:09 +01:00
|
|
|
CGRect convertedRect =
|
2016-04-13 20:38:42 +02:00
|
|
|
[self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame
|
|
|
|
toView:nil];
|
2015-01-14 22:30:01 +01:00
|
|
|
__block TSAttachment *attachment = nil;
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2016-04-13 20:38:42 +02:00
|
|
|
attachment =
|
|
|
|
[TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction];
|
2015-01-14 22:30:01 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSAttachmentStream *attStream = (TSAttachmentStream *)attachment;
|
|
|
|
FullImageViewController *vc = [[FullImageViewController alloc]
|
2016-04-13 20:38:42 +02:00
|
|
|
initWithAttachment:attStream
|
|
|
|
fromRect:convertedRect
|
|
|
|
forInteraction:[self interactionAtIndexPath:indexPath]
|
|
|
|
isAnimated:NO];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-25 02:40:51 +01:00
|
|
|
[vc presentFromViewController:self.navigationController];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
} else if ([[messageItem media] isKindOfClass:[TSAnimatedAdapter class]]) {
|
2015-09-01 04:51:39 +02:00
|
|
|
// Show animated image full-screen
|
2015-12-22 12:45:09 +01:00
|
|
|
TSAnimatedAdapter *messageMedia = (TSAnimatedAdapter *)[messageItem media];
|
|
|
|
tappedImage = ((UIImageView *)[messageMedia mediaView]).image;
|
2016-04-13 20:38:42 +02:00
|
|
|
if(tappedImage == nil) {
|
|
|
|
DDLogWarn(@"tapped TSAnimatedAdapter with nil image");
|
|
|
|
} else {
|
|
|
|
CGRect convertedRect =
|
2015-12-22 12:45:09 +01:00
|
|
|
[self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame
|
|
|
|
toView:nil];
|
2016-04-13 20:38:42 +02:00
|
|
|
__block TSAttachment *attachment = nil;
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
attachment =
|
|
|
|
[TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction];
|
|
|
|
}];
|
|
|
|
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
|
|
|
|
TSAttachmentStream *attStream = (TSAttachmentStream *)attachment;
|
|
|
|
FullImageViewController *vc =
|
2015-12-22 12:45:09 +01:00
|
|
|
[[FullImageViewController alloc] initWithAttachment:attStream
|
|
|
|
fromRect:convertedRect
|
|
|
|
forInteraction:[self interactionAtIndexPath:indexPath]
|
|
|
|
isAnimated:YES];
|
2016-04-13 20:38:42 +02:00
|
|
|
[vc presentFromViewController:self.navigationController];
|
|
|
|
}
|
2015-09-01 04:51:39 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
} else if ([[messageItem media] isKindOfClass:[TSVideoAttachmentAdapter class]]) {
|
2015-01-14 22:30:01 +01:00
|
|
|
// fileurl disappeared should look up in db as before. will do refactor
|
|
|
|
// full screen, check this setup with a .mov
|
2015-12-22 12:45:09 +01:00
|
|
|
TSVideoAttachmentAdapter *messageMedia = (TSVideoAttachmentAdapter *)[messageItem media];
|
|
|
|
_currentMediaAdapter = messageMedia;
|
|
|
|
__block TSAttachment *attachment = nil;
|
2014-12-26 23:18:54 +01:00
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
attachment =
|
|
|
|
[TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction];
|
2014-12-26 23:18:54 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 23:18:54 +01:00
|
|
|
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSAttachmentStream *attStream = (TSAttachmentStream *)attachment;
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
if ([messageMedia isVideo]) {
|
2015-01-22 05:08:12 +01:00
|
|
|
if ([fileManager fileExistsAtPath:[attStream.mediaURL path]]) {
|
2015-01-30 23:28:05 +01:00
|
|
|
[self dismissKeyBoard];
|
2015-01-22 05:08:12 +01:00
|
|
|
_videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:attStream.mediaURL];
|
2015-01-14 22:30:01 +01:00
|
|
|
[_videoPlayer prepareToPlay];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(moviePlayBackDidFinish:)
|
|
|
|
name:MPMoviePlayerPlaybackDidFinishNotification
|
|
|
|
object:_videoPlayer];
|
|
|
|
|
|
|
|
_videoPlayer.controlStyle = MPMovieControlStyleDefault;
|
2015-02-17 00:14:50 +01:00
|
|
|
_videoPlayer.shouldAutoplay = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
[self.view addSubview:_videoPlayer.view];
|
2015-01-14 22:30:01 +01:00
|
|
|
[_videoPlayer setFullscreen:YES animated:YES];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
} else if ([messageMedia isAudio]) {
|
2015-01-22 05:08:12 +01:00
|
|
|
if (messageMedia.isAudioPlaying) {
|
|
|
|
// if you had started playing an audio msg and now you're tapping it to pause
|
|
|
|
messageMedia.isAudioPlaying = NO;
|
|
|
|
[_audioPlayer pause];
|
|
|
|
messageMedia.isPaused = YES;
|
|
|
|
[_audioPlayerPoller invalidate];
|
2015-12-22 12:45:09 +01:00
|
|
|
double current = [_audioPlayer currentTime] / [_audioPlayer duration];
|
2015-01-22 05:08:12 +01:00
|
|
|
[messageMedia setAudioProgressFromFloat:(float)current];
|
|
|
|
[messageMedia setAudioIconToPlay];
|
|
|
|
} else {
|
|
|
|
BOOL isResuming = NO;
|
|
|
|
[_audioPlayerPoller invalidate];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
// loop through all the other bubbles and set their isPlaying to false
|
|
|
|
NSInteger num_bubbles = [self collectionView:collectionView numberOfItemsInSection:0];
|
2015-12-22 12:45:09 +01:00
|
|
|
for (NSInteger i = 0; i < num_bubbles; i++) {
|
2015-01-22 05:08:12 +01:00
|
|
|
NSIndexPath *index_path = [NSIndexPath indexPathForRow:i inSection:0];
|
2015-12-22 12:45:09 +01:00
|
|
|
TSMessageAdapter *msgAdapter =
|
|
|
|
[collectionView.dataSource collectionView:collectionView
|
|
|
|
messageDataForItemAtIndexPath:index_path];
|
|
|
|
if (msgAdapter.messageType == TSIncomingMessageAdapter &&
|
|
|
|
msgAdapter.isMediaMessage) {
|
|
|
|
TSVideoAttachmentAdapter *msgMedia =
|
|
|
|
(TSVideoAttachmentAdapter *)[msgAdapter media];
|
2015-01-22 05:08:12 +01:00
|
|
|
if ([msgMedia isAudio]) {
|
|
|
|
if (msgMedia == messageMedia && messageMedia.isPaused) {
|
|
|
|
isResuming = YES;
|
|
|
|
} else {
|
|
|
|
msgMedia.isAudioPlaying = NO;
|
2015-12-22 12:45:09 +01:00
|
|
|
msgMedia.isPaused = NO;
|
2015-01-22 05:08:12 +01:00
|
|
|
[msgMedia setAudioIconToPlay];
|
|
|
|
[msgMedia setAudioProgressFromFloat:0];
|
2015-01-25 00:48:40 +01:00
|
|
|
[msgMedia resetAudioDuration];
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
if (isResuming) {
|
|
|
|
// if you had paused an audio msg and now you're tapping to resume
|
|
|
|
[_audioPlayer prepareToPlay];
|
|
|
|
[_audioPlayer play];
|
|
|
|
[messageMedia setAudioIconToPause];
|
|
|
|
messageMedia.isAudioPlaying = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
messageMedia.isPaused = NO;
|
|
|
|
_audioPlayerPoller =
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:.05
|
|
|
|
target:self
|
|
|
|
selector:@selector(audioPlayerUpdated:)
|
|
|
|
userInfo:@{
|
|
|
|
@"adapter" : messageMedia
|
|
|
|
}
|
|
|
|
repeats:YES];
|
2015-01-22 05:08:12 +01:00
|
|
|
} else {
|
|
|
|
// if you are tapping an audio msg for the first time to play
|
|
|
|
messageMedia.isAudioPlaying = YES;
|
|
|
|
NSError *error;
|
2015-12-22 12:45:09 +01:00
|
|
|
_audioPlayer =
|
|
|
|
[[AVAudioPlayer alloc] initWithContentsOfURL:attStream.mediaURL error:&error];
|
2015-01-25 00:48:40 +01:00
|
|
|
if (error) {
|
2015-08-14 00:19:29 +02:00
|
|
|
DDLogError(@"error: %@", error);
|
2015-01-25 00:48:40 +01:00
|
|
|
}
|
2015-01-22 05:08:12 +01:00
|
|
|
[_audioPlayer prepareToPlay];
|
|
|
|
[_audioPlayer play];
|
|
|
|
[messageMedia setAudioIconToPause];
|
|
|
|
_audioPlayer.delegate = self;
|
2015-12-22 12:45:09 +01:00
|
|
|
_audioPlayerPoller =
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:.05
|
|
|
|
target:self
|
|
|
|
selector:@selector(audioPlayerUpdated:)
|
|
|
|
userInfo:@{
|
|
|
|
@"adapter" : messageMedia
|
|
|
|
}
|
|
|
|
repeats:YES];
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2014-12-26 23:18:54 +01:00
|
|
|
}
|
2014-12-11 00:05:41 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
} break;
|
2014-12-11 00:05:41 +01:00
|
|
|
case TSErrorMessageAdapter:
|
2015-12-22 12:45:09 +01:00
|
|
|
[self handleErrorMessageTap:(TSErrorMessage *)interaction];
|
2014-12-11 00:05:41 +01:00
|
|
|
break;
|
|
|
|
case TSInfoMessageAdapter:
|
2015-02-19 01:04:32 +01:00
|
|
|
[self handleWarningTap:interaction];
|
2014-12-11 00:05:41 +01:00
|
|
|
break;
|
2014-12-31 21:30:20 +01:00
|
|
|
case TSCallAdapter:
|
|
|
|
break;
|
2014-12-11 00:05:41 +01:00
|
|
|
default:
|
2016-07-09 00:25:28 +02:00
|
|
|
DDLogDebug(@"Unhandled bubble touch for interaction: %@.", interaction);
|
2014-12-11 00:05:41 +01:00
|
|
|
break;
|
2014-11-29 19:54:33 +01:00
|
|
|
}
|
2014-12-11 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)handleWarningTap:(TSInteraction *)interaction {
|
2016-07-09 00:25:28 +02:00
|
|
|
//TODO why is handle warning tap expecting a TSIncomingMessage? I assumed it was for info messages, but maybe those aren't actionable.
|
|
|
|
// Looks like we create an InfoMessage "attachment is downloading" and tapping on it may restart a stalled fetch
|
2015-02-19 01:04:32 +01:00
|
|
|
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSIncomingMessage *message = (TSIncomingMessage *)interaction;
|
|
|
|
|
2015-02-19 01:04:32 +01:00
|
|
|
for (NSString *attachmentId in message.attachments) {
|
|
|
|
__block TSAttachment *attachment;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-19 01:04:32 +01:00
|
|
|
[self.editingDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
attachment = [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
|
2015-02-19 01:04:32 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-19 01:04:32 +01:00
|
|
|
if ([attachment isKindOfClass:[TSAttachmentPointer class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSAttachmentPointer *pointer = (TSAttachmentPointer *)attachment;
|
|
|
|
|
2016-07-09 00:25:28 +02:00
|
|
|
// FIXME possible for pointer to get stuck in isDownloading state if app is closed while downloading.
|
2015-02-19 01:04:32 +01:00
|
|
|
if (!pointer.isDownloading) {
|
|
|
|
[[TSMessagesManager sharedManager] retrieveAttachment:pointer messageId:message.uniqueId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 04:50:41 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)moviePlayBackDidFinish:(id)sender {
|
2015-01-14 22:30:01 +01:00
|
|
|
DDLogDebug(@"playback finished");
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)collectionView:(JSQMessagesCollectionView *)collectionView
|
|
|
|
header:(JSQMessagesLoadEarlierHeaderView *)headerView
|
|
|
|
didTapLoadEarlierMessagesButton:(UIButton *)sender {
|
2014-12-31 13:22:40 +01:00
|
|
|
if ([self shouldShowLoadEarlierMessages]) {
|
|
|
|
self.page++;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
NSInteger item = (NSInteger)[self scrollToItem];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self updateRangeOptionsForPage:self.page];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[self.messageMappings updateWithTransaction:transaction];
|
2014-12-31 13:22:40 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self updateLayoutForEarlierMessagesWithOffset:item];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)shouldShowLoadEarlierMessages {
|
2014-12-31 13:22:40 +01:00
|
|
|
__block BOOL show = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
show = [self.messageMappings numberOfItemsInGroup:self.thread.uniqueId] <
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName] numberOfItemsInGroup:self.thread.uniqueId];
|
2014-12-31 13:22:40 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
return show;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSUInteger)scrollToItem {
|
|
|
|
__block NSUInteger item =
|
|
|
|
kYapDatabaseRangeLength * (self.page + 1) - [self.messageMappings numberOfItemsInGroup:self.thread.uniqueId];
|
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
NSUInteger numberOfVisibleMessages = [self.messageMappings numberOfItemsInGroup:self.thread.uniqueId];
|
|
|
|
NSUInteger numberOfTotalMessages =
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName] numberOfItemsInGroup:self.thread.uniqueId];
|
|
|
|
NSUInteger numberOfMessagesToLoad = numberOfTotalMessages - numberOfVisibleMessages;
|
|
|
|
|
|
|
|
BOOL canLoadFullRange = numberOfMessagesToLoad >= kYapDatabaseRangeLength;
|
|
|
|
|
|
|
|
if (!canLoadFullRange) {
|
|
|
|
item = numberOfMessagesToLoad;
|
|
|
|
}
|
2014-12-31 13:22:40 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
return item == 0 ? item : item - 1;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)updateLoadEarlierVisible {
|
2014-12-31 13:22:40 +01:00
|
|
|
[self setShowLoadEarlierMessagesHeader:[self shouldShowLoadEarlierMessages]];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)updateLayoutForEarlierMessagesWithOffset:(NSInteger)offset {
|
|
|
|
[self.collectionView.collectionViewLayout
|
|
|
|
invalidateLayoutWithContext:[JSQMessagesCollectionViewFlowLayoutInvalidationContext context]];
|
2014-12-31 13:22:40 +01:00
|
|
|
[self.collectionView reloadData];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:offset inSection:0]
|
|
|
|
atScrollPosition:UICollectionViewScrollPositionTop
|
|
|
|
animated:NO];
|
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self updateLoadEarlierVisible];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)updateRangeOptionsForPage:(NSUInteger)page {
|
|
|
|
YapDatabaseViewRangeOptions *rangeOptions =
|
|
|
|
[YapDatabaseViewRangeOptions flexibleRangeWithLength:kYapDatabaseRangeLength * (page + 1)
|
|
|
|
offset:0
|
|
|
|
from:YapDatabaseViewEnd];
|
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
rangeOptions.maxLength = kYapDatabaseRangeMaxLength;
|
|
|
|
rangeOptions.minLength = kYapDatabaseRangeMinLength;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-31 13:22:40 +01:00
|
|
|
[self.messageMappings setRangeOptions:rangeOptions forGroup:self.thread.uniqueId];
|
|
|
|
}
|
|
|
|
|
2014-12-11 00:05:41 +01:00
|
|
|
#pragma mark Bubble User Actions
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)handleUnsentMessageTap:(TSOutgoingMessage *)message {
|
2015-01-30 23:28:05 +01:00
|
|
|
[self dismissKeyBoard];
|
2015-12-22 12:45:09 +01:00
|
|
|
[DJWActionSheet showInView:self.parentViewController.view
|
|
|
|
withTitle:nil
|
|
|
|
cancelButtonTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", @"")
|
|
|
|
destructiveButtonTitle:NSLocalizedString(@"TXT_DELETE_TITLE", @"")
|
|
|
|
otherButtonTitles:@[ NSLocalizedString(@"SEND_AGAIN_BUTTON", @"") ]
|
|
|
|
tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) {
|
|
|
|
if (tappedButtonIndex == actionSheet.cancelButtonIndex) {
|
|
|
|
DDLogDebug(@"User Cancelled");
|
|
|
|
} else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) {
|
|
|
|
[self.editingDatabaseConnection
|
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
[message removeWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
[[TSMessagesManager sharedManager] sendMessage:message
|
|
|
|
inThread:self.thread
|
|
|
|
success:nil
|
|
|
|
failure:nil];
|
|
|
|
[self finishSendingMessage];
|
|
|
|
}
|
|
|
|
}];
|
2014-12-11 00:05:41 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)deleteMessageAtIndexPath:(NSIndexPath *)indexPath {
|
2014-12-26 23:18:54 +01:00
|
|
|
[self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSInteraction *interaction = [self interactionAtIndexPath:indexPath];
|
|
|
|
[interaction removeWithTransaction:transaction];
|
2014-12-26 23:18:54 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)handleErrorMessageTap:(TSErrorMessage *)message {
|
2014-12-31 21:30:20 +01:00
|
|
|
if ([message isKindOfClass:[TSInvalidIdentityKeyErrorMessage class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSInvalidIdentityKeyErrorMessage *errorMessage = (TSInvalidIdentityKeyErrorMessage *)message;
|
|
|
|
NSString *newKeyFingerprint = [errorMessage newIdentityKey];
|
|
|
|
|
2015-06-07 22:37:19 +02:00
|
|
|
NSString *keyOwner;
|
|
|
|
if ([message isKindOfClass:[TSInvalidIdentityKeySendingErrorMessage class]]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSInvalidIdentityKeySendingErrorMessage *m = (TSInvalidIdentityKeySendingErrorMessage *)message;
|
2015-06-07 22:37:19 +02:00
|
|
|
keyOwner = [[[Environment getCurrent] contactsManager] nameStringForPhoneIdentifier:m.recipientId];
|
|
|
|
} else {
|
|
|
|
keyOwner = [self.thread name];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
NSString *messageString = [NSString
|
|
|
|
stringWithFormat:NSLocalizedString(@"ACCEPT_IDENTITYKEY_QUESTION", @""), keyOwner, newKeyFingerprint];
|
|
|
|
NSArray *actions = @[
|
|
|
|
NSLocalizedString(@"ACCEPT_IDENTITYKEY_BUTTON", @""),
|
|
|
|
NSLocalizedString(@"COPY_IDENTITYKEY_BUTTON", @"")
|
|
|
|
];
|
|
|
|
|
2015-01-30 23:28:05 +01:00
|
|
|
[self dismissKeyBoard];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
[DJWActionSheet showInView:self.parentViewController.view
|
|
|
|
withTitle:messageString
|
|
|
|
cancelButtonTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", @"")
|
|
|
|
destructiveButtonTitle:NSLocalizedString(@"TXT_DELETE_TITLE", @"")
|
|
|
|
otherButtonTitles:actions
|
|
|
|
tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) {
|
|
|
|
if (tappedButtonIndex == actionSheet.cancelButtonIndex) {
|
|
|
|
DDLogDebug(@"User Cancelled");
|
|
|
|
} else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) {
|
|
|
|
[self.editingDatabaseConnection
|
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
[message removeWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
switch (tappedButtonIndex) {
|
|
|
|
case 0:
|
|
|
|
[errorMessage acceptNewIdentityKey];
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
[[UIPasteboard generalPasteboard] setString:newKeyFingerprint];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Navigation
|
|
|
|
|
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
2015-12-22 12:45:09 +01:00
|
|
|
if ([segue.identifier isEqualToString:kFingerprintSegueIdentifier]) {
|
2014-12-04 00:23:36 +01:00
|
|
|
FingerprintViewController *vc = [segue destinationViewController];
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[vc configWithThread:self.thread];
|
2014-12-04 00:23:36 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else if ([segue.identifier isEqualToString:kUpdateGroupSegueIdentifier]) {
|
2014-12-17 06:44:36 +01:00
|
|
|
NewGroupViewController *vc = [segue destinationViewController];
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[vc configWithThread:(TSGroupThread *)self.thread];
|
2014-12-17 06:44:36 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else if ([segue.identifier isEqualToString:kShowGroupMembersSegue]) {
|
2014-12-24 02:25:10 +01:00
|
|
|
ShowGroupMembersViewController *vc = [segue destinationViewController];
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[vc configWithThread:(TSGroupThread *)self.thread];
|
2014-12-24 02:25:10 +01:00
|
|
|
}];
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - UIImagePickerController
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Presenting UIImagePickerController
|
|
|
|
*/
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)takePictureOrVideo {
|
2016-07-07 18:54:30 +02:00
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
|
|
|
DDLogError(@"Camera ImagePicker source not available");
|
|
|
|
return;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
2016-07-07 18:54:30 +02:00
|
|
|
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
|
|
|
|
picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ];
|
|
|
|
picker.allowsEditing = NO;
|
|
|
|
picker.delegate = self;
|
|
|
|
[self presentViewController:picker animated:YES completion:[UIUtil modalCompletionBlock]];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-07-07 18:54:30 +02:00
|
|
|
- (void)chooseFromLibrary {
|
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
|
|
|
|
DDLogError(@"PhotoLibrary ImagePicker source not available");
|
|
|
|
return;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2016-07-07 18:54:30 +02:00
|
|
|
|
|
|
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
|
|
|
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
|
|
|
|
picker.delegate = self;
|
|
|
|
picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ];
|
|
|
|
[self presentViewController:picker animated:YES completion:[UIUtil modalCompletionBlock]];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dismissing UIImagePickerController
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
|
2015-01-31 09:38:52 +01:00
|
|
|
[UIUtil modalCompletionBlock]();
|
2014-10-29 21:58:58 +01:00
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)resetFrame {
|
2015-01-31 09:38:52 +01:00
|
|
|
// fixes bug on frame being off after this selection
|
2015-12-22 12:45:09 +01:00
|
|
|
CGRect frame = [UIScreen mainScreen].applicationFrame;
|
2015-01-31 09:38:52 +01:00
|
|
|
self.view.frame = frame;
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
/*
|
2014-11-29 19:54:33 +01:00
|
|
|
* Fetching data from UIImagePickerController
|
2014-10-29 21:58:58 +01:00
|
|
|
*/
|
2016-04-13 20:38:42 +02:00
|
|
|
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
|
2015-01-31 09:38:52 +01:00
|
|
|
[UIUtil modalCompletionBlock]();
|
|
|
|
[self resetFrame];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
|
|
|
|
if (CFStringCompare((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
|
2015-01-14 22:30:01 +01:00
|
|
|
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
|
|
|
|
[self sendQualityAdjustedAttachment:videoURL];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2016-04-13 20:38:42 +02:00
|
|
|
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
|
|
|
|
{
|
|
|
|
// Image captured from camera
|
|
|
|
UIImage *pictureCamera = [[info objectForKey:UIImagePickerControllerOriginalImage] normalizedImage];
|
|
|
|
if (pictureCamera) {
|
|
|
|
DDLogVerbose(@"Sending picture attachement ...");
|
|
|
|
[self sendMessageAttachment:[self qualityAdjustedAttachmentForImage:pictureCamera] ofType:@"image/jpeg"];
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
2016-04-13 20:38:42 +02:00
|
|
|
} else {
|
|
|
|
// Image picked from library
|
|
|
|
// Send image as NSData to accommodate both static and animated images
|
|
|
|
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
|
|
|
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
|
|
|
|
resultBlock:^(ALAsset *asset) {
|
|
|
|
ALAssetRepresentation *representation = [asset defaultRepresentation];
|
|
|
|
Byte *img_buffer = (Byte *)malloc((unsigned long)representation.size);
|
|
|
|
NSUInteger length_buffered =
|
|
|
|
[representation getBytes:img_buffer fromOffset:0 length:(unsigned long)representation.size error:nil];
|
|
|
|
NSData *img_data = [NSData dataWithBytesNoCopy:img_buffer length:length_buffered];
|
|
|
|
NSString *file_type;
|
|
|
|
switch (img_buffer[0]) {
|
|
|
|
case 0x89:
|
|
|
|
file_type = @"image/png";
|
|
|
|
break;
|
|
|
|
case 0x47:
|
|
|
|
file_type = @"image/gif";
|
|
|
|
break;
|
|
|
|
case 0x49:
|
|
|
|
case 0x4D:
|
|
|
|
file_type = @"image/tiff";
|
|
|
|
break;
|
|
|
|
case 0x42:
|
2016-06-23 22:43:31 +02:00
|
|
|
file_type = @"image/bmp";
|
2016-04-13 20:38:42 +02:00
|
|
|
break;
|
|
|
|
case 0xFF:
|
|
|
|
default:
|
|
|
|
file_type = @"image/jpeg";
|
|
|
|
break;
|
|
|
|
}
|
2016-06-23 22:43:31 +02:00
|
|
|
DDLogVerbose(@"Picked image. Size in bytes: %lu; first byte: %02x (%c); detected filetype: %@",
|
2016-04-13 20:38:42 +02:00
|
|
|
(unsigned long)length_buffered,
|
|
|
|
img_buffer[0],
|
|
|
|
img_buffer[0],
|
|
|
|
file_type);
|
2016-06-23 22:43:31 +02:00
|
|
|
|
|
|
|
if ([file_type isEqualToString:@"image/gif"] && img_data.length <= 5 * 1024 * 1024) {
|
|
|
|
// Media Size constraints lifted from Signal-Android (org/thoughtcrime/securesms/mms/PushMediaConstraints.java)
|
|
|
|
// GifMaxSize return 5 * MB;
|
|
|
|
// For reference, other media size limits we're not explicitly enforcing:
|
|
|
|
// ImageMaxSize return 420 * KB;
|
|
|
|
// VideoMaxSize return 100 * MB;
|
|
|
|
// getAudioMaxSize 100 * MB;
|
|
|
|
DDLogVerbose(@"Sending raw image/gif");
|
|
|
|
[self sendMessageAttachment:img_data ofType:file_type];
|
|
|
|
} else {
|
|
|
|
DDLogVerbose(@"Compressing attachment as image/jpeg");
|
|
|
|
UIImage *pickedImage = [[UIImage alloc] initWithData:img_data];
|
|
|
|
[self sendMessageAttachment:[self qualityAdjustedAttachmentForImage:pickedImage] ofType:@"image/jpeg"];
|
|
|
|
}
|
2016-04-13 20:38:42 +02:00
|
|
|
}
|
|
|
|
failureBlock:^(NSError *error) {
|
|
|
|
DDLogVerbose(@"Couldn't get image asset: %@", error);
|
|
|
|
}];
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)sendMessageAttachment:(NSData *)attachmentData ofType:(NSString *)attachmentType {
|
|
|
|
TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:self.thread
|
|
|
|
messageBody:nil
|
|
|
|
attachments:[NSMutableArray array]];
|
|
|
|
|
|
|
|
[self dismissViewControllerAnimated:YES
|
|
|
|
completion:^{
|
2016-06-23 22:43:31 +02:00
|
|
|
DDLogVerbose(@"Sending attachment. Size in bytes: %lu, contentType: %@",
|
|
|
|
attachmentData.length,
|
|
|
|
attachmentType);
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
[[TSMessagesManager sharedManager] sendAttachment:attachmentData
|
|
|
|
contentType:attachmentType
|
|
|
|
inMessage:message
|
2015-12-26 17:27:27 +01:00
|
|
|
thread:self.thread
|
|
|
|
success:nil
|
|
|
|
failure:nil];
|
2015-12-22 12:45:09 +01:00
|
|
|
}];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSURL *)videoTempFolder {
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
2015-01-14 22:30:01 +01:00
|
|
|
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
basePath = [basePath stringByAppendingPathComponent:@"videos"];
|
2015-01-14 22:30:01 +01:00
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:basePath]) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:basePath
|
|
|
|
withIntermediateDirectories:YES
|
|
|
|
attributes:nil
|
|
|
|
error:nil];
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2015-04-25 16:59:32 +02:00
|
|
|
return [NSURL fileURLWithPath:basePath];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)sendQualityAdjustedAttachment:(NSURL *)movieURL {
|
2015-04-25 16:59:32 +02:00
|
|
|
AVAsset *video = [AVAsset assetWithURL:movieURL];
|
2015-12-22 12:45:09 +01:00
|
|
|
AVAssetExportSession *exportSession =
|
|
|
|
[AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetMediumQuality];
|
2015-04-25 16:59:32 +02:00
|
|
|
exportSession.shouldOptimizeForNetworkUse = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
exportSession.outputFileType = AVFileTypeMPEG4;
|
|
|
|
|
|
|
|
double currentTime = [[NSDate date] timeIntervalSince1970];
|
|
|
|
NSString *strImageName = [NSString stringWithFormat:@"%f", currentTime];
|
|
|
|
NSURL *compressedVideoUrl =
|
|
|
|
[[self videoTempFolder] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", strImageName]];
|
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
exportSession.outputURL = compressedVideoUrl;
|
|
|
|
[exportSession exportAsynchronouslyWithCompletionHandler:^{
|
2015-12-22 12:45:09 +01:00
|
|
|
NSError *error;
|
|
|
|
[self sendMessageAttachment:[NSData dataWithContentsOfURL:compressedVideoUrl] ofType:@"video/mp4"];
|
|
|
|
[[NSFileManager defaultManager] removeItemAtURL:compressedVideoUrl error:&error];
|
|
|
|
if (error) {
|
|
|
|
DDLogWarn(@"Failed to remove cached video file: %@", error.debugDescription);
|
|
|
|
}
|
2015-01-14 22:30:01 +01:00
|
|
|
}];
|
2014-11-25 16:38:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (NSData *)qualityAdjustedAttachmentForImage:(UIImage *)image {
|
2014-12-26 21:17:43 +01:00
|
|
|
return UIImageJPEGRepresentation([self adjustedImageSizedForSending:image], [self compressionRate]);
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UIImage *)adjustedImageSizedForSending:(UIImage *)image {
|
2014-12-26 21:17:43 +01:00
|
|
|
CGFloat correctedWidth;
|
|
|
|
switch ([Environment.preferences imageUploadQuality]) {
|
2014-12-26 23:18:54 +01:00
|
|
|
case TSImageQualityUncropped:
|
|
|
|
return image;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 21:17:43 +01:00
|
|
|
case TSImageQualityHigh:
|
|
|
|
correctedWidth = 2048;
|
|
|
|
break;
|
|
|
|
case TSImageQualityMedium:
|
|
|
|
correctedWidth = 1024;
|
|
|
|
break;
|
|
|
|
case TSImageQualityLow:
|
|
|
|
correctedWidth = 512;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 21:17:43 +01:00
|
|
|
return [self imageScaled:image toMaxSize:correctedWidth];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UIImage *)imageScaled:(UIImage *)image toMaxSize:(CGFloat)size {
|
2014-12-26 21:17:43 +01:00
|
|
|
CGFloat scaleFactor;
|
|
|
|
CGFloat aspectRatio = image.size.height / image.size.width;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (aspectRatio > 1) {
|
2014-12-26 21:17:43 +01:00
|
|
|
scaleFactor = size / image.size.width;
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2014-12-26 21:17:43 +01:00
|
|
|
scaleFactor = size / image.size.height;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 21:17:43 +01:00
|
|
|
CGSize newSize = CGSizeMake(image.size.width * scaleFactor, image.size.height * scaleFactor);
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 21:17:43 +01:00
|
|
|
UIGraphicsBeginImageContext(newSize);
|
|
|
|
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
|
2015-12-22 12:45:09 +01:00
|
|
|
UIImage *updatedImage = UIGraphicsGetImageFromCurrentImageContext();
|
2014-12-26 21:17:43 +01:00
|
|
|
UIGraphicsEndImageContext();
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 21:17:43 +01:00
|
|
|
return updatedImage;
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (CGFloat)compressionRate {
|
2014-12-26 21:17:43 +01:00
|
|
|
switch ([Environment.preferences imageUploadQuality]) {
|
2014-12-26 23:18:54 +01:00
|
|
|
case TSImageQualityUncropped:
|
|
|
|
return 1;
|
2014-12-26 21:17:43 +01:00
|
|
|
case TSImageQualityHigh:
|
|
|
|
return 0.9f;
|
|
|
|
case TSImageQualityMedium:
|
|
|
|
return 0.5f;
|
|
|
|
case TSImageQualityLow:
|
|
|
|
return 0.3f;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
#pragma mark Storage access
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (YapDatabaseConnection *)uiDatabaseConnection {
|
2014-11-25 16:38:33 +01:00
|
|
|
NSAssert([NSThread isMainThread], @"Must access uiDatabaseConnection on main thread!");
|
|
|
|
if (!_uiDatabaseConnection) {
|
|
|
|
_uiDatabaseConnection = [[TSStorageManager sharedManager] newDatabaseConnection];
|
|
|
|
[_uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
}
|
|
|
|
return _uiDatabaseConnection;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (YapDatabaseConnection *)editingDatabaseConnection {
|
2014-12-06 17:45:42 +01:00
|
|
|
if (!_editingDatabaseConnection) {
|
|
|
|
_editingDatabaseConnection = [[TSStorageManager sharedManager] newDatabaseConnection];
|
|
|
|
}
|
|
|
|
return _editingDatabaseConnection;
|
|
|
|
}
|
|
|
|
|
2014-12-24 02:25:10 +01:00
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)yapDatabaseModified:(NSNotification *)notification {
|
2015-10-31 16:53:32 +01:00
|
|
|
[self updateBackButtonAsync];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (isGroupConversation) {
|
2014-12-24 02:25:10 +01:00
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
TSGroupThread *gThread = (TSGroupThread *)self.thread;
|
2015-12-26 17:27:27 +01:00
|
|
|
|
|
|
|
if (gThread.groupModel) {
|
|
|
|
self.thread = [TSGroupThread threadWithGroupModel:gThread.groupModel transaction:transaction];
|
|
|
|
}
|
2014-12-24 02:25:10 +01:00
|
|
|
}];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
NSArray *notifications = [self.uiDatabaseConnection beginLongLivedReadTransaction];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (![[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName]
|
|
|
|
hasChangesForNotifications:notifications]) {
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
[self.messageMappings updateWithTransaction:transaction];
|
2015-01-31 12:00:58 +01:00
|
|
|
}];
|
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
NSArray *messageRowChanges = nil;
|
2015-01-31 12:00:58 +01:00
|
|
|
NSArray *sectionChanges = nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
|
2015-01-31 12:00:58 +01:00
|
|
|
[[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] getSectionChanges:§ionChanges
|
2014-11-25 16:38:33 +01:00
|
|
|
rowChanges:&messageRowChanges
|
|
|
|
forNotifications:notifications
|
|
|
|
withMappings:self.messageMappings];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-05 16:31:00 +01:00
|
|
|
__block BOOL scrollToBottom = NO;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
if ([sectionChanges count] == 0 & [messageRowChanges count] == 0) {
|
2014-12-08 23:12:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-12 22:41:28 +01:00
|
|
|
[self.collectionView performBatchUpdates:^{
|
2015-12-22 12:45:09 +01:00
|
|
|
for (YapDatabaseViewRowChange *rowChange in messageRowChanges) {
|
|
|
|
switch (rowChange.type) {
|
|
|
|
case YapDatabaseViewChangeDelete: {
|
|
|
|
[self.collectionView deleteItemsAtIndexPaths:@[ rowChange.indexPath ]];
|
2016-04-18 19:07:35 +02:00
|
|
|
|
|
|
|
YapCollectionKey *collectionKey = rowChange.collectionKey;
|
|
|
|
if (collectionKey.key) {
|
|
|
|
[self.messageAdapterCache removeObjectForKey:collectionKey.key];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case YapDatabaseViewChangeInsert: {
|
|
|
|
[self.collectionView insertItemsAtIndexPaths:@[ rowChange.newIndexPath ]];
|
|
|
|
scrollToBottom = YES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case YapDatabaseViewChangeMove: {
|
|
|
|
[self.collectionView deleteItemsAtIndexPaths:@[ rowChange.indexPath ]];
|
|
|
|
[self.collectionView insertItemsAtIndexPaths:@[ rowChange.newIndexPath ]];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case YapDatabaseViewChangeUpdate: {
|
2016-04-18 19:07:35 +02:00
|
|
|
YapCollectionKey *collectionKey = rowChange.collectionKey;
|
|
|
|
if (collectionKey.key) {
|
|
|
|
[self.messageAdapterCache removeObjectForKey:collectionKey.key];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
NSMutableArray *rowsToUpdate = [@[ rowChange.indexPath ] mutableCopy];
|
|
|
|
|
|
|
|
if (_lastDeliveredMessageIndexPath) {
|
|
|
|
[rowsToUpdate addObject:_lastDeliveredMessageIndexPath];
|
|
|
|
}
|
|
|
|
[self.collectionView reloadItemsAtIndexPaths:rowsToUpdate];
|
|
|
|
scrollToBottom = YES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completion:^(BOOL success) {
|
|
|
|
if (!success) {
|
|
|
|
[self.collectionView.collectionViewLayout
|
|
|
|
invalidateLayoutWithContext:[JSQMessagesCollectionViewFlowLayoutInvalidationContext context]];
|
|
|
|
[self.collectionView reloadData];
|
|
|
|
}
|
|
|
|
if (scrollToBottom) {
|
|
|
|
[self scrollToBottomAnimated:YES];
|
|
|
|
}
|
|
|
|
}];
|
2014-11-25 16:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionView DataSource
|
2015-03-19 01:59:44 +01:00
|
|
|
|
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
2014-12-31 21:30:20 +01:00
|
|
|
NSInteger numberOfMessages = (NSInteger)[self.messageMappings numberOfItemsInSection:(NSUInteger)section];
|
2014-11-25 16:38:33 +01:00
|
|
|
return numberOfMessages;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
2014-11-25 16:38:33 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (TSInteraction *)interactionAtIndexPath:(NSIndexPath *)indexPath {
|
2014-11-25 16:38:33 +01:00
|
|
|
__block TSInteraction *message = nil;
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
YapDatabaseViewTransaction *viewTransaction = [transaction ext:TSMessageDatabaseViewExtensionName];
|
|
|
|
NSParameterAssert(viewTransaction != nil);
|
|
|
|
NSParameterAssert(self.messageMappings != nil);
|
|
|
|
NSParameterAssert(indexPath != nil);
|
|
|
|
NSUInteger row = (NSUInteger)indexPath.row;
|
|
|
|
NSUInteger section = (NSUInteger)indexPath.section;
|
|
|
|
NSUInteger numberOfItemsInSection = [self.messageMappings numberOfItemsInSection:section];
|
|
|
|
|
|
|
|
NSAssert(row < numberOfItemsInSection,
|
|
|
|
@"Cannot fetch message because row %d is >= numberOfItemsInSection %d",
|
|
|
|
(int)row,
|
|
|
|
(int)numberOfItemsInSection);
|
|
|
|
|
|
|
|
message = [viewTransaction objectAtRow:row inSection:section withMappings:self.messageMappings];
|
|
|
|
NSParameterAssert(message != nil);
|
2014-11-25 16:38:33 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-11 00:05:41 +01:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (TSMessageAdapter *)messageAtIndexPath:(NSIndexPath *)indexPath {
|
2015-12-26 17:27:27 +01:00
|
|
|
TSInteraction *interaction = [self interactionAtIndexPath:indexPath];
|
2016-04-13 19:05:09 +02:00
|
|
|
|
|
|
|
TSMessageAdapter *messageAdapter = [self.messageAdapterCache objectForKey:interaction.uniqueId];
|
|
|
|
|
|
|
|
if (messageAdapter == nil) {
|
|
|
|
messageAdapter = [TSMessageAdapter messageViewDataWithInteraction:interaction inThread:self.thread];
|
|
|
|
[self.messageAdapterCache setObject:messageAdapter forKey: interaction.uniqueId];
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageAdapter;
|
2014-11-25 16:38:33 +01:00
|
|
|
}
|
2014-12-31 13:22:40 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
#pragma mark group action view
|
2015-01-14 22:30:01 +01:00
|
|
|
|
2014-12-17 06:44:36 +01:00
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
#pragma mark - Audio
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)recordAudio {
|
2015-01-22 05:08:12 +01:00
|
|
|
// Define the recorder setting
|
2015-12-22 12:45:09 +01:00
|
|
|
NSArray *pathComponents = [NSArray
|
|
|
|
arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
|
|
|
|
[NSString stringWithFormat:@"%lld.m4a", [NSDate ows_millisecondTimeStamp]],
|
|
|
|
nil];
|
2015-01-22 05:08:12 +01:00
|
|
|
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
// Setup audio session
|
|
|
|
AVAudioSession *session = [AVAudioSession sharedInstance];
|
|
|
|
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
|
|
|
|
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
|
|
|
|
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
|
2015-12-22 12:45:09 +01:00
|
|
|
[recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
|
|
|
|
|
2015-01-22 05:08:12 +01:00
|
|
|
// Initiate and prepare the recorder
|
2015-12-22 12:45:09 +01:00
|
|
|
_audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
|
2015-01-22 05:08:12 +01:00
|
|
|
_audioRecorder.delegate = self;
|
|
|
|
_audioRecorder.meteringEnabled = YES;
|
|
|
|
[_audioRecorder prepareToRecord];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)audioPlayerUpdated:(NSTimer *)timer {
|
|
|
|
double current = [_audioPlayer currentTime] / [_audioPlayer duration];
|
2015-01-25 00:48:40 +01:00
|
|
|
double interval = [_audioPlayer duration] - [_audioPlayer currentTime];
|
|
|
|
[_currentMediaAdapter setDurationOfAudio:interval];
|
|
|
|
[_currentMediaAdapter setAudioProgressFromFloat:(float)current];
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
|
2015-01-22 05:08:12 +01:00
|
|
|
[_audioPlayerPoller invalidate];
|
2015-01-25 00:48:40 +01:00
|
|
|
[_currentMediaAdapter setAudioProgressFromFloat:0];
|
|
|
|
[_currentMediaAdapter setDurationOfAudio:_audioPlayer.duration];
|
|
|
|
[_currentMediaAdapter setAudioIconToPlay];
|
2015-01-22 05:08:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
|
|
|
|
if (flag) {
|
2015-01-22 05:08:12 +01:00
|
|
|
[self sendMessageAttachment:[NSData dataWithContentsOfURL:recorder.url] ofType:@"audio/m4a"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
#pragma mark Accessory View
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)didPressAccessoryButton:(UIButton *)sender {
|
2015-01-30 23:28:05 +01:00
|
|
|
[self dismissKeyBoard];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
UIView *presenter = self.parentViewController.view;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
[DJWActionSheet showInView:presenter
|
|
|
|
withTitle:nil
|
2015-02-18 23:21:03 +01:00
|
|
|
cancelButtonTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", @"")
|
2014-11-25 16:38:33 +01:00
|
|
|
destructiveButtonTitle:nil
|
2015-12-22 12:45:09 +01:00
|
|
|
otherButtonTitles:@[
|
|
|
|
NSLocalizedString(@"TAKE_MEDIA_BUTTON", @""),
|
|
|
|
NSLocalizedString(@"CHOOSE_MEDIA_BUTTON", @"")
|
|
|
|
] //,@"Record audio"]
|
2014-11-25 16:38:33 +01:00
|
|
|
tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) {
|
2015-12-22 12:45:09 +01:00
|
|
|
if (tappedButtonIndex == actionSheet.cancelButtonIndex) {
|
|
|
|
DDLogVerbose(@"User Cancelled");
|
|
|
|
} else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) {
|
|
|
|
DDLogVerbose(@"Destructive button tapped");
|
|
|
|
} else {
|
|
|
|
switch (tappedButtonIndex) {
|
|
|
|
case 0:
|
|
|
|
[self takePictureOrVideo];
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
[self chooseFromLibrary];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
[self recordAudio];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 16:38:33 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-12-06 17:45:42 +01:00
|
|
|
- (void)markAllMessagesAsRead {
|
2015-12-26 17:27:27 +01:00
|
|
|
[self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[self.thread markAllAsReadWithTransaction:transaction];
|
2014-12-06 17:45:42 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (BOOL)collectionView:(UICollectionView *)collectionView
|
|
|
|
canPerformAction:(SEL)action
|
|
|
|
forItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
withSender:(id)sender {
|
2014-12-26 23:18:54 +01:00
|
|
|
if (action == @selector(delete:)) {
|
|
|
|
return YES;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-12-26 23:18:54 +01:00
|
|
|
return [super collectionView:collectionView canPerformAction:action forItemAtIndexPath:indexPath withSender:sender];
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)collectionView:(UICollectionView *)collectionView
|
|
|
|
performAction:(SEL)action
|
|
|
|
forItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
withSender:(id)sender {
|
2014-12-26 23:18:54 +01:00
|
|
|
if (action == @selector(delete:)) {
|
|
|
|
[self deleteMessageAtIndexPath:indexPath];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2014-12-26 23:18:54 +01:00
|
|
|
[super collectionView:collectionView performAction:action forItemAtIndexPath:indexPath withSender:sender];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 17:49:46 +01:00
|
|
|
- (void)updateGroup {
|
2015-01-14 22:30:01 +01:00
|
|
|
[self.navController hideDropDown:self];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-01-14 22:30:01 +01:00
|
|
|
[self performSegueWithIdentifier:kUpdateGroupSegueIdentifier sender:self];
|
|
|
|
}
|
|
|
|
|
2015-02-28 17:49:46 +01:00
|
|
|
- (void)leaveGroup {
|
2015-01-14 22:30:01 +01:00
|
|
|
[self.navController hideDropDown:self];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
TSGroupThread *gThread = (TSGroupThread *)_thread;
|
|
|
|
TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:gThread
|
|
|
|
messageBody:@""
|
|
|
|
attachments:[[NSMutableArray alloc] init]];
|
2014-12-24 02:25:10 +01:00
|
|
|
message.groupMetaMessage = TSGroupMessageQuit;
|
2015-09-01 19:22:08 +02:00
|
|
|
[[TSMessagesManager sharedManager] sendMessage:message inThread:gThread success:nil failure:nil];
|
2014-12-24 02:25:10 +01:00
|
|
|
[self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
NSMutableArray *newGroupMemberIds = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds];
|
|
|
|
[newGroupMemberIds removeObject:[TSAccountManager localNumber]];
|
|
|
|
gThread.groupModel.groupMemberIds = newGroupMemberIds;
|
|
|
|
[gThread saveWithTransaction:transaction];
|
2014-12-24 02:25:10 +01:00
|
|
|
}];
|
2015-01-14 22:30:01 +01:00
|
|
|
[self hideInputIfNeeded];
|
2014-12-24 02:25:10 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)updateGroupModelTo:(TSGroupModel *)newGroupModel {
|
|
|
|
__block TSGroupThread *groupThread;
|
2015-02-17 00:14:50 +01:00
|
|
|
__block TSOutgoingMessage *message;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
|
2014-12-24 02:25:10 +01:00
|
|
|
[self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
groupThread = [TSGroupThread getOrCreateThreadWithGroupModel:newGroupModel transaction:transaction];
|
|
|
|
groupThread.groupModel = newGroupModel;
|
|
|
|
[groupThread saveWithTransaction:transaction];
|
|
|
|
message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:groupThread
|
|
|
|
messageBody:@""
|
|
|
|
attachments:[[NSMutableArray alloc] init]];
|
|
|
|
message.groupMetaMessage = TSGroupMessageUpdate;
|
2014-12-24 02:25:10 +01:00
|
|
|
}];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (newGroupModel.groupImage != nil) {
|
|
|
|
[[TSMessagesManager sharedManager] sendAttachment:UIImagePNGRepresentation(newGroupModel.groupImage)
|
|
|
|
contentType:@"image/png"
|
|
|
|
inMessage:message
|
2015-12-26 17:27:27 +01:00
|
|
|
thread:groupThread
|
|
|
|
success:nil
|
|
|
|
failure:nil];
|
2015-12-22 12:45:09 +01:00
|
|
|
} else {
|
2015-09-01 19:22:08 +02:00
|
|
|
[[TSMessagesManager sharedManager] sendMessage:message inThread:groupThread success:nil failure:nil];
|
2015-02-17 00:14:50 +01:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-02-17 00:14:50 +01:00
|
|
|
self.thread = groupThread;
|
2014-12-24 02:25:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (IBAction)unwindGroupUpdated:(UIStoryboardSegue *)segue {
|
2015-12-22 12:45:09 +01:00
|
|
|
NewGroupViewController *ngc = [segue sourceViewController];
|
|
|
|
TSGroupModel *newGroupModel = [ngc groupModel];
|
|
|
|
NSMutableSet *groupMemberIds = [NSMutableSet setWithArray:newGroupModel.groupMemberIds];
|
|
|
|
[groupMemberIds addObject:[TSAccountManager localNumber]];
|
2015-02-28 17:49:46 +01:00
|
|
|
newGroupModel.groupMemberIds = [NSMutableArray arrayWithArray:[groupMemberIds allObjects]];
|
2014-12-24 02:25:10 +01:00
|
|
|
[self updateGroupModelTo:newGroupModel];
|
2015-12-22 12:45:09 +01:00
|
|
|
[self.collectionView.collectionViewLayout
|
|
|
|
invalidateLayoutWithContext:[JSQMessagesCollectionViewFlowLayoutInvalidationContext context]];
|
2015-02-28 17:49:46 +01:00
|
|
|
[self.collectionView reloadData];
|
2014-12-24 02:25:10 +01:00
|
|
|
}
|
|
|
|
|
2015-04-14 21:49:00 +02:00
|
|
|
- (void)popKeyBoard {
|
|
|
|
[self.inputToolbar.contentView.textView becomeFirstResponder];
|
|
|
|
}
|
|
|
|
|
2015-01-30 23:28:05 +01:00
|
|
|
- (void)dismissKeyBoard {
|
|
|
|
[self.inputToolbar.contentView.textView resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
2015-03-01 00:04:39 +01:00
|
|
|
#pragma mark Drafts
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)loadDraftInCompose {
|
2015-03-01 00:04:39 +01:00
|
|
|
__block NSString *placeholder;
|
|
|
|
[self.editingDatabaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
placeholder = [_thread currentDraftWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
completionBlock:^{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-03-01 00:04:39 +01:00
|
|
|
[self.inputToolbar.contentView.textView setText:placeholder];
|
|
|
|
[self textViewDidChange:self.inputToolbar.contentView.textView];
|
2015-12-22 12:45:09 +01:00
|
|
|
});
|
|
|
|
}];
|
2015-03-01 00:04:39 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 01:59:44 +01:00
|
|
|
- (void)saveDraft {
|
2015-03-01 00:04:39 +01:00
|
|
|
if (self.inputToolbar.hidden == NO) {
|
2015-03-21 19:15:43 +01:00
|
|
|
__block TSThread *thread = _thread;
|
|
|
|
__block NSString *currentDraft = self.inputToolbar.contentView.textView.text;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-03-21 19:15:43 +01:00
|
|
|
[self.editingDatabaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-12-22 12:45:09 +01:00
|
|
|
[thread setDraft:currentDraft transaction:transaction];
|
2015-03-01 00:04:39 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
#pragma mark Unread Badge
|
|
|
|
|
|
|
|
- (void)setUnreadCount:(NSUInteger)unreadCount {
|
|
|
|
if (_unreadCount != unreadCount) {
|
|
|
|
_unreadCount = unreadCount;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
if (_unreadCount > 0) {
|
|
|
|
if (_unreadContainer == nil) {
|
|
|
|
static UIImage *backgroundImage = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
2015-12-22 12:45:09 +01:00
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
UIGraphicsBeginImageContextWithOptions(CGSizeMake(17.0f, 17.0f), false, 0.0f);
|
|
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
|
|
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
|
|
|
|
CGContextFillEllipseInRect(context, CGRectMake(0.0f, 0.0f, 17.0f, 17.0f));
|
|
|
|
backgroundImage =
|
|
|
|
[UIGraphicsGetImageFromCurrentImageContext() stretchableImageWithLeftCapWidth:8 topCapHeight:8];
|
|
|
|
UIGraphicsEndImageContext();
|
|
|
|
});
|
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
_unreadContainer = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 10.0f)];
|
|
|
|
_unreadContainer.userInteractionEnabled = NO;
|
|
|
|
_unreadContainer.layer.zPosition = 2000;
|
|
|
|
[self.navigationController.navigationBar addSubview:_unreadContainer];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
_unreadBackground = [[UIImageView alloc] initWithImage:backgroundImage];
|
|
|
|
[_unreadContainer addSubview:_unreadBackground];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
_unreadLabel = [[UILabel alloc] init];
|
|
|
|
_unreadLabel.backgroundColor = [UIColor clearColor];
|
|
|
|
_unreadLabel.textColor = [UIColor whiteColor];
|
|
|
|
_unreadLabel.font = [UIFont systemFontOfSize:12];
|
|
|
|
[_unreadContainer addSubview:_unreadLabel];
|
|
|
|
}
|
|
|
|
_unreadContainer.hidden = false;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
_unreadLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)unreadCount];
|
|
|
|
[_unreadLabel sizeToFit];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-05-23 15:54:50 +02:00
|
|
|
CGPoint offset = CGPointMake(17.0f, 2.0f);
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
_unreadBackground.frame =
|
|
|
|
CGRectMake(offset.x, offset.y, MAX(_unreadLabel.frame.size.width + 8.0f, 17.0f), 17.0f);
|
|
|
|
_unreadLabel.frame = CGRectMake(
|
|
|
|
offset.x +
|
|
|
|
floor((2.0f * (_unreadBackground.frame.size.width - _unreadLabel.frame.size.width) / 2.0f) / 2.0f),
|
|
|
|
offset.y + 1.0f,
|
|
|
|
_unreadLabel.frame.size.width,
|
|
|
|
_unreadLabel.frame.size.height);
|
2015-05-23 15:54:50 +02:00
|
|
|
} else if (_unreadContainer != nil) {
|
|
|
|
_unreadContainer.hidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-26 17:27:27 +01:00
|
|
|
#pragma mark 3D Touch Preview Actions
|
|
|
|
|
|
|
|
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
|
|
|
|
return @[];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@end
|