This commit is contained in:
2026-01-23 21:51:37 +08:00
parent 6ad9783bcb
commit 77fd46aa34
26 changed files with 3681 additions and 199 deletions

View File

@@ -0,0 +1,339 @@
//
// KBChatAssistantMessageCell.m
// keyBoard
//
// Created by Kiro on 2026/1/23.
//
#import "KBChatAssistantMessageCell.h"
#import "KBAiChatMessage.h"
#import <Masonry/Masonry.h>
@interface KBChatAssistantMessageCell ()
@property (nonatomic, strong) UIButton *voiceButton;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UIView *bubbleView;
@property (nonatomic, strong, readwrite) UILabel *messageLabel; // readwrite
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) KBAiChatMessage *currentMessage;
//
@property (nonatomic, strong) NSTimer *typewriterTimer;
@property (nonatomic, copy) NSString *fullText;
@property (nonatomic, assign) NSInteger currentCharIndex;
@end
@implementation KBChatAssistantMessageCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setupUI];
}
return self;
}
- (void)setupUI {
//
self.voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.voiceButton setImage:[UIImage systemImageNamed:@"play.circle.fill"]
forState:UIControlStateNormal];
self.voiceButton.tintColor = [UIColor whiteColor];
[self.voiceButton addTarget:self
action:@selector(voiceButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.voiceButton];
//
self.durationLabel = [[UILabel alloc] init];
self.durationLabel.font = [UIFont systemFontOfSize:14];
self.durationLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:self.durationLabel];
//
self.loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
self.loadingIndicator.color = [UIColor whiteColor];
self.loadingIndicator.hidesWhenStopped = YES;
[self.contentView addSubview:self.loadingIndicator];
//
self.bubbleView = [[UIView alloc] init];
self.bubbleView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.7];
self.bubbleView.layer.cornerRadius = 16;
self.bubbleView.layer.masksToBounds = YES;
[self.contentView addSubview:self.bubbleView];
//
self.messageLabel = [[UILabel alloc] init];
self.messageLabel.numberOfLines = 0;
self.messageLabel.font = [UIFont systemFontOfSize:16];
self.messageLabel.textColor = [UIColor whiteColor];
// preferredMaxLayoutWidth AutoLayout
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.75 - 16 - 24;
self.messageLabel.preferredMaxLayoutWidth = maxWidth;
[self.bubbleView addSubview:self.messageLabel];
//
[self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(16);
make.top.equalTo(self.contentView).offset(8);
make.width.height.mas_equalTo(24);
}];
[self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.voiceButton.mas_right).offset(4);
make.centerY.equalTo(self.voiceButton);
}];
[self.loadingIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.voiceButton);
}];
// bubbleView
[self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.voiceButton.mas_bottom).offset(4);
make.bottom.equalTo(self.contentView).offset(-4);
make.left.equalTo(self.contentView).offset(16);
make.width.lessThanOrEqualTo(self.contentView).multipliedBy(0.75);
}];
// messageLabel
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bubbleView).offset(10);
make.bottom.equalTo(self.bubbleView).offset(-10);
make.left.equalTo(self.bubbleView).offset(12);
make.right.equalTo(self.bubbleView).offset(-12);
}];
}
- (void)configureWithMessage:(KBAiChatMessage *)message {
NSLog(@"[KBChatAssistantMessageCell] 配置消息 - 文本长度: %lu, isComplete: %d, needsTypewriter: %d, 打字机运行中: %d",
(unsigned long)message.text.length, message.isComplete, message.needsTypewriterEffect,
(self.typewriterTimer && self.typewriterTimer.isValid));
//
[self stopTypewriterEffect];
self.currentMessage = message;
// 使
if (message.needsTypewriterEffect && !message.isComplete && message.text.length > 0) {
//
NSLog(@"[KBChatAssistantMessageCell] 启动新的打字机效果");
[self startTypewriterEffectWithText:message.text];
} else {
//
NSLog(@"[KBChatAssistantMessageCell] 直接显示完整文本needsTypewriter: %d, isComplete: %d",
message.needsTypewriterEffect, message.isComplete);
self.messageLabel.text = message.text;
}
// 0
if (message.audioDuration > 0) {
NSInteger seconds = (NSInteger)ceil(message.audioDuration);
self.durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)seconds];
} else {
self.durationLabel.text = @"";
}
// audioId audioData
BOOL hasAudio = (message.audioId.length > 0) ||
(message.audioData != nil && message.audioData.length > 0);
self.voiceButton.hidden = !hasAudio;
self.durationLabel.hidden = !hasAudio;
}
- (void)updateVoicePlayingState:(BOOL)isPlaying {
if (isPlaying) {
[self.voiceButton setImage:[UIImage systemImageNamed:@"pause.circle.fill"]
forState:UIControlStateNormal];
} else {
[self.voiceButton setImage:[UIImage systemImageNamed:@"play.circle.fill"]
forState:UIControlStateNormal];
}
}
- (void)showLoadingAnimation {
//
[self.voiceButton setImage:nil forState:UIControlStateNormal];
[self.loadingIndicator startAnimating];
}
- (void)hideLoadingAnimation {
//
[self.loadingIndicator stopAnimating];
[self.voiceButton setImage:[UIImage systemImageNamed:@"play.circle.fill"]
forState:UIControlStateNormal];
}
- (void)voiceButtonTapped {
if ([self.delegate respondsToSelector:@selector(assistantMessageCell:didTapVoiceButtonForMessage:)]) {
[self.delegate assistantMessageCell:self didTapVoiceButtonForMessage:self.currentMessage];
}
}
#pragma mark - Typewriter Effect
- (void)startTypewriterEffectWithText:(NSString *)text {
if (text.length == 0) {
NSLog(@"[KBChatAssistantMessageCell] 文本为空,跳过打字机效果");
return;
}
NSLog(@"[KBChatAssistantMessageCell] 开始打字机效果,文本长度: %lu, 文本: %@",
(unsigned long)text.length, text);
self.fullText = text;
self.currentCharIndex = 0;
//
self.messageLabel.text = text;
//
[self.messageLabel setNeedsLayout];
[self.bubbleView setNeedsLayout];
[self.contentView setNeedsLayout];
[self layoutIfNeeded];
NSLog(@"[KBChatAssistantMessageCell] 布局后 Label frame: %@", NSStringFromCGRect(self.messageLabel.frame));
// 使 NSAttributedString
//
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
// 线
dispatch_async(dispatch_get_main_queue(), ^{
// 0.03
self.typewriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(typewriterTick)
userInfo:nil
repeats:YES];
// RunLoop common
[[NSRunLoop currentRunLoop] addTimer:self.typewriterTimer forMode:NSRunLoopCommonModes];
//
[self typewriterTick];
NSLog(@"[KBChatAssistantMessageCell] 定时器已创建: %@", self.typewriterTimer);
});
}
- (void)typewriterTick {
// 使 fullText线
NSString *text = self.fullText;
// fullText
if (!text || text.length == 0) {
NSLog(@"[KBChatAssistantMessageCell] fullText 无效,停止打字机");
[self stopTypewriterEffect];
return;
}
//
if (self.currentCharIndex < text.length) {
self.currentCharIndex++;
// 使 NSAttributedString N
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
//
if (self.currentCharIndex > 0) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:NSMakeRange(0, self.currentCharIndex)];
}
//
if (self.currentCharIndex < text.length) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(self.currentCharIndex, text.length - self.currentCharIndex)];
}
//
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
// 10
if (self.currentCharIndex % 10 == 0) {
NSString *displayText = [text substringToIndex:self.currentCharIndex];
NSLog(@"[KBChatAssistantMessageCell] 打字机进度: %ld/%lu, 当前文本: %@",
(long)self.currentCharIndex, (unsigned long)text.length, displayText);
NSLog(@"[KBChatAssistantMessageCell] Label 状态 - frame: %@, hidden: %d, alpha: %.2f",
NSStringFromCGRect(self.messageLabel.frame),
self.messageLabel.hidden,
self.messageLabel.alpha);
}
} else {
//
NSLog(@"[KBChatAssistantMessageCell] 打字机效果完成,停止定时器");
// text
if (!text || text.length == 0) {
NSLog(@"[KBChatAssistantMessageCell] text 为空,跳过");
[self stopTypewriterEffect];
return;
}
[self stopTypewriterEffect];
// 使 attributedText
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
// Cell
if (self.currentMessage) {
self.currentMessage.isComplete = YES;
self.currentMessage.needsTypewriterEffect = NO;
NSLog(@"[KBChatAssistantMessageCell] 标记消息完成 - 文本: %@", self.currentMessage.text);
}
}
}
- (void)stopTypewriterEffect {
if (self.typewriterTimer && self.typewriterTimer.isValid) {
NSLog(@"[KBChatAssistantMessageCell] 停止打字机定时器");
[self.typewriterTimer invalidate];
}
self.typewriterTimer = nil;
self.currentCharIndex = 0;
self.fullText = nil;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self stopTypewriterEffect];
//
self.messageLabel.text = @"";
self.messageLabel.attributedText = nil;
}
- (void)dealloc {
[self stopTypewriterEffect];
}
@end