1
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#import "AiVM.h"
|
||||
#import "KBImagePositionButton.h"
|
||||
#import "KBAICommentView.h"
|
||||
#import "KBAIChatMessageCacheManager.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
#import <LSTPopView/LSTPopView.h>
|
||||
@@ -71,16 +72,18 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
/// 关键修复:Cell 复用时重置状态
|
||||
/// 关键修复:Cell 复用时不清空数据,避免重复请求
|
||||
- (void)prepareForReuse {
|
||||
[super prepareForReuse];
|
||||
|
||||
// 停止音频播放
|
||||
[self.chatView stopPlayingAudio];
|
||||
|
||||
// 重置加载状态
|
||||
// 重置加载状态标志(但不清空 hasLoadedData)
|
||||
self.isLoading = NO;
|
||||
self.hasLoadedData = NO;
|
||||
|
||||
// ✅ 移除了 self.hasLoadedData = NO;
|
||||
// 这样 Cell 复用时不会重复请求数据
|
||||
}
|
||||
|
||||
#pragma mark - 1:控件初始化
|
||||
@@ -157,11 +160,23 @@
|
||||
_persona = persona;
|
||||
|
||||
// 重置状态
|
||||
self.hasLoadedData = NO;
|
||||
self.isLoading = NO;
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
self.messages = [NSMutableArray array];
|
||||
|
||||
// ✅ 尝试从缓存加载 messages
|
||||
NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
|
||||
if (cachedMessages.count > 0) {
|
||||
// 缓存命中,直接使用
|
||||
self.messages = [cachedMessages mutableCopy];
|
||||
self.hasLoadedData = YES;
|
||||
NSLog(@"[Cell] ✅ 从缓存加载:personaId=%ld, 消息数=%ld", (long)persona.personaId, (long)cachedMessages.count);
|
||||
} else {
|
||||
// 缓存未命中,需要请求
|
||||
self.messages = [NSMutableArray array];
|
||||
self.hasLoadedData = NO;
|
||||
NSLog(@"[Cell] ⚠️ 缓存未命中:personaId=%ld, 需要请求数据", (long)persona.personaId);
|
||||
}
|
||||
|
||||
// 设置 UI
|
||||
[self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:persona.coverImageUrl]
|
||||
@@ -173,11 +188,19 @@
|
||||
|
||||
// 关键修复:清空消息时停止音频播放,避免状态混乱
|
||||
[self.chatView stopPlayingAudio];
|
||||
[self.chatView clearMessages];
|
||||
|
||||
// 如果有缓存,直接显示
|
||||
if (self.messages.count > 0) {
|
||||
[self.chatView reloadWithMessages:self.messages
|
||||
hasMoreHistory:self.hasMoreHistory
|
||||
completion:nil];
|
||||
} else {
|
||||
[self.chatView clearMessages];
|
||||
}
|
||||
|
||||
[self.commentButton setTitle:persona.commentCount forState:UIControlStateNormal];
|
||||
[self.likeButton setTitle:persona.likeCount forState:UIControlStateNormal];
|
||||
self.likeButton.selected = persona.liked;
|
||||
|
||||
}
|
||||
|
||||
#pragma mark - 2:数据加载
|
||||
@@ -269,10 +292,16 @@
|
||||
keepOffset:keepOffset
|
||||
scrollToBottom:scrollToBottom];
|
||||
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
|
||||
|
||||
// ✅ 保存到缓存
|
||||
[[KBAIChatMessageCacheManager shared] saveMessages:weakSelf.messages
|
||||
forCompanionId:companionId];
|
||||
});
|
||||
|
||||
weakSelf.currentPage++;
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] 加载成功:第 %ld 页,%ld 条消息,还有更多:%@",
|
||||
(long)weakSelf.currentPage,
|
||||
(long)weakSelf.currentPage - 1,
|
||||
(long)newMessages.count,
|
||||
pageModel.hasMore ? @"是" : @"否");
|
||||
}];
|
||||
|
||||
Reference in New Issue
Block a user