2026-01-15 18:16:56 +08:00
|
|
|
//
|
|
|
|
|
// KBChatPanelView.m
|
|
|
|
|
// CustomKeyboard
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "KBChatPanelView.h"
|
|
|
|
|
#import "KBChatMessage.h"
|
|
|
|
|
#import "KBChatMessageCell.h"
|
|
|
|
|
#import "Masonry.h"
|
|
|
|
|
|
|
|
|
|
@interface KBChatPanelView () <UITableViewDataSource, UITableViewDelegate>
|
2026-01-15 18:49:31 +08:00
|
|
|
//@property (nonatomic, strong) UIImageView *backgroundImageView;
|
|
|
|
|
@property (nonatomic, strong) UIView *headerView;
|
|
|
|
|
@property (nonatomic, strong) UILabel *titleLabel;
|
|
|
|
|
@property (nonatomic, strong) UIButton *closeButton;
|
2026-01-15 18:16:56 +08:00
|
|
|
@property (nonatomic, strong) UITableView *tableViewInternal;
|
|
|
|
|
@property (nonatomic, copy) NSArray<KBChatMessage *> *messages;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation KBChatPanelView
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
2026-01-15 18:49:31 +08:00
|
|
|
self.backgroundColor = [UIColor clearColor];
|
2026-01-15 18:16:56 +08:00
|
|
|
|
2026-01-15 18:49:31 +08:00
|
|
|
// [self addSubview:self.backgroundImageView];
|
|
|
|
|
[self addSubview:self.headerView];
|
2026-01-15 18:16:56 +08:00
|
|
|
[self addSubview:self.tableViewInternal];
|
|
|
|
|
|
2026-01-15 18:49:31 +08:00
|
|
|
// [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
// make.edges.equalTo(self);
|
|
|
|
|
// }];
|
|
|
|
|
|
|
|
|
|
[self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.left.right.equalTo(self);
|
|
|
|
|
make.top.equalTo(self.mas_top);
|
|
|
|
|
make.height.mas_equalTo(KBFit(36.0f));
|
|
|
|
|
}];
|
|
|
|
|
|
2026-01-15 18:16:56 +08:00
|
|
|
[self.tableViewInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.left.right.equalTo(self);
|
2026-01-15 18:49:31 +08:00
|
|
|
make.top.equalTo(self.headerView.mas_bottom).offset(4);
|
2026-01-15 18:16:56 +08:00
|
|
|
make.bottom.equalTo(self.mas_bottom).offset(-8);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Public
|
|
|
|
|
|
|
|
|
|
- (void)kb_reloadWithMessages:(NSArray<KBChatMessage *> *)messages {
|
|
|
|
|
self.messages = messages ?: @[];
|
|
|
|
|
[self.tableViewInternal reloadData];
|
|
|
|
|
if (self.messages.count > 0) {
|
|
|
|
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
|
|
|
|
|
[self.tableViewInternal scrollToRowAtIndexPath:indexPath
|
|
|
|
|
atScrollPosition:UITableViewScrollPositionBottom
|
|
|
|
|
animated:YES];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 18:49:31 +08:00
|
|
|
//- (void)kb_setBackgroundImage:(UIImage *)image {
|
|
|
|
|
// self.backgroundImageView.image = image;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
|
|
|
|
|
- (void)kb_onTapClose {
|
|
|
|
|
if ([self.delegate respondsToSelector:@selector(chatPanelViewDidTapClose:)]) {
|
|
|
|
|
[self.delegate chatPanelViewDidTapClose:self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 18:16:56 +08:00
|
|
|
#pragma mark - UITableViewDataSource
|
|
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
|
return self.messages.count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
KBChatMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(KBChatMessageCell.class)];
|
|
|
|
|
KBChatMessage *msg = self.messages[indexPath.row];
|
|
|
|
|
[cell kb_configureWithMessage:msg];
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
return UITableViewAutomaticDimension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
return 44.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
if (indexPath.row >= self.messages.count) { return; }
|
|
|
|
|
KBChatMessage *msg = self.messages[indexPath.row];
|
|
|
|
|
if ([self.delegate respondsToSelector:@selector(chatPanelView:didTapMessage:)]) {
|
|
|
|
|
[self.delegate chatPanelView:self didTapMessage:msg];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Lazy
|
|
|
|
|
|
|
|
|
|
- (UITableView *)tableViewInternal {
|
|
|
|
|
if (!_tableViewInternal) {
|
|
|
|
|
_tableViewInternal = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
|
|
|
|
_tableViewInternal.backgroundColor = [UIColor clearColor];
|
2026-01-15 18:49:31 +08:00
|
|
|
_tableViewInternal.backgroundView = nil;
|
2026-01-15 18:16:56 +08:00
|
|
|
_tableViewInternal.separatorStyle = UITableViewCellSeparatorStyleNone;
|
|
|
|
|
_tableViewInternal.dataSource = self;
|
|
|
|
|
_tableViewInternal.delegate = self;
|
|
|
|
|
_tableViewInternal.estimatedRowHeight = 44.0;
|
|
|
|
|
_tableViewInternal.rowHeight = UITableViewAutomaticDimension;
|
|
|
|
|
[_tableViewInternal registerClass:KBChatMessageCell.class forCellReuseIdentifier:NSStringFromClass(KBChatMessageCell.class)];
|
|
|
|
|
}
|
|
|
|
|
return _tableViewInternal;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 18:49:31 +08:00
|
|
|
- (UIView *)headerView {
|
|
|
|
|
if (!_headerView) {
|
|
|
|
|
_headerView = [[UIView alloc] init];
|
|
|
|
|
_headerView.backgroundColor = [UIColor clearColor];
|
|
|
|
|
[_headerView addSubview:self.titleLabel];
|
|
|
|
|
[_headerView addSubview:self.closeButton];
|
|
|
|
|
|
|
|
|
|
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.left.equalTo(_headerView.mas_left).offset(12);
|
|
|
|
|
make.centerY.equalTo(_headerView);
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.right.equalTo(_headerView.mas_right).offset(-12);
|
|
|
|
|
make.centerY.equalTo(_headerView);
|
|
|
|
|
make.width.height.mas_equalTo(KBFit(24.0f));
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
return _headerView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (UILabel *)titleLabel {
|
|
|
|
|
if (!_titleLabel) {
|
|
|
|
|
_titleLabel = [[UILabel alloc] init];
|
|
|
|
|
_titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
|
2026-01-15 19:14:34 +08:00
|
|
|
_titleLabel.textColor =
|
|
|
|
|
[UIColor kb_dynamicColorWithLightColor:[UIColor colorWithHex:0x1B1F1A]
|
|
|
|
|
darkColor:[UIColor whiteColor]];
|
2026-01-15 18:49:31 +08:00
|
|
|
_titleLabel.text = KBLocalized(@"AI对话");
|
|
|
|
|
}
|
|
|
|
|
return _titleLabel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (UIButton *)closeButton {
|
|
|
|
|
if (!_closeButton) {
|
|
|
|
|
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
|
UIImage *icon = [UIImage imageNamed:@"close_icon"];
|
|
|
|
|
[_closeButton setImage:icon forState:UIControlStateNormal];
|
|
|
|
|
_closeButton.backgroundColor = [UIColor clearColor];
|
|
|
|
|
[_closeButton addTarget:self
|
|
|
|
|
action:@selector(kb_onTapClose)
|
|
|
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
|
}
|
|
|
|
|
return _closeButton;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//- (UIImageView *)backgroundImageView {
|
|
|
|
|
// if (!_backgroundImageView) {
|
|
|
|
|
// _backgroundImageView = [[UIImageView alloc] init];
|
|
|
|
|
// _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
|
|
|
// _backgroundImageView.clipsToBounds = YES;
|
|
|
|
|
// _backgroundImageView.backgroundColor = [UIColor clearColor];
|
|
|
|
|
// _backgroundImageView.userInteractionEnabled = NO;
|
|
|
|
|
// }
|
|
|
|
|
// return _backgroundImageView;
|
|
|
|
|
//}
|
|
|
|
|
|
2026-01-15 18:16:56 +08:00
|
|
|
#pragma mark - Expose
|
|
|
|
|
|
|
|
|
|
- (UITableView *)tableView { return self.tableViewInternal; }
|
|
|
|
|
|
|
|
|
|
@end
|