1
This commit is contained in:
@@ -66,6 +66,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary<NSString *, KBAiChatMessage *> *pendingAssistantMessages;
|
||||
|
||||
@property (nonatomic, assign) BOOL isCurrentPersonaCell;
|
||||
@property (nonatomic, assign) BOOL shouldAutoPlayPrologueAudio;
|
||||
@property (nonatomic, assign) BOOL hasPlayedPrologueAudio;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBPersonaChatCell
|
||||
@@ -100,6 +104,9 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.isLoading = NO;
|
||||
self.canTriggerLoadMore = YES;
|
||||
[self.pendingAssistantMessages removeAllObjects];
|
||||
self.isCurrentPersonaCell = NO;
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
self.hasPlayedPrologueAudio = NO;
|
||||
|
||||
// ✅ 移除了 self.hasLoadedData = NO;
|
||||
// 这样 Cell 复用时不会重复请求数据
|
||||
@@ -191,6 +198,9 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
[self.pendingAssistantMessages removeAllObjects];
|
||||
self.isCurrentPersonaCell = NO;
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
self.hasPlayedPrologueAudio = NO;
|
||||
|
||||
// ⚠️ 临时禁用缓存,排查问题
|
||||
// NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
|
||||
@@ -291,8 +301,17 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
|
||||
strongSelf.hasLoadedData = YES;
|
||||
strongSelf.hasMoreHistory = pageModel.hasMore;
|
||||
|
||||
|
||||
NSInteger loadedPage = strongSelf.currentPage;
|
||||
if (loadedPage == 1) {
|
||||
BOOL isEmpty = (pageModel.total == 0);
|
||||
strongSelf.shouldAutoPlayPrologueAudio = isEmpty && (strongSelf.persona.prologueAudio.length > 0);
|
||||
if (!strongSelf.shouldAutoPlayPrologueAudio) {
|
||||
[strongSelf.chatView stopPlayingAudio];
|
||||
} else {
|
||||
[strongSelf tryPlayPrologueAudioIfNeeded];
|
||||
}
|
||||
}
|
||||
if (loadedPage == 1 && pageModel.total == 0) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[strongSelf.chatView clearMessages];
|
||||
@@ -391,6 +410,35 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Prologue Audio
|
||||
|
||||
- (void)tryPlayPrologueAudioIfNeeded {
|
||||
if (!self.isCurrentPersonaCell) {
|
||||
return;
|
||||
}
|
||||
if (!self.shouldAutoPlayPrologueAudio) {
|
||||
return;
|
||||
}
|
||||
if (self.hasPlayedPrologueAudio) {
|
||||
return;
|
||||
}
|
||||
if (self.persona.prologueAudio.length == 0) {
|
||||
return;
|
||||
}
|
||||
self.hasPlayedPrologueAudio = YES;
|
||||
[self.chatView playRemoteAudioWithURLString:self.persona.prologueAudio];
|
||||
}
|
||||
|
||||
- (void)onBecameCurrentPersonaCell {
|
||||
self.isCurrentPersonaCell = YES;
|
||||
[self tryPlayPrologueAudioIfNeeded];
|
||||
}
|
||||
|
||||
- (void)onResignedCurrentPersonaCell {
|
||||
self.isCurrentPersonaCell = NO;
|
||||
[self.chatView stopPlayingAudio];
|
||||
}
|
||||
|
||||
- (void)loadMoreHistory {
|
||||
if (!self.hasMoreHistory || self.isLoading) {
|
||||
[self.chatView endLoadMoreWithHasMoreData:self.hasMoreHistory];
|
||||
@@ -523,6 +571,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
[self.chatView stopPlayingAudio];
|
||||
[self.chatView updateIntroFooterText:nil];
|
||||
[self ensureOpeningMessageAtTop];
|
||||
KBAiChatMessage *message = [KBAiChatMessage userMessageWithText:text];
|
||||
@@ -543,6 +593,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
[self.chatView stopPlayingAudio];
|
||||
[self.chatView updateIntroFooterText:nil];
|
||||
[self ensureOpeningMessageAtTop];
|
||||
KBAiChatMessage *message = [KBAiChatMessage loadingUserMessage];
|
||||
@@ -614,6 +666,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
[self.chatView updateIntroFooterText:nil];
|
||||
[self ensureOpeningMessageAtTop];
|
||||
|
||||
@@ -637,6 +690,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
[self.chatView updateIntroFooterText:nil];
|
||||
[self ensureOpeningMessageAtTop];
|
||||
KBAiChatMessage *message = [KBAiChatMessage loadingAssistantMessage];
|
||||
@@ -662,6 +716,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
self.shouldAutoPlayPrologueAudio = NO;
|
||||
[self.chatView updateIntroFooterText:nil];
|
||||
[self ensureOpeningMessageAtTop];
|
||||
KBAiChatMessage *message = [KBAiChatMessage loadingAssistantMessage];
|
||||
|
||||
Reference in New Issue
Block a user