Ensure constant bubble sizes for audio media.

This commit is contained in:
Matthew Chen 2019-03-15 11:39:21 -04:00
parent fed4899c8d
commit d1447d0730
4 changed files with 50 additions and 55 deletions

View File

@ -1,17 +1,17 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@class ConversationStyle;
@class TSAttachmentStream;
@class TSAttachment;
@protocol ConversationViewItem;
@interface OWSAudioMessageView : UIStackView
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream
- (instancetype)initWithAttachment:(TSAttachment *)attachment
isIncoming:(BOOL)isIncoming
viewItem:(id<ConversationViewItem>)viewItem
conversationStyle:(ConversationStyle *)conversationStyle;

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
#import "OWSAudioMessageView.h"
@ -15,7 +15,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface OWSAudioMessageView ()
@property (nonatomic) TSAttachmentStream *attachmentStream;
@property (nonatomic) TSAttachment *attachment;
@property (nonatomic, nullable) TSAttachmentStream *attachmentStream;
@property (nonatomic) BOOL isIncoming;
@property (nonatomic, weak) id<ConversationViewItem> viewItem;
@property (nonatomic, readonly) ConversationStyle *conversationStyle;
@ -30,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSAudioMessageView
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream
- (instancetype)initWithAttachment:(TSAttachment *)attachment
isIncoming:(BOOL)isIncoming
viewItem:(id<ConversationViewItem>)viewItem
conversationStyle:(ConversationStyle *)conversationStyle
@ -38,7 +39,10 @@ NS_ASSUME_NONNULL_BEGIN
self = [super init];
if (self) {
_attachmentStream = attachmentStream;
_attachment = attachment;
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
_attachmentStream = (TSAttachmentStream *)attachment;
}
_isIncoming = isIncoming;
_viewItem = viewItem;
_conversationStyle = conversationStyle;
@ -66,8 +70,6 @@ NS_ASSUME_NONNULL_BEGIN
- (CGFloat)audioDurationSeconds
{
OWSAssertDebug(self.viewItem.audioDurationSeconds > 0.f);
return self.viewItem.audioDurationSeconds;
}
@ -174,7 +176,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isVoiceMessage
{
return self.attachmentStream.isVoiceMessage;
return self.attachment.isVoiceMessage;
}
- (void)createContents
@ -190,13 +192,13 @@ NS_ASSUME_NONNULL_BEGIN
[self addArrangedSubview:self.audioPlayPauseButton];
[self.audioPlayPauseButton setContentHuggingHigh];
NSString *filename = self.attachmentStream.sourceFilename;
if (!filename) {
NSString *_Nullable filename = self.attachment.sourceFilename;
if (filename.length < 1) {
filename = [self.attachmentStream.originalFilePath lastPathComponent];
}
NSString *topText = [[filename stringByDeletingPathExtension] ows_stripped];
if (topText.length < 1) {
topText = [MIMETypeUtil fileExtensionForMIMEType:self.attachmentStream.contentType].localizedUppercaseString;
topText = [MIMETypeUtil fileExtensionForMIMEType:self.attachment.contentType].localizedUppercaseString;
}
if (topText.length < 1) {
topText = NSLocalizedString(@"GENERIC_ATTACHMENT_LABEL", @"A label for generic attachments.");

View File

@ -168,22 +168,6 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
return self.viewItem.displayableBodyText;
}
- (nullable TSAttachmentStream *)attachmentStream
{
// This should always be valid for the appropriate cell types.
OWSAssertDebug(self.viewItem.attachmentStream);
return self.viewItem.attachmentStream;
}
- (nullable TSAttachmentPointer *)attachmentPointer
{
// This should always be valid for the appropriate cell types.
OWSAssertDebug(self.viewItem.attachmentPointer);
return self.viewItem.attachmentPointer;
}
- (TSMessage *)message
{
OWSAssertDebug([self.viewItem.interaction isKindOfClass:[TSMessage class]]);
@ -276,7 +260,6 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
case OWSMessageCellType_TextOnlyMessage:
break;
case OWSMessageCellType_Audio:
OWSAssertDebug(self.viewItem.attachmentStream);
bodyMediaView = [self loadViewForAudio];
break;
case OWSMessageCellType_GenericAttachment:
@ -837,10 +820,11 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
- (UIView *)loadViewForAudio
{
OWSAssertDebug(self.attachmentStream);
OWSAssertDebug([self.attachmentStream isAudio]);
TSAttachment *attachment = (self.viewItem.attachmentStream ?: self.viewItem.attachmentPointer);
OWSAssertDebug(attachment);
OWSAssertDebug([attachment isAudio]);
OWSAudioMessageView *audioMessageView = [[OWSAudioMessageView alloc] initWithAttachment:self.attachmentStream
OWSAudioMessageView *audioMessageView = [[OWSAudioMessageView alloc] initWithAttachment:attachment
isIncoming:self.isIncoming
viewItem:self.viewItem
conversationStyle:self.conversationStyle];
@ -861,8 +845,10 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
- (UIView *)loadViewForGenericAttachment
{
OWSAssertDebug(self.viewItem.attachmentStream);
// TODO:
OWSGenericAttachmentView *attachmentView =
[[OWSGenericAttachmentView alloc] initWithAttachment:self.attachmentStream isIncoming:self.isIncoming];
[[OWSGenericAttachmentView alloc] initWithAttachment:self.viewItem.attachmentStream isIncoming:self.isIncoming];
[attachmentView createContentsWithConversationStyle:self.conversationStyle];
[self addAttachmentUploadViewIfNecessary];
@ -878,12 +864,12 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
- (UIView *)loadViewForDownloadingAttachment
{
OWSAssertDebug(self.attachmentPointer);
OWSAssertDebug(self.viewItem.attachmentPointer);
// TODO: We probably want to do something different for attachments
// being restored from backup.
AttachmentPointerView *downloadView =
[[AttachmentPointerView alloc] initWithAttachmentPointer:self.attachmentPointer
[[AttachmentPointerView alloc] initWithAttachmentPointer:self.viewItem.attachmentPointer
isIncoming:self.isIncoming
conversationStyle:self.conversationStyle];
@ -929,7 +915,9 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
- (void)addAttachmentUploadViewIfNecessaryWithAttachmentStateCallback:
(nullable AttachmentStateBlock)attachmentStateCallback
{
OWSAssertDebug(self.attachmentStream);
if (!self.viewItem.attachmentStream) {
return;
}
if (!attachmentStateCallback) {
attachmentStateCallback = ^(BOOL isAttachmentReady) {
@ -937,9 +925,9 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
}
if (self.isOutgoing) {
if (!self.attachmentStream.isUploaded) {
if (!self.viewItem.attachmentStream.isUploaded) {
AttachmentUploadView *attachmentUploadView =
[[AttachmentUploadView alloc] initWithAttachment:self.attachmentStream
[[AttachmentUploadView alloc] initWithAttachment:self.viewItem.attachmentStream
attachmentStateCallback:attachmentStateCallback];
[self.bubbleView addSubview:attachmentUploadView];
[attachmentUploadView ows_autoPinToSuperviewEdges];
@ -1007,7 +995,8 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
case OWSMessageCellType_GenericAttachment: {
OWSAssertDebug(self.viewItem.attachmentStream);
OWSGenericAttachmentView *attachmentView =
[[OWSGenericAttachmentView alloc] initWithAttachment:self.attachmentStream isIncoming:self.isIncoming];
[[OWSGenericAttachmentView alloc] initWithAttachment:self.viewItem.attachmentStream
isIncoming:self.isIncoming];
[attachmentView createContentsWithConversationStyle:self.conversationStyle];
result = [attachmentView measureSizeWithMaxMessageWidth:maxMessageWidth];
break;
@ -1391,29 +1380,27 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
{
OWSAssertDebug(self.delegate);
if (self.viewItem.attachmentPointer && self.viewItem.attachmentPointer.state == TSAttachmentPointerStateFailed) {
[self.delegate didTapFailedIncomingAttachment:self.viewItem];
return;
}
switch (self.cellType) {
case OWSMessageCellType_Unknown:
case OWSMessageCellType_TextOnlyMessage:
break;
case OWSMessageCellType_Audio:
OWSAssertDebug(self.viewItem.attachmentStream);
[self.delegate didTapAudioViewItem:self.viewItem attachmentStream:self.viewItem.attachmentStream];
if (self.viewItem.attachmentStream) {
[self.delegate didTapAudioViewItem:self.viewItem attachmentStream:self.viewItem.attachmentStream];
}
return;
case OWSMessageCellType_GenericAttachment:
OWSAssertDebug(self.viewItem.attachmentStream);
[AttachmentSharing showShareUIForAttachment:self.viewItem.attachmentStream];
break;
case OWSMessageCellType_DownloadingAttachment: {
TSAttachmentPointer *_Nullable attachmentPointer = self.viewItem.attachmentPointer;
OWSAssertDebug(attachmentPointer);
if (attachmentPointer.state == TSAttachmentPointerStateFailed) {
[self.delegate didTapFailedIncomingAttachment:self.viewItem];
if (self.viewItem.attachmentStream) {
[AttachmentSharing showShareUIForAttachment:self.viewItem.attachmentStream];
}
break;
}
case OWSMessageCellType_DownloadingAttachment:
break;
case OWSMessageCellType_ContactShare:
[self.delegate didTapContactShareViewItem:self.viewItem];
break;

View File

@ -632,13 +632,19 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
self.audioDurationSeconds = audioDurationSeconds;
self.messageCellType = OWSMessageCellType_Audio;
} else {
OWSLogVerbose(@"contentType: %@", self.attachmentStream.contentType);
self.messageCellType = OWSMessageCellType_GenericAttachment;
}
} else if (self.messageCellType == OWSMessageCellType_Unknown) {
self.messageCellType = OWSMessageCellType_GenericAttachment;
}
} else if ([mediaAttachment isKindOfClass:[TSAttachmentPointer class]]) {
self.messageCellType = OWSMessageCellType_DownloadingAttachment;
if ([mediaAttachment isAudio]) {
self.audioDurationSeconds = 0;
self.messageCellType = OWSMessageCellType_Audio;
} else {
self.messageCellType = OWSMessageCellType_DownloadingAttachment;
}
self.attachmentPointer = (TSAttachmentPointer *)mediaAttachment;
} else {
OWSFailDebug(@"Unknown attachment type");