跨进程 键盘用ai 在主应用里也要显示
This commit is contained in:
@@ -69,6 +69,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
/// 当前 Cell 不再是屏幕主显示页
|
||||
- (void)onResignedCurrentPersonaCell;
|
||||
|
||||
/// 刷新聊天记录(重置分页状态,从第一页重新加载)
|
||||
- (void)refreshChatHistory;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -256,7 +256,20 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
if (self.hasLoadedData || self.isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
[self loadChatHistory];
|
||||
}
|
||||
|
||||
- (void)refreshChatHistory {
|
||||
// 重置分页状态
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
self.hasLoadedData = NO;
|
||||
self.isLoading = NO;
|
||||
|
||||
// 清空当前消息并重新加载
|
||||
[self.messages removeAllObjects];
|
||||
[self.chatView clearMessages];
|
||||
[self loadChatHistory];
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,19 @@
|
||||
|
||||
static NSString * const KBAISelectedPersonaIdKey = @"KBAISelectedPersonaId";
|
||||
|
||||
#pragma mark - Darwin Notification Callback (键盘扩展聊天更新)
|
||||
|
||||
static void KBChatUpdatedDarwinCallback(CFNotificationCenterRef center,
|
||||
void *observer,
|
||||
CFStringRef name,
|
||||
const void *object,
|
||||
CFDictionaryRef userInfo) {
|
||||
KBAIHomeVC *self = (__bridge KBAIHomeVC *)observer;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self kb_handleChatUpdatedFromExtension];
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - Keyboard Gate
|
||||
|
||||
/// 查找当前 view 树里的 firstResponder
|
||||
@@ -163,6 +176,15 @@ static NSString * const KBAISelectedPersonaIdKey = @"KBAISelectedPersonaId";
|
||||
[self setupKeyboardNotifications];
|
||||
[self setupKeyboardDismissGesture];
|
||||
[self loadPersonas];
|
||||
|
||||
// 监听键盘扩展聊天更新的 Darwin 跨进程通知
|
||||
CFNotificationCenterAddObserver(
|
||||
CFNotificationCenterGetDarwinNotifyCenter(),
|
||||
(__bridge const void *)(self),
|
||||
KBChatUpdatedDarwinCallback,
|
||||
(__bridge CFStringRef)kKBDarwinChatUpdated,
|
||||
NULL,
|
||||
CFNotificationSuspensionBehaviorDeliverImmediately);
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated {
|
||||
@@ -763,6 +785,36 @@ static NSString * const KBAISelectedPersonaIdKey = @"KBAISelectedPersonaId";
|
||||
return nil;
|
||||
}
|
||||
|
||||
#pragma mark - 键盘扩展聊天更新处理
|
||||
|
||||
/// 收到键盘扩展的聊天更新通知后,刷新对应 persona 的聊天记录
|
||||
- (void)kb_handleChatUpdatedFromExtension {
|
||||
NSUserDefaults *ud = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
||||
NSInteger companionId = [ud integerForKey:AppGroup_ChatUpdatedCompanionId];
|
||||
if (companionId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSLog(@"[KBAIHomeVC] 收到键盘扩展聊天更新通知,companionId=%ld", (long)companionId);
|
||||
|
||||
// 查找对应 persona 的索引
|
||||
NSInteger index = [self indexOfPersonaId:companionId];
|
||||
if (index == NSNotFound) {
|
||||
NSLog(@"[KBAIHomeVC] 未找到 companionId=%ld 对应的 persona", (long)companionId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取对应的 cell 并刷新聊天记录
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
|
||||
KBPersonaChatCell *cell = (KBPersonaChatCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
|
||||
if (cell) {
|
||||
[cell refreshChatHistory];
|
||||
NSLog(@"[KBAIHomeVC] 已触发 companionId=%ld 的聊天记录刷新", (long)companionId);
|
||||
} else {
|
||||
NSLog(@"[KBAIHomeVC] companionId=%ld 的 cell 不可见,下次显示时会自动加载", (long)companionId);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSInteger)indexOfPersonaId:(NSInteger)personaId {
|
||||
if (personaId <= 0) {
|
||||
return NSNotFound;
|
||||
@@ -1360,6 +1412,11 @@ static NSString * const KBAISelectedPersonaIdKey = @"KBAISelectedPersonaId";
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
CFNotificationCenterRemoveObserver(
|
||||
CFNotificationCenterGetDarwinNotifyCenter(),
|
||||
(__bridge const void *)(self),
|
||||
(__bridge CFStringRef)kKBDarwinChatUpdated,
|
||||
NULL);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user