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

129 lines
3.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",
@"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;
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];
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;
2026-01-16 15:55:08 +08:00
}
@end