基本ok
This commit is contained in:
@@ -44,6 +44,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
/// 是否正在加载
|
||||
@property (nonatomic, assign) BOOL isLoading;
|
||||
|
||||
@property (nonatomic, assign) BOOL canTriggerLoadMore;
|
||||
|
||||
/// 当前页码
|
||||
@property (nonatomic, assign) NSInteger currentPage;
|
||||
|
||||
@@ -94,6 +96,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
// 重置加载状态标志(但不清空 hasLoadedData)
|
||||
self.isLoading = NO;
|
||||
self.canTriggerLoadMore = YES;
|
||||
|
||||
// ✅ 移除了 self.hasLoadedData = NO;
|
||||
// 这样 Cell 复用时不会重复请求数据
|
||||
@@ -174,6 +177,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
// 重置状态
|
||||
self.isLoading = NO;
|
||||
self.canTriggerLoadMore = YES;
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
|
||||
@@ -258,21 +262,25 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
pageNum:self.currentPage
|
||||
pageSize:20
|
||||
completion:^(KBChatHistoryPageModel *pageModel, NSError *error) {
|
||||
weakSelf.isLoading = NO;
|
||||
|
||||
if (error) {
|
||||
NSLog(@"[KBPersonaChatCell] 加载聊天记录失败:%@", error.localizedDescription);
|
||||
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
|
||||
|
||||
// 如果是第一次加载失败,显示开场白
|
||||
if (weakSelf.currentPage == 1 && weakSelf.persona.introText.length > 0) {
|
||||
[weakSelf showOpeningMessage];
|
||||
}
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
return;
|
||||
}
|
||||
|
||||
weakSelf.hasLoadedData = YES;
|
||||
weakSelf.hasMoreHistory = pageModel.hasMore;
|
||||
if (error) {
|
||||
NSLog(@"[KBPersonaChatCell] 加载聊天记录失败:%@", error.localizedDescription);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
strongSelf.isLoading = NO;
|
||||
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
|
||||
if (strongSelf.currentPage == 1 && strongSelf.persona.introText.length > 0) {
|
||||
[strongSelf showOpeningMessage];
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
strongSelf.hasLoadedData = YES;
|
||||
strongSelf.hasMoreHistory = pageModel.hasMore;
|
||||
|
||||
// 转换为 KBAiChatMessage
|
||||
NSMutableArray *newMessages = [NSMutableArray array];
|
||||
@@ -296,56 +304,54 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
message.isComplete = YES;
|
||||
message.needsTypewriterEffect = NO;
|
||||
[newMessages addObject:message];
|
||||
// [newMessages addObject:message];
|
||||
[newMessages insertObject:message atIndex:0];
|
||||
}
|
||||
|
||||
// 插入历史消息(确保开场白始终是第一条)
|
||||
// 关键修复:在 dispatch_async 之前保存当前页码,避免异步执行时 currentPage 已经被递增
|
||||
NSInteger loadedPage = weakSelf.currentPage;
|
||||
NSInteger loadedPage = strongSelf.currentPage;
|
||||
|
||||
if (loadedPage == 1) {
|
||||
// 第一页,直接赋值
|
||||
weakSelf.messages = newMessages;
|
||||
[weakSelf ensureOpeningMessageAtTop];
|
||||
strongSelf.messages = newMessages;
|
||||
[strongSelf ensureOpeningMessageAtTop];
|
||||
} else {
|
||||
// 后续页,插入到开场白之后
|
||||
[weakSelf ensureOpeningMessageAtTop];
|
||||
[strongSelf ensureOpeningMessageAtTop];
|
||||
if (newMessages.count > 0) {
|
||||
NSUInteger insertIndex = [weakSelf hasOpeningMessageAtTop] ? 1 : 0;
|
||||
NSUInteger insertIndex = [strongSelf hasOpeningMessageAtTop] ? 1 : 0;
|
||||
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(insertIndex, newMessages.count)];
|
||||
[weakSelf.messages insertObjects:newMessages atIndexes:indexSet];
|
||||
[strongSelf.messages insertObjects:newMessages atIndexes:indexSet];
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新 UI
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
NSLog(@"[KBPersonaChatCell] ⚠️ strongSelf 为空,跳过刷新");
|
||||
return;
|
||||
if (loadedPage == 1) {
|
||||
NSLog(@"[KBPersonaChatCell] 刷新 UI - loadedPage: %ld, keepOffset: 0, scrollToBottom: 1",
|
||||
(long)loadedPage);
|
||||
[strongSelf.chatView reloadWithMessages:strongSelf.messages
|
||||
keepOffset:NO
|
||||
scrollToBottom:YES];
|
||||
} else {
|
||||
NSLog(@"[KBPersonaChatCell] 刷新 UI - loadedPage: %ld, prependHistory",
|
||||
(long)loadedPage);
|
||||
KBAiChatMessage *openingMessage = [strongSelf hasOpeningMessageAtTop] ? strongSelf.messages.firstObject : nil;
|
||||
[strongSelf.chatView prependHistoryMessages:newMessages openingMessage:openingMessage];
|
||||
}
|
||||
|
||||
// 使用保存的页码判断,而不是 currentPage(可能已被递增)
|
||||
BOOL keepOffset = (loadedPage != 1);
|
||||
BOOL scrollToBottom = (loadedPage == 1);
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] 刷新 UI - loadedPage: %ld, keepOffset: %d, scrollToBottom: %d",
|
||||
(long)loadedPage, keepOffset, scrollToBottom);
|
||||
|
||||
[strongSelf.chatView reloadWithMessages:strongSelf.messages
|
||||
keepOffset:keepOffset
|
||||
scrollToBottom:scrollToBottom];
|
||||
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
|
||||
|
||||
// ✅ 保存到缓存(包含开场白)
|
||||
[[KBAIChatMessageCacheManager shared] saveMessages:strongSelf.messages
|
||||
forCompanionId:companionId];
|
||||
strongSelf.isLoading = NO;
|
||||
});
|
||||
|
||||
weakSelf.currentPage++;
|
||||
strongSelf.currentPage++;
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] 加载成功:第 %ld 页,%ld 条消息,还有更多:%@",
|
||||
(long)weakSelf.currentPage - 1,
|
||||
(long)strongSelf.currentPage - 1,
|
||||
(long)newMessages.count,
|
||||
pageModel.hasMore ? @"是" : @"否");
|
||||
}];
|
||||
@@ -357,7 +363,6 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
return;
|
||||
}
|
||||
|
||||
self.currentPage++;
|
||||
[self loadChatHistory];
|
||||
}
|
||||
|
||||
@@ -552,8 +557,11 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
CGFloat offsetY = scrollView.contentOffset.y;
|
||||
|
||||
// 下拉到顶部,加载历史消息
|
||||
if (offsetY <= -50 && !self.isLoading) {
|
||||
if (offsetY <= 50 && !self.isLoading && self.canTriggerLoadMore && self.hasMoreHistory) {
|
||||
self.canTriggerLoadMore = NO;
|
||||
[self loadMoreHistory];
|
||||
} else if (offsetY > -20) {
|
||||
self.canTriggerLoadMore = YES;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user