跨进程 键盘用ai 在主应用里也要显示

This commit is contained in:
2026-02-26 21:47:22 +08:00
parent 69bd2b2af9
commit a711be4c4d
6 changed files with 104 additions and 2 deletions

View File

@@ -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