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

168 lines
4.7 KiB
Mathematica
Raw Normal View History

2026-01-16 15:55:08 +08:00
//
// KBAICommentModel.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAICommentModel.h"
#import "KBAIReplyModel.h"
#import <MJExtension/MJExtension.h>
@implementation KBAICommentModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"commentId" : @"id",
2026-01-28 12:04:31 +08:00
@"userId" : @"userId",
@"userName" : @"userName",
@"avatarUrl" : @"userAvatar",
@"createTime" : @"createdAt",
@"totalReplyCount" : @"replyCount",
2026-01-16 15:55:08 +08:00
};
}
+ (NSDictionary *)mj_objectClassInArray {
return @{@"replies" : [KBAIReplyModel class]};
}
2026-01-28 12:04:31 +08:00
- (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-28 13:43:36 +08:00
#pragma mark - MJExtension Hook
/// MJExtension
- (void)mj_didConvertToObjectWithKeyValues:(NSDictionary *)keyValues {
// null
if (!_commentId) {
_commentId = @"";
}
if (!_userId) {
_userId = @"";
}
if (!_userName) {
_userName = @"";
}
if (!_avatarUrl) {
_avatarUrl = @"";
}
if (!_content) {
_content = @"";
}
}
2026-01-16 15:55:08 +08:00
- (instancetype)init {
self = [super init];
if (self) {
_displayedReplies = [NSMutableArray array];
_isRepliesExpanded = NO;
_hasMoreReplies = NO;
}
return self;
}
- (void)setReplies:(NSArray<KBAIReplyModel *> *)replies {
_replies = replies;
_totalReplyCount = replies.count;
_hasMoreReplies = replies.count > 0;
}
- (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];
}
}
- (KBAIReplyFooterState)footerState {
//
if (self.totalReplyCount == 0) {
return KBAIReplyFooterStateHidden;
}
//
if (!self.isRepliesExpanded) {
return KBAIReplyFooterStateExpand;
2026-01-16 15:55:08 +08:00
}
//
if (self.displayedReplies.count < self.totalReplyCount) {
return KBAIReplyFooterStateLoadMore;
}
//
return KBAIReplyFooterStateCollapse;
2026-01-16 15:55:08 +08:00
}
- (void)loadMoreReplies:(NSInteger)count {
2026-01-16 15:55:08 +08:00
self.isRepliesExpanded = YES;
NSInteger currentCount = self.displayedReplies.count;
NSInteger endIndex = MIN(currentCount + count, self.replies.count);
for (NSInteger i = currentCount; i < endIndex; i++) {
[self.displayedReplies addObject:self.replies[i]];
}
2026-01-16 15:55:08 +08:00
}
- (void)collapseReplies {
self.isRepliesExpanded = NO;
[self.displayedReplies removeAllObjects];
2026-01-16 19:09:54 +08:00
}
- (CGFloat)calculateHeaderHeightWithMaxWidth:(CGFloat)maxWidth {
if (self.cachedHeaderHeight > 0) {
return self.cachedHeaderHeight;
}
// Header
// (40) + (12) + + (50) + (16)
// = maxWidth - 16() - 40() - 12() - 50() - 16()
CGFloat contentWidth = maxWidth - 16 - 40 - 12 - 50 - 16;
//
CGFloat userNameHeight = 17; // 14
//
CGFloat timeHeight = 15; // 12
//
UIFont *contentFont = [UIFont systemFontOfSize:15];
2026-01-28 13:43:36 +08:00
NSString *contentText = self.content ?: @"";
CGRect contentRect = [contentText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
2026-01-16 19:09:54 +08:00
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: contentFont}
context:nil];
CGFloat contentHeight = ceil(contentRect.size.height);
// = (12) + + (2) + + (8) + + (12)
CGFloat totalHeight = 12 + userNameHeight + 2 + timeHeight + 8 + contentHeight + 12;
self.cachedHeaderHeight = totalHeight;
return totalHeight;
2026-01-16 15:55:08 +08:00
}
@end