This commit is contained in:
2026-01-28 18:58:30 +08:00
parent 93a20cd92a
commit 66d85f78a0
8 changed files with 310 additions and 8 deletions

View File

@@ -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 ? @"是" : @"否");
}];