1
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#import "AiVM.h"
|
||||
#import <MJExtension/MJExtension.h>
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <MJRefresh/MJRefresh.h>
|
||||
|
||||
static NSString *const kCommentHeaderIdentifier = @"CommentHeader";
|
||||
static NSString *const kReplyCellIdentifier = @"ReplyCell";
|
||||
@@ -33,6 +34,12 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
@property(nonatomic, strong) NSMutableArray<KBAICommentModel *> *comments;
|
||||
@property(nonatomic, assign) NSInteger totalCommentCount;
|
||||
|
||||
/// 分页参数
|
||||
@property(nonatomic, assign) NSInteger currentPage;
|
||||
@property(nonatomic, assign) NSInteger pageSize;
|
||||
@property(nonatomic, assign) BOOL isLoading;
|
||||
@property(nonatomic, assign) BOOL hasMoreData;
|
||||
|
||||
/// 键盘高度
|
||||
@property(nonatomic, assign) CGFloat keyboardHeight;
|
||||
/// 输入框底部约束
|
||||
@@ -56,6 +63,9 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
self = [super initWithFrame:frame];
|
||||
if (self) {
|
||||
self.comments = [NSMutableArray array];
|
||||
self.currentPage = 1;
|
||||
self.pageSize = 20;
|
||||
self.hasMoreData = YES;
|
||||
[self setupUI];
|
||||
[self setupKeyboardObservers];
|
||||
}
|
||||
@@ -114,6 +124,20 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
make.left.right.equalTo(self);
|
||||
make.bottom.equalTo(self.inputView.mas_top);
|
||||
}];
|
||||
|
||||
// 上拉加载更多
|
||||
__weak typeof(self) weakSelf = self;
|
||||
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
return;
|
||||
}
|
||||
[strongSelf loadMoreComments];
|
||||
}];
|
||||
footer.stateLabel.hidden = YES;
|
||||
footer.backgroundColor = [UIColor clearColor];
|
||||
footer.automaticallyHidden = YES;
|
||||
self.tableView.mj_footer = footer;
|
||||
}
|
||||
|
||||
#pragma mark - Keyboard Observers
|
||||
@@ -174,49 +198,87 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
#pragma mark - Data Loading
|
||||
|
||||
- (void)loadComments {
|
||||
if (self.isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.currentPage = 1;
|
||||
self.hasMoreData = YES;
|
||||
[self.tableView.mj_footer resetNoMoreData];
|
||||
|
||||
[self fetchCommentsAtPage:self.currentPage append:NO];
|
||||
}
|
||||
|
||||
- (void)loadMoreComments {
|
||||
if (self.isLoading) {
|
||||
[self.tableView.mj_footer endRefreshing];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self.hasMoreData) {
|
||||
[self.tableView.mj_footer endRefreshingWithNoMoreData];
|
||||
return;
|
||||
}
|
||||
|
||||
NSInteger nextPage = self.currentPage + 1;
|
||||
[self fetchCommentsAtPage:nextPage append:YES];
|
||||
}
|
||||
|
||||
- (void)fetchCommentsAtPage:(NSInteger)page append:(BOOL)append {
|
||||
if (self.companionId <= 0) {
|
||||
NSLog(@"[KBAICommentView] companionId 未设置,无法加载评论");
|
||||
[self showEmptyState];
|
||||
[self.tableView.mj_footer endRefreshing];
|
||||
return;
|
||||
}
|
||||
|
||||
self.isLoading = YES;
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[self.aiVM fetchCommentsWithCompanionId:self.companionId
|
||||
pageNum:1
|
||||
pageSize:20
|
||||
pageNum:page
|
||||
pageSize:self.pageSize
|
||||
completion:^(KBCommentPageModel *pageModel, NSError *error) {
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
return;
|
||||
}
|
||||
|
||||
strongSelf.isLoading = NO;
|
||||
|
||||
if (error) {
|
||||
NSLog(@"[KBAICommentView] 加载评论失败:%@", error.localizedDescription);
|
||||
// 加载失败也显示空态
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[strongSelf showEmptyStateWithError];
|
||||
if (append) {
|
||||
[strongSelf.tableView.mj_footer endRefreshing];
|
||||
} else {
|
||||
[strongSelf showEmptyStateWithError];
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[strongSelf updateCommentsWithPageModel:pageModel];
|
||||
[strongSelf updateCommentsWithPageModel:pageModel append:append];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
/// 更新评论数据(从后端返回的 KBCommentPageModel 转换为 UI 层的 KBAICommentModel)
|
||||
- (void)updateCommentsWithPageModel:(KBCommentPageModel *)pageModel {
|
||||
- (void)updateCommentsWithPageModel:(KBCommentPageModel *)pageModel append:(BOOL)append {
|
||||
if (!pageModel) {
|
||||
NSLog(@"[KBAICommentView] pageModel 为空");
|
||||
// 数据为空,显示空态
|
||||
[self showEmptyState];
|
||||
[self.tableView.mj_footer endRefreshing];
|
||||
return;
|
||||
}
|
||||
|
||||
self.totalCommentCount = pageModel.total;
|
||||
|
||||
[self.comments removeAllObjects];
|
||||
|
||||
if (!append) {
|
||||
[self.comments removeAllObjects];
|
||||
}
|
||||
|
||||
// 获取 tableView 宽度用于计算高度
|
||||
CGFloat tableWidth = self.tableView.bounds.size.width;
|
||||
@@ -224,7 +286,7 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
tableWidth = [UIScreen mainScreen].bounds.size.width;
|
||||
}
|
||||
|
||||
NSLog(@"[KBAICommentView] 加载到 %ld 条评论,共 %ld 条", (long)pageModel.records.count, (long)pageModel.total);
|
||||
NSLog(@"[KBAICommentView] 加载到 %ld 条评论,共 %ld 条,页码:%ld/%ld", (long)pageModel.records.count, (long)pageModel.total, (long)pageModel.current, (long)pageModel.pages);
|
||||
|
||||
for (KBCommentItem *item in pageModel.records) {
|
||||
// 转换为 KBAICommentModel(使用 MJExtension)
|
||||
@@ -243,6 +305,20 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
||||
|
||||
[self updateTitle];
|
||||
[self.tableView reloadData];
|
||||
|
||||
// 更新分页状态
|
||||
self.currentPage = pageModel.current > 0 ? pageModel.current : self.currentPage;
|
||||
if (pageModel.pages > 0) {
|
||||
self.hasMoreData = pageModel.current < pageModel.pages;
|
||||
} else {
|
||||
self.hasMoreData = pageModel.records.count >= self.pageSize;
|
||||
}
|
||||
|
||||
if (self.hasMoreData) {
|
||||
[self.tableView.mj_footer endRefreshing];
|
||||
} else {
|
||||
[self.tableView.mj_footer endRefreshingWithNoMoreData];
|
||||
}
|
||||
|
||||
// 根据数据是否为空,动态控制空态显示
|
||||
if (self.comments.count == 0) {
|
||||
@@ -641,6 +717,7 @@ static NSInteger const kRepliesLoadCount = 5;
|
||||
_tableView.backgroundColor = [UIColor clearColor];
|
||||
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
||||
_tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
|
||||
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 0.01)];
|
||||
|
||||
// 关闭空数据占位,避免加载时显示"暂无数据"
|
||||
_tableView.useEmptyDataSet = NO;
|
||||
|
||||
Reference in New Issue
Block a user