Files
keyboard/keyBoard/Class/AiTalk/VC/KBAIMessageChatingVC.m

238 lines
8.0 KiB
Mathematica
Raw Normal View History

2026-01-28 16:35:47 +08:00
//
// KBAIMessageChatingVC.m
// keyBoard
//
// Created by Mac on 2026/1/28.
//
#import "KBAIMessageChatingVC.h"
#import "AiVM.h"
#import "KBChattedCompanionModel.h"
#import "KBHUD.h"
2026-01-28 18:15:58 +08:00
#import <Masonry/Masonry.h>
2026-01-28 16:35:47 +08:00
@interface KBAIMessageChatingVC ()
@property (nonatomic, strong) AiVM *viewModel;
@property (nonatomic, strong) NSMutableArray<KBChattedCompanionModel *> *chattedList;
2026-01-28 18:15:58 +08:00
///
@property (nonatomic, strong) UIButton *deleteButton;
/// indexPath
@property (nonatomic, strong) NSIndexPath *longPressIndexPath;
2026-01-28 16:35:47 +08:00
@end
@implementation KBAIMessageChatingVC
#pragma mark - Lifecycle
- (void)viewDidLoad {
self.listType = 1; // Chatting
[super viewDidLoad];
2026-01-28 18:15:58 +08:00
//
[self setupLongPressGesture];
//
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.cancelsTouchesInView = NO; // cell
[self.tableView addGestureRecognizer:tapGesture];
}
#pragma mark -
- (void)setupLongPressGesture {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 0.5; // 0.5
[self.tableView addGestureRecognizer:longPress];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if (indexPath) {
self.longPressIndexPath = indexPath;
//
[self showDeleteButtonAtPoint:point];
}
}
}
- (void)handleTapGesture:(UITapGestureRecognizer *)gesture {
//
if (self.deleteButton && !self.deleteButton.hidden) {
CGPoint point = [gesture locationInView:self.view];
CGPoint buttonPoint = [gesture locationInView:self.deleteButton];
//
if (!CGRectContainsPoint(self.deleteButton.bounds, buttonPoint)) {
[self hideDeleteButton];
}
}
}
- (void)showDeleteButtonAtPoint:(CGPoint)point {
//
[self hideDeleteButton];
//
if (!self.deleteButton) {
self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteButton setTitle:@"删除此记录" forState:UIControlStateNormal];
[self.deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
self.deleteButton.backgroundColor = [UIColor colorWithRed:244/255.0 green:67/255.0 blue:54/255.0 alpha:1.0]; // #F44336
self.deleteButton.layer.cornerRadius = 6;
self.deleteButton.layer.masksToBounds = YES;
[self.deleteButton addTarget:self action:@selector(deleteButtonTapped) forControlEvents:UIControlEventTouchUpInside];
//
self.deleteButton.layer.shadowColor = [UIColor blackColor].CGColor;
self.deleteButton.layer.shadowOffset = CGSizeMake(0, 2);
self.deleteButton.layer.shadowOpacity = 0.3;
self.deleteButton.layer.shadowRadius = 4;
}
//
[self.view addSubview:self.deleteButton];
//
CGSize buttonSize = CGSizeMake(110, 40);
// tableView
CGPoint pointInView = [self.tableView convertPoint:point toView:self.view];
//
CGFloat buttonX = pointInView.x - buttonSize.width / 2;
CGFloat buttonY = pointInView.y - buttonSize.height - 10; // 10px
//
CGFloat margin = 10;
if (buttonX < margin) {
buttonX = margin;
} else if (buttonX + buttonSize.width > self.view.bounds.size.width - margin) {
buttonX = self.view.bounds.size.width - buttonSize.width - margin;
}
if (buttonY < KB_NAV_TOTAL_HEIGHT + margin) {
//
buttonY = pointInView.y + 10;
}
[self.deleteButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(buttonX);
make.top.equalTo(self.view).offset(buttonY);
make.width.mas_equalTo(buttonSize.width);
make.height.mas_equalTo(buttonSize.height);
}];
self.deleteButton.hidden = NO;
//
self.deleteButton.transform = CGAffineTransformMakeScale(0.3, 0.3);
self.deleteButton.alpha = 0;
[UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.deleteButton.transform = CGAffineTransformIdentity;
self.deleteButton.alpha = 1.0;
} completion:nil];
}
- (void)hideDeleteButton {
if (self.deleteButton) {
[UIView animateWithDuration:0.15 animations:^{
self.deleteButton.alpha = 0;
self.deleteButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
self.deleteButton.hidden = YES;
[self.deleteButton removeFromSuperview];
}];
}
self.longPressIndexPath = nil;
}
- (void)deleteButtonTapped {
if (self.longPressIndexPath) {
//
[self hideDeleteButton];
//
[self deleteItemAtIndexPath:self.longPressIndexPath];
}
2026-01-28 16:35:47 +08:00
}
#pragma mark - 2
- (void)loadData {
[KBHUD show];
__weak typeof(self) weakSelf = self;
[self.viewModel fetchChattedCompanionsWithCompletion:^(NSArray<KBChattedCompanionModel *> * _Nullable list, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
[KBHUD dismiss];
if (error) {
[KBHUD showError:error.localizedDescription];
return;
}
[weakSelf.chattedList removeAllObjects];
if (list.count > 0) {
[weakSelf.chattedList addObjectsFromArray:list];
}
[weakSelf.dataArray removeAllObjects];
//
for (KBChattedCompanionModel *model in weakSelf.chattedList) {
NSMutableDictionary *item = [NSMutableDictionary dictionary];
item[@"avatar"] = model.avatarUrl ?: @"";
item[@"name"] = model.name ?: @"";
item[@"content"] = model.shortDesc ?: @"";
item[@"time"] = model.createdAt ?: @"";
item[@"isPinned"] = @NO;
item[@"companionId"] = @(model.companionId);
[weakSelf.dataArray addObject:item];
}
[weakSelf.tableView reloadData];
});
}];
}
#pragma mark -
- (void)deleteItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= self.chattedList.count) {
return;
}
// TODO:
//
if (indexPath.row < self.chattedList.count) {
[self.chattedList removeObjectAtIndex:indexPath.row];
}
if (indexPath.row < self.dataArray.count) {
[self.dataArray removeObjectAtIndex:indexPath.row];
}
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - Lazy Load
- (AiVM *)viewModel {
if (!_viewModel) {
_viewModel = [[AiVM alloc] init];
}
return _viewModel;
}
- (NSMutableArray<KBChattedCompanionModel *> *)chattedList {
if (!_chattedList) {
_chattedList = [NSMutableArray array];
}
return _chattedList;
}
@end