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

View file

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

View file

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