Files
keyboard/keyBoard/Class/AiTalk/M/KBAIReplyModel.m

107 lines
3.7 KiB
Mathematica
Raw Normal View History

2026-01-16 15:55:08 +08:00
//
// KBAIReplyModel.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAIReplyModel.h"
#import <MJExtension/MJExtension.h>
2026-01-28 12:04:31 +08:00
#import "KBAIReplyModel.h"
2026-01-16 15:55:08 +08:00
@implementation KBAIReplyModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"replyId" : @"id",
2026-01-28 12:04:31 +08:00
@"userId" : @"userId",
@"userName" : @"userName",
@"avatarUrl" : @"userAvatar",
@"createTime" : @"createdAt",
2026-01-16 15:55:08 +08:00
};
}
2026-01-28 12:04:31 +08:00
- (void)setLiked:(NSInteger)liked {
// NSInteger (0/1) BOOL
_isLiked = (liked == 1);
}
- (void)setCreatedAt:(NSString *)createdAt {
//
if (createdAt && createdAt.length > 0) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSDate *date = [formatter dateFromString:createdAt];
if (date) {
_createTime = [date timeIntervalSince1970];
}
}
}
2026-01-16 15:55:08 +08:00
- (NSString *)formattedTime {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.createTime];
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
if (interval < 60) {
return @"刚刚";
} else if (interval < 3600) {
return [NSString stringWithFormat:@"%.0f分钟前", interval / 60];
} else if (interval < 86400) {
return [NSString stringWithFormat:@"%.0f小时前", interval / 3600];
} else if (interval < 86400 * 30) {
return [NSString stringWithFormat:@"%.0f天前", interval / 86400];
} else {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM-dd";
return [formatter stringFromDate:date];
}
}
2026-01-16 19:09:54 +08:00
- (CGFloat)calculateCellHeightWithMaxWidth:(CGFloat)maxWidth {
if (self.cachedCellHeight > 0) {
return self.cachedCellHeight;
}
// Cell
2026-01-16 20:31:42 +08:00
// (68) + (28) + (8) + + (50)
// = maxWidth - 68 - 28 - 8 - 50
CGFloat contentWidth = maxWidth - 68 - 28 - 8 - 50;
2026-01-16 19:09:54 +08:00
2026-01-16 20:31:42 +08:00
// "回复 @xxx"
NSMutableString *userNameText = [NSMutableString stringWithString:self.userName];
2026-01-16 19:09:54 +08:00
if (self.replyToUserName.length > 0) {
2026-01-16 20:31:42 +08:00
[userNameText appendFormat:@" 回复 @%@", self.replyToUserName];
2026-01-16 19:09:54 +08:00
}
2026-01-16 20:31:42 +08:00
UIFont *userNameFont = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
CGRect userNameRect = [userNameText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: userNameFont}
context:nil];
CGFloat userNameHeight = ceil(userNameRect.size.height);
2026-01-16 19:09:54 +08:00
2026-01-16 20:31:42 +08:00
//
2026-01-16 19:09:54 +08:00
UIFont *contentFont = [UIFont systemFontOfSize:14];
2026-01-16 20:31:42 +08:00
CGRect contentRect = [self.content boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: contentFont}
context:nil];
2026-01-16 19:09:54 +08:00
CGFloat contentHeight = ceil(contentRect.size.height);
//
CGFloat timeHeight = 14; // 11
2026-01-16 20:31:42 +08:00
// = (8) + + (4) + + (6) + + (8)
CGFloat totalHeight = 8 + userNameHeight + 4 + contentHeight + 6 + timeHeight + 8;
2026-01-16 19:09:54 +08:00
// +
CGFloat minHeight = 8 + 28 + 8;
totalHeight = MAX(totalHeight, minHeight);
self.cachedCellHeight = totalHeight;
return totalHeight;
}
2026-01-16 15:55:08 +08:00
@end