2
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
|
||||
@interface KBPersonaChatCell () <UITableViewDelegate, UITableViewDataSource>
|
||||
@interface KBPersonaChatCell () <KBChatTableViewDelegate>
|
||||
|
||||
/// 背景图
|
||||
@property (nonatomic, strong) UIImageView *backgroundImageView;
|
||||
@@ -28,7 +28,7 @@
|
||||
@property (nonatomic, strong) UILabel *openingLabel;
|
||||
|
||||
/// 聊天列表
|
||||
@property (nonatomic, strong) UITableView *tableView;
|
||||
@property (nonatomic, strong) KBChatTableView *chatView;
|
||||
|
||||
/// 聊天消息
|
||||
@property (nonatomic, strong) NSMutableArray<KBAiChatMessage *> *messages;
|
||||
@@ -102,8 +102,8 @@
|
||||
}];
|
||||
|
||||
// 聊天列表
|
||||
[self.contentView addSubview:self.tableView];
|
||||
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
[self.contentView addSubview:self.chatView];
|
||||
[self.chatView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.openingLabel.mas_bottom).offset(30);
|
||||
make.left.right.bottom.equalTo(self.contentView);
|
||||
}];
|
||||
@@ -130,7 +130,7 @@
|
||||
self.nameLabel.text = persona.name;
|
||||
self.openingLabel.text = persona.shortDesc.length > 0 ? persona.shortDesc : persona.introText;
|
||||
|
||||
[self.tableView reloadData];
|
||||
[self.chatView clearMessages];
|
||||
}
|
||||
|
||||
#pragma mark - 2:数据加载
|
||||
@@ -145,11 +145,16 @@
|
||||
|
||||
- (void)loadChatHistory {
|
||||
if (self.isLoading || !self.hasMoreHistory) {
|
||||
[self.chatView endLoadMoreWithHasMoreData:self.hasMoreHistory];
|
||||
return;
|
||||
}
|
||||
|
||||
self.isLoading = YES;
|
||||
|
||||
if (self.currentPage == 1) {
|
||||
[self.chatView resetNoMoreData];
|
||||
}
|
||||
|
||||
// 使用 persona.personaId 作为 companionId
|
||||
NSInteger companionId = self.persona.personaId;
|
||||
|
||||
@@ -162,6 +167,7 @@
|
||||
|
||||
if (error) {
|
||||
NSLog(@"[KBPersonaChatCell] 加载聊天记录失败:%@", error.localizedDescription);
|
||||
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
|
||||
|
||||
// 如果是第一次加载失败,显示开场白
|
||||
if (weakSelf.currentPage == 1 && weakSelf.persona.introText.length > 0) {
|
||||
@@ -177,12 +183,24 @@
|
||||
NSMutableArray *newMessages = [NSMutableArray array];
|
||||
for (KBChatHistoryModel *item in pageModel.records) {
|
||||
KBAiChatMessage *message;
|
||||
if (item.isUserMessage) {
|
||||
|
||||
// 根据 sender 判断消息类型
|
||||
// sender = 1: 用户消息(右侧)
|
||||
// sender = 2: AI 消息(左侧)
|
||||
if (item.sender == KBChatSenderUser) {
|
||||
// 用户消息
|
||||
message = [KBAiChatMessage userMessageWithText:item.content];
|
||||
} else if (item.sender == KBChatSenderAssistant) {
|
||||
// AI 消息
|
||||
message = [KBAiChatMessage assistantMessageWithText:item.content];
|
||||
} else {
|
||||
// 未知类型,默认为 AI 消息
|
||||
NSLog(@"[KBPersonaChatCell] 未知的 sender 类型:%ld", (long)item.sender);
|
||||
message = [KBAiChatMessage assistantMessageWithText:item.content];
|
||||
}
|
||||
|
||||
message.isComplete = YES;
|
||||
message.needsTypewriterEffect = NO;
|
||||
[newMessages addObject:message];
|
||||
}
|
||||
|
||||
@@ -198,24 +216,12 @@
|
||||
|
||||
// 刷新 UI
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (weakSelf.currentPage == 1) {
|
||||
[weakSelf.tableView reloadData];
|
||||
|
||||
// 滚动到底部(最新消息)
|
||||
if (weakSelf.messages.count > 0) {
|
||||
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:weakSelf.messages.count - 1 inSection:0];
|
||||
[weakSelf.tableView scrollToRowAtIndexPath:lastIndexPath
|
||||
atScrollPosition:UITableViewScrollPositionBottom
|
||||
animated:NO];
|
||||
}
|
||||
} else {
|
||||
// 保持滚动位置
|
||||
CGFloat oldContentHeight = weakSelf.tableView.contentSize.height;
|
||||
[weakSelf.tableView reloadData];
|
||||
CGFloat newContentHeight = weakSelf.tableView.contentSize.height;
|
||||
CGFloat offsetY = newContentHeight - oldContentHeight;
|
||||
[weakSelf.tableView setContentOffset:CGPointMake(0, offsetY) animated:NO];
|
||||
}
|
||||
BOOL keepOffset = (weakSelf.currentPage != 1);
|
||||
BOOL scrollToBottom = (weakSelf.currentPage == 1);
|
||||
[weakSelf.chatView reloadWithMessages:weakSelf.messages
|
||||
keepOffset:keepOffset
|
||||
scrollToBottom:scrollToBottom];
|
||||
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
|
||||
});
|
||||
|
||||
NSLog(@"[KBPersonaChatCell] 加载成功:第 %ld 页,%ld 条消息,还有更多:%@",
|
||||
@@ -227,6 +233,7 @@
|
||||
|
||||
- (void)loadMoreHistory {
|
||||
if (!self.hasMoreHistory || self.isLoading) {
|
||||
[self.chatView endLoadMoreWithHasMoreData:self.hasMoreHistory];
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -238,41 +245,53 @@
|
||||
// 显示开场白作为第一条消息
|
||||
KBAiChatMessage *openingMsg = [KBAiChatMessage assistantMessageWithText:self.persona.introText];
|
||||
openingMsg.isComplete = YES;
|
||||
openingMsg.needsTypewriterEffect = NO;
|
||||
[self.messages addObject:openingMsg];
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self.tableView reloadData];
|
||||
[self.chatView reloadWithMessages:self.messages
|
||||
keepOffset:NO
|
||||
scrollToBottom:YES];
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - UITableViewDataSource
|
||||
#pragma mark - 3:消息追加
|
||||
|
||||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
||||
return self.messages.count;
|
||||
}
|
||||
|
||||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
|
||||
if (!cell) {
|
||||
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
|
||||
cell.backgroundColor = [UIColor clearColor];
|
||||
cell.textLabel.textColor = [UIColor whiteColor];
|
||||
cell.textLabel.numberOfLines = 0;
|
||||
- (void)appendUserMessage:(NSString *)text {
|
||||
if (text.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
KBAiChatMessage *message = self.messages[indexPath.row];
|
||||
cell.textLabel.text = message.text;
|
||||
if (!self.messages) {
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
return cell;
|
||||
KBAiChatMessage *message = [KBAiChatMessage userMessageWithText:text];
|
||||
[self.messages addObject:message];
|
||||
[self.chatView addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||
return UITableViewAutomaticDimension;
|
||||
- (void)appendAssistantMessage:(NSString *)text
|
||||
audioId:(NSString *)audioId {
|
||||
if (text.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self.messages) {
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
KBAiChatMessage *message = [KBAiChatMessage assistantMessageWithText:text
|
||||
audioId:audioId];
|
||||
message.needsTypewriterEffect = YES;
|
||||
[self.messages addObject:message];
|
||||
[self.chatView addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
#pragma mark - UIScrollViewDelegate
|
||||
#pragma mark - KBChatTableViewDelegate
|
||||
|
||||
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
||||
- (void)chatTableViewDidScroll:(KBChatTableView *)chatView
|
||||
scrollView:(UIScrollView *)scrollView {
|
||||
CGFloat offsetY = scrollView.contentOffset.y;
|
||||
|
||||
// 下拉到顶部,加载历史消息
|
||||
@@ -281,6 +300,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)chatTableViewDidTriggerLoadMore:(KBChatTableView *)chatView {
|
||||
[self loadMoreHistory];
|
||||
}
|
||||
|
||||
#pragma mark - Lazy Load
|
||||
|
||||
- (UIImageView *)backgroundImageView {
|
||||
@@ -325,22 +348,13 @@
|
||||
return _openingLabel;
|
||||
}
|
||||
|
||||
- (UITableView *)tableView {
|
||||
if (!_tableView) {
|
||||
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
||||
_tableView.delegate = self;
|
||||
_tableView.dataSource = self;
|
||||
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
||||
_tableView.backgroundColor = [UIColor clearColor];
|
||||
_tableView.showsVerticalScrollIndicator = NO;
|
||||
_tableView.estimatedRowHeight = 60;
|
||||
_tableView.rowHeight = UITableViewAutomaticDimension;
|
||||
|
||||
if (@available(iOS 11.0, *)) {
|
||||
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
||||
}
|
||||
- (KBChatTableView *)chatView {
|
||||
if (!_chatView) {
|
||||
_chatView = [[KBChatTableView alloc] init];
|
||||
_chatView.backgroundColor = [UIColor clearColor];
|
||||
_chatView.delegate = self;
|
||||
}
|
||||
return _tableView;
|
||||
return _chatView;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user