From 62f3ddae4a41a225fce674fd2023b757cb49756e Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Wed, 12 Nov 2025 15:40:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B5=8B=E8=AF=95=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CustomKeyboard/View/KBFunctionView.m | 65 +++------------------------- 1 file changed, 7 insertions(+), 58 deletions(-) diff --git a/CustomKeyboard/View/KBFunctionView.m b/CustomKeyboard/View/KBFunctionView.m index 98ee941..eea7ed9 100644 --- a/CustomKeyboard/View/KBFunctionView.m +++ b/CustomKeyboard/View/KBFunctionView.m @@ -85,7 +85,6 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; - (void)dealloc { [self stopPasteboardMonitor]; [self kb_stopNetworkStreaming]; - [self kb_stopMockStreaming]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @@ -241,13 +240,12 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; self.streamDeleteButton = del; // 优先拉取后端测试数据(GET);失败则回落到本地演示 - [self kb_startNetworkStreamingWithSeed:title fallbackToMock:YES]; + [self kb_startNetworkStreamingWithSeed:title]; } - (void)kb_onTapStreamDelete { // 关闭并销毁流式视图,恢复标签列表 [self kb_stopNetworkStreaming]; - [self kb_stopMockStreaming]; [self.streamDeleteButton removeFromSuperview]; self.streamDeleteButton = nil; [self.streamTextView removeFromSuperview]; @@ -255,67 +253,18 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; self.collectionViewInternal.hidden = NO; } -#pragma mark - Mock Streaming - -// 生成并按时间片推送模拟流数据到 KBStreamTextView -- (void)kb_startMockStreamingWithSeed:(NSString *)seedTitle { - [self kb_stopMockStreaming]; - - // 连续文本源(包含 \t 作为“分段”分隔符;\n 为换行),按 1~3 字随机切片推送 - NSString *t = seedTitle ?: @"高情商"; - self.streamMockSource = [NSString stringWithFormat: - @"选择了标签:%@\n" - "下面演示流式输出,每隔一小段时间来一截。第一段结束。\t" - "第二段开始:这是一个较长的句子,会被拆成多个小块推送,直到遇到分隔符就换段。\n第二段结束。\t" - "最后一段:右上角可随时关闭展示。\n演示完成。\t", - t]; - self.streamMockCursor = 0; - self.streamMockChunks = nil; // 废弃数组方式 - self.streamMockIndex = 0; - - // 定时喂数据(主线程):每次随机 1~3 个字 - __weak typeof(self) weakSelf = self; - self.streamMockTimer = [NSTimer scheduledTimerWithTimeInterval:0.20 repeats:YES block:^(NSTimer * _Nonnull timer) { - __strong typeof(weakSelf) self = weakSelf; if (!self) { [timer invalidate]; return; } - if (!self.streamTextView) { [timer invalidate]; return; } - NSInteger total = (NSInteger)self.streamMockSource.length; - if (self.streamMockCursor >= total) { - // 结束:收尾裁剪当前段 - [self.streamTextView finishStreaming]; - [timer invalidate]; - self.streamMockTimer = nil; - return; - } - // 随机 1~3 个字(不跨越结尾),逐段追加 - uint32_t step = arc4random_uniform(3) + 1; // 1..3 - NSInteger remain = total - self.streamMockCursor; - NSInteger take = MIN((NSInteger)step, remain); - NSString *piece = [self.streamMockSource substringWithRange:NSMakeRange(self.streamMockCursor, take)]; - self.streamMockCursor += take; - [self.streamTextView appendStreamText:piece ?: @""]; - }]; -} - -- (void)kb_stopMockStreaming { - [self.streamMockTimer invalidate]; - self.streamMockTimer = nil; - self.streamMockChunks = nil; - self.streamMockIndex = 0; - self.streamMockSource = nil; - self.streamMockCursor = 0; -} #pragma mark - Network Streaming (GET) // 后端测试地址(内网)。若被 ATS 拦截,请在扩展 target 的 Info.plist 中允许 HTTP 或添加例外域。 static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/talk"; -- (void)kb_startNetworkStreamingWithSeed:(NSString *)seedTitle fallbackToMock:(BOOL)fallback { +- (void)kb_startNetworkStreamingWithSeed:(NSString *)seedTitle { [self kb_stopNetworkStreaming]; - if (![[KBFullAccessManager shared] hasFullAccess]) { if (fallback) /*[self kb_startMockStreamingWithSeed:seedTitle]*/; return; } + if (![[KBFullAccessManager shared] hasFullAccess]) { return; } NSURL *url = [NSURL URLWithString:kKBStreamDemoURL]; - if (!url) { if (fallback) [self kb_startMockStreamingWithSeed:seedTitle]; return; } + if (!url) { return; } self.streamHasOutput = NO; // 重置首段处理标记 __weak typeof(self) weakSelf = self; @@ -333,9 +282,9 @@ static NSString * const kKBStreamDemoURL = @"http://192.168.1.144:7529/api/demo/ }; fetcher.onFinish = ^(NSError * _Nullable error) { __strong typeof(weakSelf) self = weakSelf; if (!self) return; - if (error && fallback && !self.streamHasOutput) { - [KBHUD showInfo:@"拉取失败,使用本地演示"]; // 降级 - [self kb_startMockStreamingWithSeed:nil]; + if (error) { + [KBHUD showInfo:@"拉取失败"]; + [self.streamTextView finishStreaming]; } else { [self.streamTextView finishStreaming]; }