Files
keyboard/CustomKeyboard/View/KBChatPanelView.m
2026-01-15 18:49:31 +08:00

182 lines
6.3 KiB
Objective-C

//
// KBChatPanelView.m
// CustomKeyboard
//
#import "KBChatPanelView.h"
#import "KBChatMessage.h"
#import "KBChatMessageCell.h"
#import "Masonry.h"
@interface KBChatPanelView () <UITableViewDataSource, UITableViewDelegate>
//@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UITableView *tableViewInternal;
@property (nonatomic, copy) NSArray<KBChatMessage *> *messages;
@end
@implementation KBChatPanelView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
// [self addSubview:self.backgroundImageView];
[self addSubview:self.headerView];
[self addSubview:self.tableViewInternal];
// [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));
}];
[self.tableViewInternal mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.headerView.mas_bottom).offset(4);
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];
}
}
//- (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];
}
}
#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];
_tableViewInternal.backgroundView = nil;
_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;
}
- (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];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
_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;
//}
#pragma mark - Expose
- (UITableView *)tableView { return self.tableViewInternal; }
@end