Files
keyboard/keyBoard/Class/AiTalk/M/KBAIReplyModel.m
2026-01-29 14:42:49 +08:00

128 lines
4.2 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBAIReplyModel.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAIReplyModel.h"
#import <MJExtension/MJExtension.h>
@implementation KBAIReplyModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"replyId" : @"id",
@"userId" : @"userId",
@"userName" : @"userName",
@"avatarUrl" : @"userAvatar",
@"createTime" : @"createdAt",
};
}
- (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];
}
}
}
#pragma mark - MJExtension Hook
/// MJExtension 转换完成后的钩子方法,用于处理空值
- (void)mj_didConvertToObjectWithKeyValues:(NSDictionary *)keyValues {
// 防止后端返回 null统一设置空字符串为默认值
if (!_replyId) {
_replyId = @"";
}
if (!_userId) {
_userId = @"";
}
if (!_userName) {
_userName = @"";
}
if (!_avatarUrl) {
_avatarUrl = @"";
}
if (!_content) {
_content = @"";
}
if (!_replyToUserName) {
_replyToUserName = @"";
}
}
- (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];
}
}
- (CGFloat)calculateCellHeightWithMaxWidth:(CGFloat)maxWidth {
if (self.cachedCellHeight > 0) {
return self.cachedCellHeight;
}
// Cell 布局:
// 左边距(68) + 头像(28) + 间距(8) + 内容区域 + 右边距(50)
// 内容区域宽度 = maxWidth - 68 - 28 - 8 - 50
CGFloat contentWidth = maxWidth - 68 - 28 - 8 - 50;
// 用户名高度(可能包含 "回复 @xxx"
NSMutableString *userNameText = [NSMutableString stringWithString:self.userName ?: @""];
if (self.replyToUserName.length > 0) {
[userNameText appendFormat:@" 回复 @%@", self.replyToUserName];
}
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);
// 内容高度
UIFont *contentFont = [UIFont systemFontOfSize:14];
NSString *contentText = self.content ?: @"";
CGRect contentRect = [contentText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: contentFont}
context:nil];
CGFloat contentHeight = ceil(contentRect.size.height);
// 时间高度(单行)
CGFloat timeHeight = 14; // 11号字体
// 总高度 = 上边距(8) + 用户名 + 间距(4) + 内容 + 间距(6) + 时间 + 下边距(8)
CGFloat totalHeight = 8 + userNameHeight + 4 + contentHeight + 6 + timeHeight + 8;
// 最小高度(头像高度 + 上下边距)
CGFloat minHeight = 8 + 28 + 8;
totalHeight = MAX(totalHeight, minHeight);
self.cachedCellHeight = totalHeight;
return totalHeight;
}
@end