1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-04 10:27:46 +01:00
Telegram/Watch/Extension/TGNeoChatViewModel.m

227 lines
7.3 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGNeoChatViewModel.h"
#import "TGNeoLabelViewModel.h"
#import "TGNeoImageViewModel.h"
#import "TGNeoAttachmentViewModel.h"
#import "TGBridgeContext.h"
#import "TGBridgeChat.h"
#import "TGBridgeUser.h"
2016-02-25 01:03:51 +01:00
#import "TGExtensionDelegate.h"
2015-10-01 18:19:52 +02:00
#import "TGStringUtils.h"
#import "TGDateUtils.h"
@interface TGNeoChatViewModel ()
{
TGNeoLabelViewModel *_nameModel;
TGNeoLabelViewModel *_authorNameModel;
TGNeoImageViewModel *_verifiedModel;
TGNeoLabelViewModel *_authorInitialsModel;
TGNeoLabelViewModel *_textModel;
TGNeoAttachmentViewModel *_attachmentModel;
TGNeoLabelViewModel *_timeModel;
}
@end
@implementation TGNeoChatViewModel
- (instancetype)initWithChat:(TGBridgeChat *)chat users:(NSDictionary *)users context:(TGBridgeContext *)context
{
self = [super init];
if (self != nil)
{
TGBridgeUser *author = nil;
NSString *name = nil;
2016-02-25 01:03:51 +01:00
if (chat.isGroup || chat.isChannelGroup)
2015-10-01 18:19:52 +02:00
{
author = users[@(chat.fromUid)];
name = chat.groupTitle;
}
else if (chat.isChannel)
{
name = chat.groupTitle;
}
else
{
author = users[@(chat.identifier)];
name = [author displayName];
}
2016-02-25 01:03:51 +01:00
_nameModel = [[TGNeoLabelViewModel alloc] initWithText:name font:[UIFont systemFontOfSize:[TGNeoChatViewModel titleFontSize] weight:UIFontWeightMedium] color:[UIColor whiteColor] attributes:nil];
2015-10-01 18:19:52 +02:00
_nameModel.multiline = false;
[self addSubmodel:_nameModel];
2016-02-25 01:03:51 +01:00
if (chat.isVerified || author.isVerified)
2015-10-01 18:19:52 +02:00
{
_verifiedModel = [[TGNeoImageViewModel alloc] initWithImage:[UIImage imageNamed:@"VerifiedList"]];
[self addSubmodel:_verifiedModel];
}
2016-02-25 01:03:51 +01:00
_attachmentModel = [[TGNeoAttachmentViewModel alloc] initWithAttachments:chat.media author:author forChannel:(chat.isChannel && !chat.isChannelGroup) users:users font:[UIFont systemFontOfSize:[TGNeoChatViewModel textFontSize]] subTitleColor:[UIColor hexColor:0x8f8f8f] normalColor:[UIColor whiteColor] compact:false];
2015-10-01 18:19:52 +02:00
if (_attachmentModel != nil)
[self addSubmodel:_attachmentModel];
2016-02-25 01:03:51 +01:00
if ((chat.isGroup || chat.isChannelGroup) && !_attachmentModel.inhibitsInitials)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
NSString *initials = (chat.fromUid == context.userId) ? TGLocalized(@"DialogList.You") : [TGStringUtils initialsForFirstName:author.firstName lastName:author.lastName single:false];
2015-10-01 18:19:52 +02:00
if (initials.length > 0)
{
2016-02-25 01:03:51 +01:00
_authorInitialsModel = [[TGNeoLabelViewModel alloc] initWithText:[NSString stringWithFormat:@"%@:", initials] font:[UIFont systemFontOfSize:[TGNeoChatViewModel textFontSize] weight:UIFontWeightMedium] color:[UIColor whiteColor] attributes:nil];
_authorInitialsModel.multiline = false;
2015-10-01 18:19:52 +02:00
[self addSubmodel:_authorInitialsModel];
}
}
if (chat.text.length > 0)
{
2016-02-25 01:03:51 +01:00
_textModel = [[TGNeoLabelViewModel alloc] initWithText:chat.text font:[UIFont systemFontOfSize:[TGNeoChatViewModel textFontSize]] color:[UIColor hexColor:0x8f8f8f] attributes:nil];
2015-10-01 18:19:52 +02:00
_textModel.multiline = false;
[self addSubmodel:_textModel];
}
NSString *time = @"";
if (chat.date > 0)
time = [TGDateUtils stringForMessageListDate:chat.date];
2016-02-25 01:03:51 +01:00
_timeModel = [[TGNeoLabelViewModel alloc] initWithText:time font:[UIFont systemFontOfSize:[TGNeoChatViewModel timeFontSize]] color:[UIColor hexColor:0x8f8f8f] attributes:nil];
_timeModel.multiline = false;
2015-10-01 18:19:52 +02:00
[self addSubmodel:_timeModel];
}
return self;
}
- (CGSize)layoutWithContainerSize:(CGSize)containerSize
{
CGSize nameSize = [_nameModel contentSizeWithContainerSize:CGSizeMake(containerSize.width - 31 - 7, FLT_MAX)];
if (_verifiedModel != nil)
{
CGFloat margin = 4;
_verifiedModel.frame = CGRectMake(MIN(31.5f + nameSize.width + margin, containerSize.width - 20), 6, 12, 12);
nameSize.width = MIN(nameSize.width, _verifiedModel.frame.origin.x - 31.5f - margin);
2016-02-25 01:03:51 +01:00
_nameModel.frame = CGRectMake(31.5f, 1.5f, nameSize.width, nameSize.height);
2015-10-01 18:19:52 +02:00
}
else
{
2016-02-25 01:03:51 +01:00
_nameModel.frame = CGRectMake(31.5f, 1.5f, containerSize.width - 31 - 7, nameSize.height);
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
CGFloat textX = 0;
CGFloat textY = CGRectGetMaxY(_nameModel.frame) - 2.5f;
2015-10-01 18:19:52 +02:00
if (_authorInitialsModel != nil)
{
CGFloat width = [_authorInitialsModel contentSizeWithContainerSize:CGSizeMake(40, 20)].width + 4;
2016-02-25 01:03:51 +01:00
_authorInitialsModel.frame = CGRectMake(31.5f, textY, width, 20);
textX += width;
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
TGNeoViewModel *contentViewModel = (_attachmentModel != nil) ? _attachmentModel : _textModel;
CGSize textSize = [contentViewModel contentSizeWithContainerSize:CGSizeMake(containerSize.width - 31 - 7, FLT_MAX)];
contentViewModel.frame = CGRectMake(31.5f + textX, textY, containerSize.width - 31 - 7 - textX, textSize.height);
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
CGSize timeSize = [_timeModel contentSizeWithContainerSize:CGSizeMake(containerSize.width - 31 - 7, FLT_MAX)];
_timeModel.frame = CGRectMake(31.5f, CGRectGetMaxY(contentViewModel.frame) - 1, containerSize.width - 31 - 36, timeSize.height);
2015-10-01 18:19:52 +02:00
2016-02-25 01:03:51 +01:00
self.contentSize = CGSizeMake(containerSize.width, CGRectGetMaxY(_timeModel.frame) + 3);
2015-10-01 18:19:52 +02:00
return self.contentSize;
}
2016-02-25 01:03:51 +01:00
+ (CGFloat)titleFontSize
{
TGContentSizeCategory category = [TGExtensionDelegate instance].contentSizeCategory;
switch (category)
{
case TGContentSizeCategoryXS:
return 14.0f;
case TGContentSizeCategoryS:
return 15.0f;
case TGContentSizeCategoryL:
return 16.0f;
case TGContentSizeCategoryXL:
return 17.0f;
case TGContentSizeCategoryXXL:
return 18.0f;
case TGContentSizeCategoryXXXL:
return 19.0f;
default:
break;
}
return 16.0f;
}
+ (CGFloat)textFontSize
{
TGContentSizeCategory category = [TGExtensionDelegate instance].contentSizeCategory;
switch (category)
{
case TGContentSizeCategoryXS:
return 14.0f;
case TGContentSizeCategoryS:
return 15.0f;
case TGContentSizeCategoryL:
return 16.0f;
case TGContentSizeCategoryXL:
return 17.0f;
case TGContentSizeCategoryXXL:
return 18.0f;
case TGContentSizeCategoryXXXL:
return 19.0f;
default:
break;
}
return 16.0f;
}
+ (CGFloat)timeFontSize
{
TGContentSizeCategory category = [TGExtensionDelegate instance].contentSizeCategory;
switch (category)
{
case TGContentSizeCategoryXS:
return 11.0f;
case TGContentSizeCategoryS:
return 12.0f;
case TGContentSizeCategoryL:
return 13.0f;
case TGContentSizeCategoryXL:
return 14.0f;
case TGContentSizeCategoryXXL:
return 15.0f;
case TGContentSizeCategoryXXXL:
return 16.0f;
default:
break;
}
return 13.0f;
}
2015-10-01 18:19:52 +02:00
@end