-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathTGNeoFileMessageViewModel.m
74 lines (56 loc) · 3.31 KB
/
TGNeoFileMessageViewModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#import "TGNeoFileMessageViewModel.h"
#import <WatchCommonWatch/WatchCommonWatch.h>
#import "TGNeoImageViewModel.h"
#import "TGStringUtils.h"
@interface TGNeoFileMessageViewModel ()
{
TGNeoImageViewModel *_iconModel;
TGNeoLabelViewModel *_nameModel;
TGNeoLabelViewModel *_sizeModel;
}
@end
@implementation TGNeoFileMessageViewModel
- (instancetype)initWithMessage:(TGBridgeMessage *)message type:(TGNeoMessageType)type users:(NSDictionary *)users context:(TGBridgeContext *)context
{
self = [super initWithMessage:message type:type users:users context:context];
if (self != nil)
{
TGBridgeDocumentMediaAttachment *documentAttachment = nil;
for (TGBridgeMediaAttachment *attachment in message.media)
{
if ([attachment isKindOfClass:[TGBridgeDocumentMediaAttachment class]])
{
documentAttachment = (TGBridgeDocumentMediaAttachment *)attachment;
break;
}
}
_iconModel = [[TGNeoImageViewModel alloc] initWithImage:[UIImage imageNamed:@"File"] tintColor:[self accentColorForMessage:message type:type]];
[self addSubmodel:_iconModel];
_nameModel = [[TGNeoLabelViewModel alloc] initWithText:documentAttachment.fileName font:[UIFont systemFontOfSize:12 weight:UIFontWeightMedium] color:[self normalColorForMessage:message type:type] attributes:nil];
_nameModel.multiline = false;
[self addSubmodel:_nameModel];
_sizeModel = [[TGNeoLabelViewModel alloc] initWithText:[TGStringUtils stringForFileSize:documentAttachment.fileSize precision:2] font:[UIFont systemFontOfSize:12] color:[self subtitleColorForMessage:message type:type] attributes:nil];
_sizeModel.multiline = false;
[self addSubmodel:_sizeModel];
}
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 = 20 + TGNeoBubbleMessageMetaSpacing;
contentContainerSize = CGSizeMake(containerSize.width - TGNeoBubbleMessageViewModelInsets.left - TGNeoBubbleMessageViewModelInsets.right - leftOffset, FLT_MAX);
CGSize nameSize = [_nameModel contentSizeWithContainerSize:contentContainerSize];
CGSize metaSize = [_sizeModel contentSizeWithContainerSize:contentContainerSize];
maxContentWidth = MAX(maxContentWidth, MAX(nameSize.width, metaSize.width) + leftOffset);
_iconModel.frame = CGRectMake(TGNeoBubbleMessageViewModelInsets.left - 3, textTopOffset + 1.5f, 26, 26);
_nameModel.frame = CGRectMake(TGNeoBubbleMessageViewModelInsets.left + leftOffset, textTopOffset, nameSize.width, 14);
_sizeModel.frame = CGRectMake(TGNeoBubbleMessageViewModelInsets.left + leftOffset, CGRectGetMaxY(_nameModel.frame), metaSize.width, 14);
CGSize contentSize = CGSizeMake(TGNeoBubbleMessageViewModelInsets.left + TGNeoBubbleMessageViewModelInsets.right + maxContentWidth, CGRectGetMaxY(_sizeModel.frame) + TGNeoBubbleMessageViewModelInsets.bottom);
[super layoutWithContainerSize:contentSize];
return contentSize;
}
@end