1
This commit is contained in:
@@ -35,6 +35,11 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
/// 输入框底部约束
|
||||
@property(nonatomic, strong) MASConstraint *inputBottomConstraint;
|
||||
|
||||
/// 当前回复的目标(一级评论)
|
||||
@property(nonatomic, weak) KBAICommentModel *replyToComment;
|
||||
/// 当前回复的目标(二级评论)
|
||||
@property(nonatomic, weak) KBAIReplyModel *replyToReply;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBAICommentView
|
||||
@@ -245,6 +250,10 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
[weakSelf.tableView reloadRowsAtIndexPaths:@[ indexPath ]
|
||||
withRowAnimation:UITableViewRowAnimationNone];
|
||||
};
|
||||
|
||||
cell.onReplyAction = ^{
|
||||
[weakSelf setReplyToComment:comment reply:reply];
|
||||
};
|
||||
|
||||
return cell;
|
||||
}
|
||||
@@ -267,6 +276,10 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
[weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section]
|
||||
withRowAnimation:UITableViewRowAnimationNone];
|
||||
};
|
||||
|
||||
header.onReplyAction = ^{
|
||||
[weakSelf setReplyToComment:comment reply:nil];
|
||||
};
|
||||
|
||||
return header;
|
||||
}
|
||||
@@ -481,12 +494,55 @@ static NSInteger const kRepliesLoadCount = 5;
|
||||
return _inputView;
|
||||
}
|
||||
|
||||
#pragma mark - Reply
|
||||
|
||||
- (void)setReplyToComment:(KBAICommentModel *)comment reply:(KBAIReplyModel *)reply {
|
||||
self.replyToComment = comment;
|
||||
self.replyToReply = reply;
|
||||
|
||||
if (reply) {
|
||||
// 回复二级评论
|
||||
self.inputView.placeholder = [NSString stringWithFormat:@"回复 @%@", reply.userName];
|
||||
} else if (comment) {
|
||||
// 回复一级评论
|
||||
self.inputView.placeholder = [NSString stringWithFormat:@"回复 @%@", comment.userName];
|
||||
} else {
|
||||
// 普通评论
|
||||
self.inputView.placeholder = @"说点什么...";
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clearReplyTarget {
|
||||
self.replyToComment = nil;
|
||||
self.replyToReply = nil;
|
||||
self.inputView.placeholder = @"说点什么...";
|
||||
}
|
||||
|
||||
#pragma mark - Send Comment
|
||||
|
||||
- (void)sendCommentWithText:(NSString *)text {
|
||||
if (text.length == 0) return;
|
||||
|
||||
// 创建新评论
|
||||
CGFloat tableWidth = self.tableView.bounds.size.width;
|
||||
if (tableWidth <= 0) {
|
||||
tableWidth = [UIScreen mainScreen].bounds.size.width;
|
||||
}
|
||||
|
||||
if (self.replyToComment) {
|
||||
// 回复评论(添加二级评论)
|
||||
[self sendReplyWithText:text tableWidth:tableWidth];
|
||||
} else {
|
||||
// 发送一级评论
|
||||
[self sendNewCommentWithText:text tableWidth:tableWidth];
|
||||
}
|
||||
|
||||
// 清空输入框和回复目标
|
||||
[self.inputView clearText];
|
||||
[self clearReplyTarget];
|
||||
}
|
||||
|
||||
- (void)sendNewCommentWithText:(NSString *)text tableWidth:(CGFloat)tableWidth {
|
||||
// 创建新一级评论
|
||||
KBAICommentModel *newComment = [[KBAICommentModel alloc] init];
|
||||
newComment.commentId = [NSUUID UUID].UUIDString;
|
||||
newComment.userId = @"current_user";
|
||||
@@ -499,10 +555,6 @@ static NSInteger const kRepliesLoadCount = 5;
|
||||
newComment.replies = @[];
|
||||
|
||||
// 计算高度缓存
|
||||
CGFloat tableWidth = self.tableView.bounds.size.width;
|
||||
if (tableWidth <= 0) {
|
||||
tableWidth = [UIScreen mainScreen].bounds.size.width;
|
||||
}
|
||||
newComment.cachedHeaderHeight = [newComment calculateHeaderHeightWithMaxWidth:tableWidth];
|
||||
|
||||
// 插入到数组第一个
|
||||
@@ -516,9 +568,69 @@ static NSInteger const kRepliesLoadCount = 5;
|
||||
|
||||
// 滚动到顶部
|
||||
[self.tableView setContentOffset:CGPointZero animated:YES];
|
||||
}
|
||||
|
||||
- (void)sendReplyWithText:(NSString *)text tableWidth:(CGFloat)tableWidth {
|
||||
KBAICommentModel *comment = self.replyToComment;
|
||||
if (!comment) return;
|
||||
|
||||
// 清空输入框
|
||||
[self.inputView clearText];
|
||||
// 创建新二级评论
|
||||
KBAIReplyModel *newReply = [[KBAIReplyModel alloc] init];
|
||||
newReply.replyId = [NSUUID UUID].UUIDString;
|
||||
newReply.userId = @"current_user";
|
||||
newReply.userName = @"我";
|
||||
newReply.avatarUrl = @"";
|
||||
newReply.content = text;
|
||||
newReply.likeCount = 0;
|
||||
newReply.isLiked = NO;
|
||||
newReply.createTime = [[NSDate date] timeIntervalSince1970];
|
||||
|
||||
// 如果是回复二级评论,设置被回复用户
|
||||
if (self.replyToReply) {
|
||||
newReply.replyToUserName = self.replyToReply.userName;
|
||||
}
|
||||
|
||||
// 计算高度缓存
|
||||
newReply.cachedCellHeight = [newReply calculateCellHeightWithMaxWidth:tableWidth];
|
||||
|
||||
// 添加到 replies 数组
|
||||
NSMutableArray *newReplies = [NSMutableArray arrayWithArray:comment.replies];
|
||||
[newReplies addObject:newReply];
|
||||
comment.replies = newReplies;
|
||||
comment.totalReplyCount = newReplies.count;
|
||||
|
||||
// 找到该评论的 section
|
||||
NSInteger section = [self.comments indexOfObject:comment];
|
||||
if (section == NSNotFound) return;
|
||||
|
||||
// 如果已展开,添加到 displayedReplies 并插入行
|
||||
if (comment.isRepliesExpanded) {
|
||||
NSInteger newRowIndex = comment.displayedReplies.count;
|
||||
[comment.displayedReplies addObject:newReply];
|
||||
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:section];
|
||||
[self.tableView insertRowsAtIndexPaths:@[indexPath]
|
||||
withRowAnimation:UITableViewRowAnimationAutomatic];
|
||||
|
||||
// 刷新 Footer
|
||||
KBAICommentFooterView *footerView =
|
||||
(KBAICommentFooterView *)[self.tableView footerViewForSection:section];
|
||||
if (footerView) {
|
||||
[footerView configureWithComment:comment];
|
||||
}
|
||||
|
||||
// 滚动到新回复
|
||||
[self.tableView scrollToRowAtIndexPath:indexPath
|
||||
atScrollPosition:UITableViewScrollPositionBottom
|
||||
animated:YES];
|
||||
} else {
|
||||
// 未展开,刷新 Footer 显示新的回复数
|
||||
KBAICommentFooterView *footerView =
|
||||
(KBAICommentFooterView *)[self.tableView footerViewForSection:section];
|
||||
if (footerView) {
|
||||
[footerView configureWithComment:comment];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user