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

182 lines
5.5 KiB
Mathematica
Raw Normal View History

2017-10-10 22:13:54 +02:00
//
2018-04-02 21:34:10 +02:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-10-10 22:13:54 +02:00
//
#import "OWSGenericAttachmentView.h"
#import "OWSBezierPathView.h"
2018-07-09 15:58:02 +02:00
#import "Signal-Swift.h"
2017-10-10 22:13:54 +02:00
#import "UIFont+OWS.h"
#import "UIView+OWS.h"
#import "ViewControllerUtils.h"
2017-12-19 03:50:51 +01:00
#import <SignalMessaging/NSString+OWS.h>
#import <SignalMessaging/OWSFormat.h>
2018-06-01 20:20:48 +02:00
#import <SignalMessaging/UIColor+OWS.h>
2017-10-10 22:13:54 +02:00
#import <SignalServiceKit/MimeTypeUtil.h>
#import <SignalServiceKit/TSAttachmentStream.h>
NS_ASSUME_NONNULL_BEGIN
@interface OWSGenericAttachmentView ()
@property (nonatomic) TSAttachmentStream *attachmentStream;
@property (nonatomic) BOOL isIncoming;
2018-06-29 15:54:22 +02:00
@property (nonatomic) UILabel *topLabel;
@property (nonatomic) UILabel *bottomLabel;
2017-10-10 22:13:54 +02:00
@end
#pragma mark -
@implementation OWSGenericAttachmentView
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream isIncoming:(BOOL)isIncoming
{
self = [super init];
if (self) {
_attachmentStream = attachmentStream;
_isIncoming = isIncoming;
}
return self;
}
2018-05-01 22:16:44 +02:00
#pragma mark -
2017-10-10 22:13:54 +02:00
2018-06-27 20:53:31 +02:00
- (CGFloat)hMargin
2017-10-10 22:13:54 +02:00
{
2018-06-27 20:53:31 +02:00
return 0.f;
2017-10-10 22:13:54 +02:00
}
2018-06-27 21:13:15 +02:00
- (CGFloat)hSpacing
2017-10-10 22:13:54 +02:00
{
2018-05-04 19:06:26 +02:00
return 8.f;
2017-10-10 22:13:54 +02:00
}
2018-06-29 15:54:22 +02:00
- (CGFloat)vMargin
2017-10-10 22:13:54 +02:00
{
2018-07-17 00:30:18 +02:00
return 5.f;
2017-10-10 22:13:54 +02:00
}
2018-06-29 15:54:22 +02:00
- (CGSize)measureSizeWithMaxMessageWidth:(CGFloat)maxMessageWidth
2017-10-10 22:13:54 +02:00
{
2018-06-29 15:54:22 +02:00
CGSize result = CGSizeZero;
2017-10-10 22:13:54 +02:00
2018-06-27 21:13:15 +02:00
CGFloat labelsHeight = ([OWSGenericAttachmentView topLabelFont].lineHeight +
[OWSGenericAttachmentView bottomLabelFont].lineHeight + [OWSGenericAttachmentView labelVSpacing]);
2018-06-29 15:54:22 +02:00
CGFloat contentHeight = MAX(self.iconHeight, labelsHeight);
result.height = contentHeight + self.vMargin * 2;
2017-10-10 22:13:54 +02:00
2018-06-29 15:54:22 +02:00
CGFloat labelsWidth
= MAX([self.topLabel sizeThatFits:CGSizeZero].width, [self.bottomLabel sizeThatFits:CGSizeZero].width);
CGFloat contentWidth = (self.iconWidth + labelsWidth + self.hSpacing);
result.width = MIN(maxMessageWidth, contentWidth + self.hMargin * 2);
return CGSizeCeil(result);
2017-10-10 22:13:54 +02:00
}
2018-06-29 15:54:22 +02:00
- (CGFloat)iconWidth
2017-10-10 22:13:54 +02:00
{
2018-06-29 15:54:22 +02:00
return 36.f;
2017-10-10 22:13:54 +02:00
}
2018-06-27 20:53:31 +02:00
- (CGFloat)iconHeight
2017-10-10 22:13:54 +02:00
{
2018-06-29 15:54:22 +02:00
return 48.f;
2017-10-10 22:13:54 +02:00
}
2018-07-09 15:58:02 +02:00
- (void)createContentsWithConversationStyle:(ConversationStyle *)conversationStyle
2017-10-10 22:13:54 +02:00
{
OWSAssertDebug(conversationStyle);
2018-07-09 15:58:02 +02:00
2018-06-27 20:53:31 +02:00
self.axis = UILayoutConstraintAxisHorizontal;
self.alignment = UIStackViewAlignmentCenter;
2018-06-27 21:13:15 +02:00
self.spacing = self.hSpacing;
2018-07-17 00:30:18 +02:00
self.layoutMarginsRelativeArrangement = YES;
self.layoutMargins = UIEdgeInsetsMake(self.vMargin, 0, self.vMargin, 0);
2017-10-10 22:13:54 +02:00
2018-06-27 20:53:31 +02:00
// attachment_file
UIImage *image = [UIImage imageNamed:@"generic-attachment"];
OWSAssertDebug(image);
OWSAssertDebug(image.size.width == self.iconWidth);
OWSAssertDebug(image.size.height == self.iconHeight);
2017-10-10 22:13:54 +02:00
UIImageView *imageView = [UIImageView new];
2018-06-27 20:53:31 +02:00
imageView.image = image;
[self addArrangedSubview:imageView];
[imageView setContentHuggingHigh];
2018-06-29 16:25:36 +02:00
NSString *filename = self.attachmentStream.sourceFilename;
if (!filename) {
filename = [[self.attachmentStream filePath] lastPathComponent];
}
NSString *fileExtension = filename.pathExtension;
if (fileExtension.length < 1) {
2018-06-29 18:56:08 +02:00
fileExtension = [MIMETypeUtil fileExtensionForMIMEType:self.attachmentStream.contentType];
2018-06-29 16:25:36 +02:00
}
UILabel *fileTypeLabel = [UILabel new];
fileTypeLabel.text = fileExtension.localizedUppercaseString;
2018-06-29 21:09:05 +02:00
fileTypeLabel.textColor = [UIColor ows_light90Color];
2018-06-29 16:25:36 +02:00
fileTypeLabel.lineBreakMode = NSLineBreakByTruncatingTail;
fileTypeLabel.font = [UIFont ows_dynamicTypeCaption1Font].ows_mediumWeight;
fileTypeLabel.adjustsFontSizeToFitWidth = YES;
fileTypeLabel.textAlignment = NSTextAlignmentCenter;
// Center on icon.
[imageView addSubview:fileTypeLabel];
[fileTypeLabel autoCenterInSuperview];
[fileTypeLabel autoSetDimension:ALDimensionWidth toSize:self.iconWidth - 20.f];
2018-06-27 20:53:31 +02:00
UIStackView *labelsView = [UIStackView new];
labelsView.axis = UILayoutConstraintAxisVertical;
2018-06-27 21:13:15 +02:00
labelsView.spacing = [OWSGenericAttachmentView labelVSpacing];
2018-06-27 20:53:31 +02:00
labelsView.alignment = UIStackViewAlignmentLeading;
[self addArrangedSubview:labelsView];
2017-10-10 22:13:54 +02:00
2017-10-18 20:53:31 +02:00
NSString *topText = [self.attachmentStream.sourceFilename ows_stripped];
2017-10-10 22:13:54 +02:00
if (topText.length < 1) {
topText = [MIMETypeUtil fileExtensionForMIMEType:self.attachmentStream.contentType].localizedUppercaseString;
2017-10-10 22:13:54 +02:00
}
if (topText.length < 1) {
topText = NSLocalizedString(@"GENERIC_ATTACHMENT_LABEL", @"A label for generic attachments.");
}
UILabel *topLabel = [UILabel new];
2018-06-29 15:54:22 +02:00
self.topLabel = topLabel;
2017-10-10 22:13:54 +02:00
topLabel.text = topText;
2018-07-09 15:58:02 +02:00
topLabel.textColor = [conversationStyle bubbleTextColorWithIsIncoming:self.isIncoming];
2017-10-10 22:13:54 +02:00
topLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
2018-06-27 22:00:44 +02:00
topLabel.font = [OWSGenericAttachmentView topLabelFont];
2018-06-27 20:53:31 +02:00
[labelsView addArrangedSubview:topLabel];
2017-10-10 22:13:54 +02:00
NSError *error;
unsigned long long fileSize =
[[NSFileManager defaultManager] attributesOfItemAtPath:[self.attachmentStream filePath] error:&error].fileSize;
OWSAssertDebug(!error);
NSString *bottomText = [OWSFormat formatFileSize:fileSize];
2017-10-10 22:13:54 +02:00
UILabel *bottomLabel = [UILabel new];
2018-06-29 15:54:22 +02:00
self.bottomLabel = bottomLabel;
2017-10-10 22:13:54 +02:00
bottomLabel.text = bottomText;
2018-07-09 15:58:02 +02:00
bottomLabel.textColor = [conversationStyle bubbleSecondaryTextColorWithIsIncoming:self.isIncoming];
2017-10-10 22:13:54 +02:00
bottomLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
2018-06-27 21:13:15 +02:00
bottomLabel.font = [OWSGenericAttachmentView bottomLabelFont];
2018-06-27 20:53:31 +02:00
[labelsView addArrangedSubview:bottomLabel];
2017-10-10 22:13:54 +02:00
}
2018-06-27 21:13:15 +02:00
+ (UIFont *)topLabelFont
{
2018-07-12 21:02:25 +02:00
return [UIFont ows_dynamicTypeBodyFont];
2018-06-27 21:13:15 +02:00
}
+ (UIFont *)bottomLabelFont
{
2018-07-12 21:02:25 +02:00
return [UIFont ows_dynamicTypeCaption1Font];
2018-06-27 21:13:15 +02:00
}
+ (CGFloat)labelVSpacing
{
return 2.f;
}
2017-10-10 22:13:54 +02:00
@end
NS_ASSUME_NONNULL_END