处理键盘bug
This commit is contained in:
@@ -855,8 +855,7 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
|
|||||||
if (text.length == 0) {
|
if (text.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSLog(@"[Keyboard] ========== kb_sendChatText ==========");
|
NSLog(@"[KB] 发送消息: %@", text);
|
||||||
NSLog(@"[Keyboard] chatPanelView=%p", self.chatPanelView);
|
|
||||||
|
|
||||||
KBChatMessage *outgoing = [KBChatMessage userMessageWithText:text];
|
KBChatMessage *outgoing = [KBChatMessage userMessageWithText:text];
|
||||||
outgoing.avatarURL = [self kb_sharedUserAvatarURL];
|
outgoing.avatarURL = [self kb_sharedUserAvatarURL];
|
||||||
@@ -869,7 +868,6 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 添加 loading 消息
|
// 添加 loading 消息
|
||||||
NSLog(@"[Keyboard] 准备添加 loading 消息,chatPanelView=%p", self.chatPanelView);
|
|
||||||
[self.chatPanelView kb_addLoadingAssistantMessage];
|
[self.chatPanelView kb_addLoadingAssistantMessage];
|
||||||
|
|
||||||
// 调用新的聊天接口
|
// 调用新的聊天接口
|
||||||
@@ -932,16 +930,9 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_reloadChatRowForMessage:(KBChatMessage *)message {
|
- (void)kb_reloadChatRowForMessage:(KBChatMessage *)message {
|
||||||
NSLog(@"[Keyboard] ========== kb_reloadChatRowForMessage ==========");
|
// 头像预加载完成后不需要刷新表格
|
||||||
// 不再使用 self.chatMessages,直接刷新 tableView
|
// 因为键盘扩展的聊天面板不显示头像,所以这里直接返回
|
||||||
UITableView *tableView = self.chatPanelView.tableView;
|
// 如果将来需要显示头像,可以只刷新特定行而不是整个表格
|
||||||
if (!tableView) {
|
|
||||||
NSLog(@"[Keyboard] tableView 为空,跳过");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 刷新整个 tableView
|
|
||||||
NSLog(@"[Keyboard] 调用 tableView reloadData");
|
|
||||||
[tableView reloadData];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_requestChatAudioForText:(NSString *)text {
|
- (void)kb_requestChatAudioForText:(NSString *)text {
|
||||||
@@ -1011,51 +1002,40 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
|
|||||||
|
|
||||||
/// 调用新的聊天接口(返回文本和 audioId)
|
/// 调用新的聊天接口(返回文本和 audioId)
|
||||||
- (void)kb_requestChatMessageWithContent:(NSString *)content {
|
- (void)kb_requestChatMessageWithContent:(NSString *)content {
|
||||||
NSLog(@"[Keyboard] ========== kb_requestChatMessageWithContent ==========");
|
|
||||||
NSLog(@"[Keyboard] 请求内容: %@", content);
|
|
||||||
|
|
||||||
if (content.length == 0) {
|
if (content.length == 0) {
|
||||||
NSLog(@"[Keyboard] ❌ 内容为空,移除 loading");
|
|
||||||
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从 AppGroup 获取选中的 persona companionId
|
|
||||||
NSInteger companionId = [[KBVM shared] selectedCompanionIdFromAppGroup];
|
NSInteger companionId = [[KBVM shared] selectedCompanionIdFromAppGroup];
|
||||||
NSLog(@"[Keyboard] 发送聊天请求: companionId=%ld", (long)companionId);
|
NSLog(@"[KB] 请求聊天: companionId=%ld", (long)companionId);
|
||||||
|
|
||||||
__weak typeof(self) weakSelf = self;
|
__weak typeof(self) weakSelf = self;
|
||||||
[[KBVM shared] sendChatMessageWithContent:content
|
[[KBVM shared] sendChatMessageWithContent:content
|
||||||
companionId:companionId
|
companionId:companionId
|
||||||
completion:^(KBChatResponse *response) {
|
completion:^(KBChatResponse *response) {
|
||||||
__strong typeof(weakSelf) self = weakSelf;
|
__strong typeof(weakSelf) self = weakSelf;
|
||||||
if (!self) {
|
if (!self) return;
|
||||||
NSLog(@"[Keyboard] ❌ self 为空");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NSLog(@"[Keyboard] 回调中 chatPanelView=%p", self.chatPanelView);
|
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
NSLog(@"[Keyboard] ❌ 请求失败: %@", response.errorMessage);
|
NSLog(@"[KB] ❌ 请求失败: %@", response.errorMessage);
|
||||||
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
||||||
[KBHUD showInfo:response.errorMessage ?: KBLocalized(@"请求失败")];
|
[KBHUD showInfo:response.errorMessage ?: KBLocalized(@"请求失败")];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLog(@"[Keyboard] ✅ 解析结果: text=%@, audioId=%@", response.text, response.audioId);
|
NSLog(@"[KB] ✅ 收到回复: %@", response.text);
|
||||||
|
|
||||||
if (response.text.length == 0) {
|
if (response.text.length == 0) {
|
||||||
NSLog(@"[Keyboard] ❌ 文本为空,移除 loading");
|
|
||||||
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
[self.chatPanelView kb_removeLoadingAssistantMessage];
|
||||||
[KBHUD showInfo:KBLocalized(@"未获取到回复内容")];
|
[KBHUD showInfo:KBLocalized(@"未获取到回复内容")];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLog(@"[Keyboard] 准备调用 kb_addAssistantMessage, chatPanelView=%p", self.chatPanelView);
|
|
||||||
// 添加 AI 消息(带打字机效果)
|
// 添加 AI 消息(带打字机效果)
|
||||||
|
NSLog(@"[KB] 准备添加 AI 消息");
|
||||||
[self.chatPanelView kb_addAssistantMessage:response.text audioId:response.audioId];
|
[self.chatPanelView kb_addAssistantMessage:response.text audioId:response.audioId];
|
||||||
NSLog(@"[Keyboard] kb_addAssistantMessage 调用完成");
|
NSLog(@"[KB] AI 消息添加完成");
|
||||||
|
|
||||||
// 如果有 audioId,开始预加载音频
|
// 如果有 audioId,开始预加载音频
|
||||||
if (response.audioId.length > 0) {
|
if (response.audioId.length > 0) {
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
|
|
||||||
- (instancetype)initWithFrame:(CGRect)frame {
|
- (instancetype)initWithFrame:(CGRect)frame {
|
||||||
if (self = [super initWithFrame:frame]) {
|
if (self = [super initWithFrame:frame]) {
|
||||||
NSLog(@"[KBChatPanelView] ⚠️ initWithFrame 被调用,self=%p", self);
|
|
||||||
self.backgroundColor = [UIColor clearColor];
|
self.backgroundColor = [UIColor clearColor];
|
||||||
self.messages = [NSMutableArray array];
|
self.messages = [NSMutableArray array];
|
||||||
|
|
||||||
@@ -50,9 +49,7 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
#pragma mark - Public
|
#pragma mark - Public
|
||||||
|
|
||||||
- (void)kb_reloadWithMessages:(NSArray<KBChatMessage *> *)messages {
|
- (void)kb_reloadWithMessages:(NSArray<KBChatMessage *> *)messages {
|
||||||
NSLog(@"[KBChatPanelView] ========== kb_reloadWithMessages ==========");
|
NSLog(@"[Panel] ⚠️ kb_reloadWithMessages 被调用,传入 %lu 条消息", (unsigned long)messages.count);
|
||||||
NSLog(@"[KBChatPanelView] self=%p, 传入消息数量: %lu", self, (unsigned long)messages.count);
|
|
||||||
NSLog(@"[KBChatPanelView] 调用堆栈: %@", [NSThread callStackSymbols]);
|
|
||||||
|
|
||||||
[self.messages removeAllObjects];
|
[self.messages removeAllObjects];
|
||||||
if (messages.count > 0) {
|
if (messages.count > 0) {
|
||||||
@@ -65,94 +62,95 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
- (void)kb_addUserMessage:(NSString *)text {
|
- (void)kb_addUserMessage:(NSString *)text {
|
||||||
if (text.length == 0) return;
|
if (text.length == 0) return;
|
||||||
|
|
||||||
NSLog(@"[KBChatPanelView] ========== kb_addUserMessage ==========");
|
NSLog(@"[Panel] 添加用户消息: %@,当前消息数: %lu", text, (unsigned long)self.messages.count);
|
||||||
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
|
|
||||||
NSLog(@"[KBChatPanelView] 添加用户消息: %@", text);
|
|
||||||
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
|
|
||||||
|
|
||||||
KBChatMessage *msg = [KBChatMessage userMessageWithText:text];
|
KBChatMessage *msg = [KBChatMessage userMessageWithText:text];
|
||||||
NSLog(@"[KBChatPanelView] 创建消息 - outgoing: %d, text: %@", msg.outgoing, msg.text);
|
|
||||||
[self kb_appendMessage:msg];
|
[self kb_appendMessage:msg];
|
||||||
|
|
||||||
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
|
NSLog(@"[Panel] 添加后消息数: %lu", (unsigned long)self.messages.count);
|
||||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
|
||||||
KBChatMessage *m = self.messages[i];
|
|
||||||
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_addLoadingAssistantMessage {
|
- (void)kb_addLoadingAssistantMessage {
|
||||||
NSLog(@"[KBChatPanelView] ========== kb_addLoadingAssistantMessage ==========");
|
NSLog(@"[Panel] 添加 loading 消息,当前消息数: %lu", (unsigned long)self.messages.count);
|
||||||
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
|
|
||||||
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
|
|
||||||
|
|
||||||
KBChatMessage *msg = [KBChatMessage loadingAssistantMessage];
|
KBChatMessage *msg = [KBChatMessage loadingAssistantMessage];
|
||||||
NSLog(@"[KBChatPanelView] 创建 loading 消息 - outgoing: %d, isLoading: %d", msg.outgoing, msg.isLoading);
|
|
||||||
[self kb_appendMessage:msg];
|
[self kb_appendMessage:msg];
|
||||||
|
|
||||||
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
|
NSLog(@"[Panel] 添加后消息数: %lu", (unsigned long)self.messages.count);
|
||||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
|
||||||
KBChatMessage *m = self.messages[i];
|
|
||||||
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_removeLoadingAssistantMessage {
|
- (void)kb_removeLoadingAssistantMessage {
|
||||||
NSLog(@"[KBChatPanelView] ========== kb_removeLoadingAssistantMessage ==========");
|
NSLog(@"[Panel] 移除 loading 消息,当前消息数: %lu", (unsigned long)self.messages.count);
|
||||||
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
|
|
||||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
|
||||||
KBChatMessage *m = self.messages[i];
|
|
||||||
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
||||||
KBChatMessage *msg = self.messages[i];
|
KBChatMessage *msg = self.messages[i];
|
||||||
NSLog(@"[KBChatPanelView] 检查消息[%ld]: outgoing=%d, isLoading=%d", (long)i, msg.outgoing, msg.isLoading);
|
|
||||||
// 只移除 AI 消息(outgoing == NO)且是 loading 状态的
|
// 只移除 AI 消息(outgoing == NO)且是 loading 状态的
|
||||||
if (!msg.outgoing && msg.isLoading) {
|
if (!msg.outgoing && msg.isLoading) {
|
||||||
NSLog(@"[KBChatPanelView] ✅ 找到 loading AI 消息,准备移除索引: %ld", (long)i);
|
NSLog(@"[Panel] ✅ 找到 loading 消息,移除索引: %ld", (long)i);
|
||||||
[self.messages removeObjectAtIndex:i];
|
[self.messages removeObjectAtIndex:i];
|
||||||
|
|
||||||
|
// 使用 beginUpdates/endUpdates 包裹删除操作
|
||||||
|
[self.tableViewInternal beginUpdates];
|
||||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
||||||
[self.tableViewInternal deleteRowsAtIndexPaths:@[indexPath]
|
[self.tableViewInternal deleteRowsAtIndexPaths:@[indexPath]
|
||||||
withRowAnimation:UITableViewRowAnimationNone];
|
withRowAnimation:UITableViewRowAnimationNone];
|
||||||
NSLog(@"[KBChatPanelView] 移除后消息数量: %lu", (unsigned long)self.messages.count);
|
[self.tableViewInternal endUpdates];
|
||||||
|
|
||||||
|
NSLog(@"[Panel] 移除后消息数: %lu", (unsigned long)self.messages.count);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLog(@"[KBChatPanelView] 最终消息数量: %lu", (unsigned long)self.messages.count);
|
|
||||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
|
||||||
KBChatMessage *m = self.messages[i];
|
|
||||||
NSLog(@"[KBChatPanelView] 最终消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_addAssistantMessage:(NSString *)text audioId:(NSString *)audioId {
|
- (void)kb_addAssistantMessage:(NSString *)text audioId:(NSString *)audioId {
|
||||||
NSLog(@"[KBChatPanelView] ========== kb_addAssistantMessage ==========");
|
NSLog(@"[Panel] ========== kb_addAssistantMessage ==========");
|
||||||
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
|
NSLog(@"[Panel] 当前消息数: %lu", (unsigned long)self.messages.count);
|
||||||
NSLog(@"[KBChatPanelView] AI 回复文本: %@", text);
|
|
||||||
NSLog(@"[KBChatPanelView] audioId: %@", audioId);
|
|
||||||
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
|
|
||||||
|
|
||||||
// 先移除 loading 消息
|
// 查找 loading 消息的索引
|
||||||
[self kb_removeLoadingAssistantMessage];
|
NSInteger loadingIndex = -1;
|
||||||
|
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
||||||
NSLog(@"[KBChatPanelView] 移除 loading 后消息数量: %lu", (unsigned long)self.messages.count);
|
KBChatMessage *msg = self.messages[i];
|
||||||
|
if (!msg.outgoing && msg.isLoading) {
|
||||||
|
loadingIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建 AI 消息
|
||||||
KBChatMessage *msg = [KBChatMessage assistantMessageWithText:text audioId:audioId];
|
KBChatMessage *msg = [KBChatMessage assistantMessageWithText:text audioId:audioId];
|
||||||
msg.displayName = KBLocalized(@"AI助手");
|
msg.displayName = KBLocalized(@"AI助手");
|
||||||
NSLog(@"[KBChatPanelView] 创建 AI 消息 - outgoing: %d, isLoading: %d, needsTypewriter: %d, text: %@",
|
NSLog(@"[Panel] 创建 AI 消息,needsTypewriter: %d", msg.needsTypewriterEffect);
|
||||||
msg.outgoing, msg.isLoading, msg.needsTypewriterEffect, msg.text);
|
|
||||||
[self kb_appendMessage:msg];
|
|
||||||
|
|
||||||
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
|
// 使用批量更新,避免界面跳动
|
||||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
[self.tableViewInternal beginUpdates];
|
||||||
KBChatMessage *m = self.messages[i];
|
|
||||||
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
|
if (loadingIndex >= 0) {
|
||||||
|
// 移除 loading 消息
|
||||||
|
NSLog(@"[Panel] 移除 loading 索引: %ld", (long)loadingIndex);
|
||||||
|
[self.messages removeObjectAtIndex:loadingIndex];
|
||||||
|
NSIndexPath *deleteIndexPath = [NSIndexPath indexPathForRow:loadingIndex inSection:0];
|
||||||
|
[self.tableViewInternal deleteRowsAtIndexPaths:@[deleteIndexPath]
|
||||||
|
withRowAnimation:UITableViewRowAnimationNone];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加 AI 消息
|
||||||
|
NSInteger insertIndex = self.messages.count;
|
||||||
|
[self.messages addObject:msg];
|
||||||
|
NSLog(@"[Panel] 插入 AI 消息索引: %ld", (long)insertIndex);
|
||||||
|
NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:insertIndex inSection:0];
|
||||||
|
[self.tableViewInternal insertRowsAtIndexPaths:@[insertIndexPath]
|
||||||
|
withRowAnimation:UITableViewRowAnimationNone];
|
||||||
|
|
||||||
|
[self.tableViewInternal endUpdates];
|
||||||
|
|
||||||
|
// 滚动到底部
|
||||||
|
[self kb_scrollToBottom];
|
||||||
|
|
||||||
|
NSLog(@"[Panel] 添加后消息数: %lu", (unsigned long)self.messages.count);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)kb_updateLastAssistantMessageWithAudioData:(NSData *)audioData duration:(NSTimeInterval)duration {
|
- (void)kb_updateLastAssistantMessageWithAudioData:(NSData *)audioData duration:(NSTimeInterval)duration {
|
||||||
|
NSLog(@"[Panel] 更新音频数据,duration: %.2f", duration);
|
||||||
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
||||||
KBChatMessage *msg = self.messages[i];
|
KBChatMessage *msg = self.messages[i];
|
||||||
// 只更新 AI 消息(outgoing == NO)且非 loading 状态的
|
// 只更新 AI 消息(outgoing == NO)且非 loading 状态的
|
||||||
@@ -160,19 +158,12 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
msg.audioData = audioData;
|
msg.audioData = audioData;
|
||||||
msg.audioDuration = duration;
|
msg.audioDuration = duration;
|
||||||
|
|
||||||
// 刷新该行以更新语音时长显示
|
// 不刷新 Cell,避免打断打字机效果
|
||||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
|
||||||
KBChatAssistantCell *cell = [self.tableViewInternal cellForRowAtIndexPath:indexPath];
|
|
||||||
if ([cell isKindOfClass:[KBChatAssistantCell class]]) {
|
|
||||||
// 直接更新 Cell,不刷新整行(避免打断打字机效果)
|
|
||||||
if (duration > 0) {
|
if (duration > 0) {
|
||||||
// 通过重新配置来更新时长显示
|
|
||||||
// 但不要触发打字机效果
|
|
||||||
msg.needsTypewriterEffect = NO;
|
msg.needsTypewriterEffect = NO;
|
||||||
msg.isComplete = YES;
|
msg.isComplete = YES;
|
||||||
}
|
}
|
||||||
}
|
NSLog(@"[Panel] ✅ 音频数据已更新");
|
||||||
NSLog(@"[KBChatPanelView] 更新 AI 消息音频数据,时长: %.2f秒", duration);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,11 +172,13 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
- (void)kb_scrollToBottom {
|
- (void)kb_scrollToBottom {
|
||||||
if (self.messages.count == 0) return;
|
if (self.messages.count == 0) return;
|
||||||
|
|
||||||
|
NSLog(@"[Panel] 滚动到底部,消息数: %lu", (unsigned long)self.messages.count);
|
||||||
|
|
||||||
[self.tableViewInternal layoutIfNeeded];
|
[self.tableViewInternal layoutIfNeeded];
|
||||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
|
||||||
[self.tableViewInternal scrollToRowAtIndexPath:indexPath
|
[self.tableViewInternal scrollToRowAtIndexPath:indexPath
|
||||||
atScrollPosition:UITableViewScrollPositionBottom
|
atScrollPosition:UITableViewScrollPositionBottom
|
||||||
animated:YES];
|
animated:NO]; // 改为 NO,避免动画导致跳动
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Private
|
#pragma mark - Private
|
||||||
@@ -195,21 +188,25 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
|
|
||||||
NSInteger oldCount = self.messages.count;
|
NSInteger oldCount = self.messages.count;
|
||||||
[self.messages addObject:message];
|
[self.messages addObject:message];
|
||||||
|
NSLog(@"[Panel] kb_appendMessage: oldCount=%ld, newCount=%lu", (long)oldCount, (unsigned long)self.messages.count);
|
||||||
|
|
||||||
// 限制消息数量
|
// 限制消息数量
|
||||||
if (self.messages.count > kKBChatMessageLimit) {
|
if (self.messages.count > kKBChatMessageLimit) {
|
||||||
NSUInteger overflow = self.messages.count - kKBChatMessageLimit;
|
NSUInteger overflow = self.messages.count - kKBChatMessageLimit;
|
||||||
[self.messages removeObjectsInRange:NSMakeRange(0, overflow)];
|
[self.messages removeObjectsInRange:NSMakeRange(0, overflow)];
|
||||||
|
NSLog(@"[Panel] 消息超限,reloadData");
|
||||||
[self.tableViewInternal reloadData];
|
[self.tableViewInternal reloadData];
|
||||||
} else {
|
} else {
|
||||||
|
NSLog(@"[Panel] 插入新行: %ld", (long)oldCount);
|
||||||
|
[self.tableViewInternal beginUpdates];
|
||||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:oldCount inSection:0];
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:oldCount inSection:0];
|
||||||
[self.tableViewInternal insertRowsAtIndexPaths:@[indexPath]
|
[self.tableViewInternal insertRowsAtIndexPaths:@[indexPath]
|
||||||
withRowAnimation:UITableViewRowAnimationNone];
|
withRowAnimation:UITableViewRowAnimationNone];
|
||||||
|
[self.tableViewInternal endUpdates];
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
// 直接滚动,不用 dispatch_async
|
||||||
[self kb_scrollToBottom];
|
[self kb_scrollToBottom];
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Actions
|
#pragma mark - Actions
|
||||||
@@ -227,26 +224,21 @@ static const NSUInteger kKBChatMessageLimit = 10;
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||||
NSLog(@"[KBChatPanelView] ========== cellForRowAtIndexPath: %ld ==========", (long)indexPath.row);
|
|
||||||
|
|
||||||
if (indexPath.row >= self.messages.count) {
|
if (indexPath.row >= self.messages.count) {
|
||||||
NSLog(@"[KBChatPanelView] ❌ 索引越界,返回空 Cell");
|
NSLog(@"[Panel] ❌ cellForRow 索引越界: %ld >= %lu", (long)indexPath.row, (unsigned long)self.messages.count);
|
||||||
return [[UITableViewCell alloc] init];
|
return [[UITableViewCell alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
KBChatMessage *msg = self.messages[indexPath.row];
|
KBChatMessage *msg = self.messages[indexPath.row];
|
||||||
NSLog(@"[KBChatPanelView] 消息: outgoing=%d, isLoading=%d, needsTypewriter=%d, text=%@",
|
NSLog(@"[Panel] cellForRow[%ld]: outgoing=%d, isLoading=%d", (long)indexPath.row, msg.outgoing, msg.isLoading);
|
||||||
msg.outgoing, msg.isLoading, msg.needsTypewriterEffect, msg.text);
|
|
||||||
|
|
||||||
if (msg.outgoing) {
|
if (msg.outgoing) {
|
||||||
// 用户消息(右侧)
|
// 用户消息(右侧)
|
||||||
NSLog(@"[KBChatPanelView] 使用 KBChatUserCell");
|
|
||||||
KBChatUserCell *cell = [tableView dequeueReusableCellWithIdentifier:kUserCellIdentifier forIndexPath:indexPath];
|
KBChatUserCell *cell = [tableView dequeueReusableCellWithIdentifier:kUserCellIdentifier forIndexPath:indexPath];
|
||||||
[cell configureWithMessage:msg];
|
[cell configureWithMessage:msg];
|
||||||
return cell;
|
return cell;
|
||||||
} else {
|
} else {
|
||||||
// AI 消息(左侧)
|
// AI 消息(左侧)
|
||||||
NSLog(@"[KBChatPanelView] 使用 KBChatAssistantCell");
|
|
||||||
KBChatAssistantCell *cell = [tableView dequeueReusableCellWithIdentifier:kAssistantCellIdentifier forIndexPath:indexPath];
|
KBChatAssistantCell *cell = [tableView dequeueReusableCellWithIdentifier:kAssistantCellIdentifier forIndexPath:indexPath];
|
||||||
cell.delegate = self;
|
cell.delegate = self;
|
||||||
[cell configureWithMessage:msg];
|
[cell configureWithMessage:msg];
|
||||||
|
|||||||
Reference in New Issue
Block a user