129 lines
3.7 KiB
Objective-C
129 lines
3.7 KiB
Objective-C
//
|
||
// 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",
|
||
@"userName" : @[ @"userName", @"nickname", @"name" ],
|
||
@"avatarUrl" : @[ @"avatarUrl", @"avatar" ],
|
||
};
|
||
}
|
||
|
||
+ (NSDictionary *)mj_objectClassInArray {
|
||
return @{@"replies" : [KBAIReplyModel class]};
|
||
}
|
||
|
||
- (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;
|
||
}
|
||
|
||
// 已展开但还有更多
|
||
if (self.displayedReplies.count < self.totalReplyCount) {
|
||
return KBAIReplyFooterStateLoadMore;
|
||
}
|
||
|
||
// 全部展开
|
||
return KBAIReplyFooterStateCollapse;
|
||
}
|
||
|
||
- (void)loadMoreReplies:(NSInteger)count {
|
||
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]];
|
||
}
|
||
}
|
||
|
||
- (void)collapseReplies {
|
||
self.isRepliesExpanded = NO;
|
||
[self.displayedReplies removeAllObjects];
|
||
}
|
||
|
||
- (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];
|
||
CGRect contentRect = [self.content boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
|
||
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;
|
||
}
|
||
|
||
@end
|