This commit is contained in:
2026-01-16 20:31:42 +08:00
parent 663cb8493b
commit 93489b09d9
6 changed files with 302 additions and 116 deletions

View File

@@ -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