This commit is contained in:
2026-01-30 21:24:17 +08:00
parent 2ff8a7a4af
commit cc82396195
5 changed files with 222 additions and 45 deletions

View File

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