1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-12 09:29:55 +01:00
Telegram/Watch/Extension/TGNeoAudioMessageViewModel.m

125 lines
5.1 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGNeoAudioMessageViewModel.h"
#import "TGBridgeMessage.h"
@interface TGNeoAudioMessageViewModel ()
{
TGNeoLabelViewModel *_nameModel;
TGNeoLabelViewModel *_durationModel;
2016-02-25 01:03:51 +01:00
bool _isVoiceMessage;
int32_t _duration;
UIColor *_iconTint;
NSString *_spinnerName;
2015-10-01 18:19:52 +02:00
}
@end
@implementation TGNeoAudioMessageViewModel
2016-02-25 01:03:51 +01:00
- (instancetype)initWithMessage:(TGBridgeMessage *)message type:(TGNeoMessageType)type users:(NSDictionary *)users context:(TGBridgeContext *)context
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
self = [super initWithMessage:message type:type users:users context:context];
2015-10-01 18:19:52 +02:00
if (self != nil)
{
TGBridgeAudioMediaAttachment *audioAttachment = nil;
TGBridgeDocumentMediaAttachment *documentAttachment = nil;
for (TGBridgeMediaAttachment *attachment in message.media)
{
if ([attachment isKindOfClass:[TGBridgeAudioMediaAttachment class]])
{
audioAttachment = (TGBridgeAudioMediaAttachment *)attachment;
2016-02-25 01:03:51 +01:00
_isVoiceMessage = true;
_duration = audioAttachment.duration;
2015-10-01 18:19:52 +02:00
break;
}
else if ([attachment isKindOfClass:[TGBridgeDocumentMediaAttachment class]])
{
documentAttachment = (TGBridgeDocumentMediaAttachment *)attachment;
2016-02-25 01:03:51 +01:00
_isVoiceMessage = documentAttachment.isVoice;
_duration = documentAttachment.duration;
2015-10-01 18:19:52 +02:00
break;
}
}
2016-02-25 01:03:51 +01:00
NSString *title = TGLocalized(@"Message.Audio");
NSString *subtitle = @"";
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
if (!_isVoiceMessage)
2015-10-01 18:19:52 +02:00
{
[self removeSubmodel:self.forwardHeaderModel];
self.forwardHeaderModel = nil;
2016-02-25 01:03:51 +01:00
if (documentAttachment.title.length > 0)
title = documentAttachment.title;
else
title = documentAttachment.fileName;
2015-10-01 18:19:52 +02:00
subtitle = documentAttachment.performer.length > 0 ? documentAttachment.performer : @"";
}
else
{
2016-02-25 01:03:51 +01:00
NSInteger durationMinutes = floor(_duration / 60.0);
NSInteger durationSeconds = _duration % 60;
2015-10-01 18:19:52 +02:00
subtitle = [NSString stringWithFormat:@"%ld:%02ld", (long)durationMinutes, (long)durationSeconds];
}
2016-02-25 01:03:51 +01:00
_nameModel = [[TGNeoLabelViewModel alloc] initWithText:title font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] color:[self normalColorForMessage:message] attributes:nil];
_nameModel.multiline = false;
[self addSubmodel:_nameModel];
2015-10-01 18:19:52 +02:00
_durationModel = [[TGNeoLabelViewModel alloc] initWithText:subtitle font:[UIFont systemFontOfSize:12] color:[self subtitleColorForMessage:message] attributes:nil];
_durationModel.multiline = false;
[self addSubmodel:_durationModel];
2016-02-25 01:03:51 +01:00
_iconTint = [self accentColorForMessage:message];
if (message.outgoing)
_spinnerName = @"BubbleSpinner";
else
_spinnerName = @"BubbleSpinnerIncoming";
2015-10-01 18:19:52 +02:00
}
return self;
}
- (CGSize)layoutWithContainerSize:(CGSize)containerSize
{
CGSize contentContainerSize = [self contentContainerSizeWithContainerSize:containerSize];
CGSize headerSize = [self layoutHeaderModelsWithContainerSize:contentContainerSize];
CGFloat maxContentWidth = headerSize.width;
CGFloat textTopOffset = headerSize.height;
CGFloat leftOffset = 26 + TGNeoBubbleMessageMetaSpacing;
2016-02-25 01:03:51 +01:00
UIEdgeInsets inset = UIEdgeInsetsMake(textTopOffset + 1.5f, TGNeoBubbleMessageViewModelInsets.left, 0, 0);
NSDictionary *audioButtonDictionary = @{};
if (_isVoiceMessage)
{
audioButtonDictionary = @{ TGNeoMessageAudioIcon: @"MediaAudioPlay",
TGNeoMessageAudioIconTint: _iconTint,
TGNeoMessageAudioAnimatedIcon: _spinnerName,
TGNeoMessageAudioButtonHasBackground: @false };
inset.left -= 4;
leftOffset -= 5;
}
2015-10-01 18:19:52 +02:00
contentContainerSize = CGSizeMake(containerSize.width - TGNeoBubbleMessageViewModelInsets.left - TGNeoBubbleMessageViewModelInsets.right - leftOffset, FLT_MAX);
CGSize nameSize = [_nameModel contentSizeWithContainerSize:contentContainerSize];
CGSize durationSize = [_durationModel contentSizeWithContainerSize:contentContainerSize];
maxContentWidth = MAX(maxContentWidth, MAX(nameSize.width, durationSize.width) + leftOffset);
_nameModel.frame = CGRectMake(TGNeoBubbleMessageViewModelInsets.left + leftOffset, textTopOffset, nameSize.width, 14);
_durationModel.frame = CGRectMake(TGNeoBubbleMessageViewModelInsets.left + leftOffset, CGRectGetMaxY(_nameModel.frame), durationSize.width, 14);
[self addAdditionalLayout:@{ TGNeoContentInset: [NSValue valueWithUIEdgeInsets:inset], TGNeoMessageAudioButton: audioButtonDictionary } withKey:TGNeoMessageMetaGroup];
2016-02-25 01:03:51 +01:00
CGSize contentSize = CGSizeMake(inset.left + TGNeoBubbleMessageViewModelInsets.right + maxContentWidth, CGRectGetMaxY(_durationModel.frame) + TGNeoBubbleMessageViewModelInsets.bottom);
2015-10-01 18:19:52 +02:00
[super layoutWithContainerSize:contentSize];
return contentSize;
}
@end