Support captions *and* independent message body

This commit is contained in:
Michael Kirk 2018-11-20 17:08:55 -06:00
parent cd88ef2bec
commit 4f0092615a
3 changed files with 93 additions and 121 deletions

View File

@ -2481,7 +2481,7 @@ typedef enum : NSUInteger {
{
OWSAssertDebug(attachment);
[self tryToSendAttachmentIfApproved:attachment];
[self showApprovalDialogForAttachment:attachment];
[ThreadUtil addThreadToProfileWhitelistIfEmptyContactThread:self.thread];
[self.conversationViewModel ensureDynamicInteractions];
@ -2580,14 +2580,14 @@ typedef enum : NSUInteger {
// Although we want to be able to send higher quality attachments through the document picker
// it's more imporant that we ensure the sent format is one all clients can accept (e.g. *not* quicktime .mov)
if ([SignalAttachment isInvalidVideoWithDataSource:dataSource dataUTI:type]) {
[self sendQualityAdjustedAttachmentForVideo:url filename:filename skipApprovalDialog:NO];
[self showApprovalDialogAfterProcessingVideoURL:url filename:filename];
return;
}
// "Document picker" attachments _SHOULD NOT_ be resized, if possible.
SignalAttachment *attachment =
[SignalAttachment attachmentWithDataSource:dataSource dataUTI:type imageQuality:TSImageQualityOriginal];
[self tryToSendAttachmentIfApproved:attachment];
[self showApprovalDialogForAttachment:attachment];
}
#pragma mark - UIImagePickerController
@ -2673,12 +2673,17 @@ typedef enum : NSUInteger {
self.view.frame = frame;
}
#pragma mark - OWSImagePickerControllerDelegate
- (void)imagePicker:(OWSImagePickerGridController *)imagePicker
didPickImageAttachments:(NSArray<SignalAttachment *> *)attachments
messageText:(NSString *_Nullable)messageText
{
[self tryToSendAttachmentsIfApproved:attachments skipApprovalDialog:YES];
[self tryToSendAttachments:attachments messageText:messageText];
}
#pragma mark - UIImagePickerControllerDelegate
/*
* Fetching data from UIImagePickerController
*/
@ -2725,9 +2730,7 @@ typedef enum : NSUInteger {
NSURL *videoURL = info[UIImagePickerControllerMediaURL];
[self dismissViewControllerAnimated:YES
completion:^{
[self sendQualityAdjustedAttachmentForVideo:videoURL
filename:filename
skipApprovalDialog:NO];
[self showApprovalDialogAfterProcessingVideoURL:videoURL filename:filename];
}];
} else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// Static Image captured from camera
@ -2751,7 +2754,7 @@ typedef enum : NSUInteger {
[self showErrorAlertForAttachment:attachment];
failedToPickAttachment(nil);
} else {
[self tryToSendAttachmentIfApproved:attachment skipApprovalDialog:NO];
[self showApprovalDialogForAttachment:attachment];
}
} else {
failedToPickAttachment(nil);
@ -2808,34 +2811,13 @@ typedef enum : NSUInteger {
[self showErrorAlertForAttachment:attachment];
failedToPickAttachment(nil);
} else {
[self tryToSendAttachmentIfApproved:attachment];
[self showApprovalDialogForAttachment:attachment];
}
}];
}];
}
}
- (void)sendMessageAttachments:(NSArray<SignalAttachment *> *)attachments
{
OWSAssertIsOnMainThread();
for (SignalAttachment *attachment in attachments) {
OWSAssertDebug(![attachment hasError]);
OWSAssertDebug([attachment mimeType].length > 0);
}
BOOL didAddToProfileWhitelist = [ThreadUtil addThreadToProfileWhitelistIfEmptyContactThread:self.thread];
TSOutgoingMessage *message = [ThreadUtil enqueueMessageWithAttachments:attachments
messageBody:nil
inThread:self.thread
quotedReplyModel:self.inputToolbar.quotedReply];
[self messageWasSent:message];
if (didAddToProfileWhitelist) {
[self.conversationViewModel ensureDynamicInteractions];
}
}
- (void)sendContactShare:(ContactShareViewModel *)contactShare
{
OWSAssertIsOnMainThread();
@ -2864,9 +2846,7 @@ typedef enum : NSUInteger {
}];
}
- (void)sendQualityAdjustedAttachmentForVideo:(NSURL *)movieURL
filename:(nullable NSString *)filename
skipApprovalDialog:(BOOL)skipApprovalDialog
- (void)showApprovalDialogAfterProcessingVideoURL:(NSURL *)movieURL filename:(nullable NSString *)filename
{
OWSAssertIsOnMainThread();
@ -2895,7 +2875,7 @@ typedef enum : NSUInteger {
attachment ? [attachment errorName] : @"Missing data");
[self showErrorAlertForAttachment:attachment];
} else {
[self tryToSendAttachmentIfApproved:attachment skipApprovalDialog:skipApprovalDialog];
[self showApprovalDialogForAttachment:attachment];
}
}];
}) retainUntilComplete];
@ -3086,7 +3066,7 @@ typedef enum : NSUInteger {
OWSLogWarn(@"Invalid attachment: %@.", attachment ? [attachment errorName] : @"Missing data");
[self showErrorAlertForAttachment:attachment];
} else {
[self tryToSendAttachmentIfApproved:attachment skipApprovalDialog:YES];
[self tryToSendAttachments:@[ attachment ] messageText:nil];
}
}
@ -3480,37 +3460,28 @@ typedef enum : NSUInteger {
{
OWSLogError(@"");
[self tryToSendAttachmentIfApproved:attachment];
[self showApprovalDialogForAttachment:attachment];
}
- (void)tryToSendAttachmentIfApproved:(SignalAttachment *_Nullable)attachment
- (void)showApprovalDialogForAttachment:(SignalAttachment *_Nullable)attachment
{
if (attachment == nil) {
OWSLogWarn(@"Missing attachment");
OWSFailDebug(@"attachment was unexpetedly nil");
[self showErrorAlertForAttachment:nil];
return;
}
[self tryToSendAttachmentsIfApproved:@[ attachment ]];
[self showApprovalDialogForAttachments:@[ attachment ]];
}
- (void)tryToSendAttachmentIfApproved:(SignalAttachment *_Nullable)attachment
skipApprovalDialog:(BOOL)skipApprovalDialog
- (void)showApprovalDialogForAttachments:(NSArray<SignalAttachment *> *)attachments
{
if (attachment == nil) {
OWSLogWarn(@"Missing attachment");
[self showErrorAlertForAttachment:nil];
return;
}
[self tryToSendAttachmentsIfApproved:@[ attachment ] skipApprovalDialog:skipApprovalDialog];
OWSNavigationController *modal =
[AttachmentApprovalViewController wrappedInNavControllerWithAttachments:attachments approvalDelegate:self];
[self presentViewController:modal animated:YES completion:nil];
}
- (void)tryToSendAttachmentsIfApproved:(NSArray<SignalAttachment *> *)attachments
{
[self tryToSendAttachmentsIfApproved:attachments skipApprovalDialog:NO];
}
- (void)tryToSendAttachmentsIfApproved:(NSArray<SignalAttachment *> *)attachments
skipApprovalDialog:(BOOL)skipApprovalDialog
- (void)tryToSendAttachments:(NSArray<SignalAttachment *> *)attachments messageText:(NSString *_Nullable)messageText
{
OWSLogError(@"");
@ -3519,7 +3490,7 @@ typedef enum : NSUInteger {
if ([self isBlockedConversation]) {
[self showUnblockConversationUI:^(BOOL isBlocked) {
if (!isBlocked) {
[weakSelf tryToSendAttachmentsIfApproved:attachments];
[weakSelf tryToSendAttachments:attachments messageText:messageText];
}
}];
return;
@ -3529,8 +3500,8 @@ typedef enum : NSUInteger {
showSafetyNumberConfirmationIfNecessaryWithConfirmationText:[SafetyNumberStrings confirmSendButton]
completion:^(BOOL didConfirmIdentity) {
if (didConfirmIdentity) {
[weakSelf
tryToSendAttachmentsIfApproved:attachments];
[weakSelf tryToSendAttachments:attachments
messageText:messageText];
}
}];
if (didShowSNAlert) {
@ -3545,13 +3516,16 @@ typedef enum : NSUInteger {
}
}
if (skipApprovalDialog) {
[self sendMessageAttachments:attachments];
} else {
OWSNavigationController *modal =
[AttachmentApprovalViewController wrappedInNavControllerWithAttachments:attachments
approvalDelegate:self];
[self presentViewController:modal animated:YES completion:nil];
BOOL didAddToProfileWhitelist = [ThreadUtil addThreadToProfileWhitelistIfEmptyContactThread:self.thread];
TSOutgoingMessage *message = [ThreadUtil enqueueMessageWithAttachments:attachments
messageBody:messageText
inThread:self.thread
quotedReplyModel:self.inputToolbar.quotedReply];
[self messageWasSent:message];
if (didAddToProfileWhitelist) {
[self.conversationViewModel ensureDynamicInteractions];
}
});
}
@ -3660,10 +3634,13 @@ typedef enum : NSUInteger {
[self updateNavigationBarSubtitleLabel];
}
#pragma mark - AttachmentApprovalViewControllerDelegate
- (void)attachmentApproval:(AttachmentApprovalViewController *)attachmentApproval
didApproveAttachments:(NSArray<SignalAttachment *> *)attachments
messageText:(NSString *_Nullable)messageText
{
[self sendMessageAttachments:attachments];
[self tryToSendAttachments:attachments messageText:messageText];
[self dismissViewControllerAnimated:YES completion:nil];
// We always want to scroll to the bottom of the conversation after the local user
// sends a message. Normally, this is taken care of in yapDatabaseModified:, but

View File

@ -8,7 +8,7 @@ import PromiseKit
@objc(OWSImagePickerControllerDelegate)
protocol ImagePickerControllerDelegate {
func imagePicker(_ imagePicker: ImagePickerGridController, didPickImageAttachments attachments: [SignalAttachment])
func imagePicker(_ imagePicker: ImagePickerGridController, didPickImageAttachments attachments: [SignalAttachment], messageText: String?)
}
@objc(OWSImagePickerGridController)
@ -386,9 +386,9 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
// MARK: - AttachmentApprovalViewControllerDelegate
func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didApproveAttachments attachments: [SignalAttachment]) {
func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didApproveAttachments attachments: [SignalAttachment], messageText: String?) {
self.dismiss(animated: true) {
self.delegate?.imagePicker(self, didPickImageAttachments: attachments)
self.delegate?.imagePicker(self, didPickImageAttachments: attachments, messageText: messageText)
}
}

View File

@ -9,7 +9,7 @@ import PromiseKit
@objc
public protocol AttachmentApprovalViewControllerDelegate: class {
func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didApproveAttachments attachments: [SignalAttachment])
func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didApproveAttachments attachments: [SignalAttachment], messageText: String?)
func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didCancelAttachments attachments: [SignalAttachment])
@objc optional func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, addMoreToAttachments attachments: [SignalAttachment])
@objc optional func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, changedCaptionOfAttachment attachment: SignalAttachment)
@ -62,6 +62,10 @@ class SignalAttachmentItem: Hashable {
// MARK:
var captionText: String? {
return attachment.captionText
}
var imageSize: CGSize = .zero
func getThumbnailImage() -> Promise<UIImage> {
@ -100,7 +104,7 @@ public enum AttachmentApprovalViewControllerMode: UInt {
}
@objc
public class AttachmentApprovalViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, MediaMessageTextToolbarDelegate {
public class AttachmentApprovalViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
// MARK: - Properties
@ -193,8 +197,6 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
}
self.setCurrentItem(firstItem, direction: .forward, animated: false)
mediaMessageTextToolbar.messageText = currentViewController.attachment.captionText
}
override public func viewWillAppear(_ animated: Bool) {
@ -335,13 +337,6 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
}
if transitionCompleted {
UIView.transition(with: self.mediaMessageTextToolbar,
duration: 0.1,
options: .transitionCrossDissolve,
animations: {
self.mediaMessageTextToolbar.messageText = self.currentViewController.attachment.captionText
},
completion: nil)
previousPage.zoomOut(animated: false)
updateMediaRail()
}
@ -411,6 +406,7 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
Logger.debug("cache miss.")
let viewController = AttachmentPrepViewController(attachmentItem: item)
viewController.prepDelegate = self
cachedPages[item] = viewController
return viewController
@ -483,9 +479,9 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
@objc func cancelPressed(sender: UIButton) {
self.approvalDelegate?.attachmentApproval(self, didCancelAttachments: attachments)
}
}
// MARK: - MediaMessageTextToolbarDelegate
extension AttachmentApprovalViewController: MediaMessageTextToolbarDelegate {
var currentPageController: AttachmentPrepViewController {
return viewControllers!.first as! AttachmentPrepViewController
}
@ -506,13 +502,7 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
mediaMessageTextToolbar.isUserInteractionEnabled = false
mediaMessageTextToolbar.isHidden = true
approvalDelegate?.attachmentApproval(self, didApproveAttachments: attachments)
}
func mediaMessageTextToolbar(_ mediaMessageTextToolbar: MediaMessageTextToolbar, textViewDidChange textView: UITextView) {
currentItem.attachment.captionText = textView.text
self.approvalDelegate?.attachmentApproval?(self, changedCaptionOfAttachment: currentItem.attachment)
approvalDelegate?.attachmentApproval(self, didApproveAttachments: attachments, messageText: mediaMessageTextToolbar.messageText)
}
func mediaMessageTextToolbarDidAddMore(_ mediaMessageTextToolbar: MediaMessageTextToolbar) {
@ -520,6 +510,12 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
}
}
extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate {
func prepViewController(_ prepViewController: AttachmentPrepViewController, didUpdateCaptionForAttachmentItem attachmentItem: SignalAttachmentItem) {
self.approvalDelegate?.attachmentApproval?(self, changedCaptionOfAttachment: attachmentItem.attachment)
}
}
// MARK: GalleryRail
extension SignalAttachmentItem: GalleryRailItem {
@ -563,6 +559,10 @@ extension AttachmentApprovalViewController: GalleryRailViewDelegate {
// MARK: - Individual Page
protocol AttachmentPrepViewControllerDelegate: class {
func prepViewController(_ prepViewController: AttachmentPrepViewController, didUpdateCaptionForAttachmentItem attachmentItem: SignalAttachmentItem)
}
public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarDelegate, OWSVideoPlayerDelegate {
// We sometimes shrink the attachment view so that it remains somewhat visible
// when the keyboard is presented.
@ -572,6 +572,8 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
// MARK: - Properties
weak var prepDelegate: AttachmentPrepViewControllerDelegate?
let attachmentItem: SignalAttachmentItem
var attachment: SignalAttachment {
return attachmentItem.attachment
@ -598,7 +600,9 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
// MARK: - View Lifecycle
let captionView = CaptionView()
lazy var captionView: CaptionView = {
return CaptionView(attachmentItem: attachmentItem)
}()
override public func loadView() {
self.view = UIView()
@ -703,9 +707,9 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
// Caption
captionView.captionText = attachment.captionText
view.addSubview(captionView)
captionView.delegate = self
captionView.autoPinWidthToSuperview()
// MJK TODO ideal CaptionView placement
@ -880,6 +884,14 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
}
}
extension AttachmentPrepViewController: CaptionViewDelegate {
func captionView(_ captionView: CaptionView, didChangeCaptionText captionText: String?, attachmentItem: SignalAttachmentItem) {
let attachment = attachmentItem.attachment
attachment.captionText = captionText
prepDelegate?.prepViewController(self, didUpdateCaptionForAttachmentItem: attachmentItem)
}
}
extension AttachmentPrepViewController: UIScrollViewDelegate {
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
@ -997,7 +1009,7 @@ class BottomToolView: UIView {
}
protocol CaptionViewDelegate: class {
func captionViewDidChange(_ captionView: CaptionView)
func captionView(_ captionView: CaptionView, didChangeCaptionText captionText: String?, attachmentItem: SignalAttachmentItem)
}
class CaptionView: UIView {
@ -1010,6 +1022,11 @@ class CaptionView: UIView {
}
}
let attachmentItem: SignalAttachmentItem
var attachment: SignalAttachment {
return attachmentItem.attachment
}
weak var delegate: CaptionViewDelegate?
private let kMinTextViewHeight: CGFloat = 38
@ -1020,8 +1037,12 @@ class CaptionView: UIView {
// MARK: Initializers
override init(frame: CGRect) {
super.init(frame: frame)
init(attachmentItem: SignalAttachmentItem) {
self.attachmentItem = attachmentItem
super.init(frame: .zero)
self.captionText = attachmentItem.captionText
addSubview(placeholderTextView)
placeholderTextView.autoPinEdgesToSuperviewMargins()
@ -1094,12 +1115,6 @@ class CaptionView: UIView {
}
extension CaptionView: UITextViewDelegate {
// @available(iOS 2.0, *)
// optional public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool
//
// @available(iOS 2.0, *)
// optional public func textViewShouldEndEditing(_ textView: UITextView) -> Bool
//
public func textViewDidBeginEditing(_ textView: UITextView) {
updatePlaceholderTextViewVisibility()
}
@ -1143,33 +1158,14 @@ extension CaptionView: UITextViewDelegate {
}
public func textViewDidChange(_ textView: UITextView) {
self.delegate?.captionViewDidChange(self)
self.delegate?.captionView(self, didChangeCaptionText: textView.text, attachmentItem: attachmentItem)
}
//
//
// @available(iOS 2.0, *)
// optional public func textViewDidChangeSelection(_ textView: UITextView)
//
//
// @available(iOS 10.0, *)
// optional public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
//
// @available(iOS 10.0, *)
// optional public func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
//
//
// @available(iOS, introduced: 7.0, deprecated: 10.0, message: "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead")
// optional public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool
//
// @available(iOS, introduced: 7.0, deprecated: 10.0, message: "Use textView:shouldInteractWithTextAttachment:inRange:forInteractionType: instead")
// optional public func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange) -> Bool
}
protocol MediaMessageTextToolbarDelegate: class {
func mediaMessageTextToolbarDidTapSend(_ mediaMessageTextToolbar: MediaMessageTextToolbar)
func mediaMessageTextToolbarDidBeginEditing(_ mediaMessageTextToolbar: MediaMessageTextToolbar)
func mediaMessageTextToolbarDidEndEditing(_ mediaMessageTextToolbar: MediaMessageTextToolbar)
func mediaMessageTextToolbar(_ mediaMessageTextToolbar: MediaMessageTextToolbar, textViewDidChange: UITextView)
func mediaMessageTextToolbarDidAddMore(_ mediaMessageTextToolbar: MediaMessageTextToolbar)
}
@ -1342,7 +1338,6 @@ class MediaMessageTextToolbar: UIView, UITextViewDelegate {
public func textViewDidChange(_ textView: UITextView) {
updateHeight(textView: textView)
self.mediaMessageTextToolbarDelegate?.mediaMessageTextToolbar(self, textViewDidChange: textView)
}
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {