新增删除弹窗,修改bug

This commit is contained in:
2026-02-04 16:57:19 +08:00
parent 533e23ebfe
commit 3e30f619b9
5 changed files with 349 additions and 22 deletions

View File

@@ -10,12 +10,14 @@
#import "KBChattedCompanionModel.h"
#import "KBHUD.h"
#import "KBAIChatMessageCacheManager.h"
#import "KBAIChatDeleteConfirmView.h"
#import "LSTPopView.h"
#import <Masonry/Masonry.h>
///
static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidResetNotification";
@interface KBAIMessageChatingVC ()
@interface KBAIMessageChatingVC () <KBAIChatDeleteConfirmViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) AiVM *viewModel;
@property (nonatomic, strong) NSMutableArray<KBChattedCompanionModel *> *chattedList;
@@ -24,6 +26,16 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
@property (nonatomic, strong) UIButton *deleteButton;
/// indexPath
@property (nonatomic, strong) NSIndexPath *longPressIndexPath;
///
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGesture;
///
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
/// completion
@property (nonatomic, assign) NSInteger deleteButtonGeneration;
/// indexPath使
@property (nonatomic, strong) NSIndexPath *pendingDeleteIndexPath;
///
@property (nonatomic, weak) LSTPopView *deleteConfirmPopView;
@end
@@ -39,23 +51,53 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
[self setupLongPressGesture];
//
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.cancelsTouchesInView = NO; // cell
[self.tableView addGestureRecognizer:tapGesture];
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
self.tapGesture.cancelsTouchesInView = NO; // cell
self.tapGesture.delegate = self;
if (self.longPressGesture) {
[self.tapGesture requireGestureRecognizerToFail:self.longPressGesture];
}
[self.tableView addGestureRecognizer:self.tapGesture];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// tableView tap
if (gestureRecognizer == self.tapGesture) {
if (self.deleteButton && !self.deleteButton.hidden) {
CGPoint p = [touch locationInView:self.deleteButton];
if (CGRectContainsPoint(self.deleteButton.bounds, p)) {
return NO;
}
}
}
return YES;
}
#pragma mark -
- (void)setupLongPressGesture {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 0.5; // 0.5
[self.tableView addGestureRecognizer:longPress];
self.longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
self.longPressGesture.minimumPressDuration = 0.5; // 0.5
self.longPressGesture.delegate = self;
[self.tableView addGestureRecognizer:self.longPressGesture];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
CGPoint point = [gesture locationInView:self.tableView];
NSLog(@"[KBAIMessageChatingVC] longPress state=%ld point=(%.1f, %.1f)",
(long)gesture.state, point.x, point.y);
if (gesture.state == UIGestureRecognizerStateBegan) {
CGPoint point = [gesture locationInView:self.tableView];
// tap tap
if (self.tapGesture.enabled) {
self.tapGesture.enabled = NO;
NSLog(@"[KBAIMessageChatingVC] tapGesture disabled for longPress");
}
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
NSLog(@"[KBAIMessageChatingVC] longPress began indexPath=%@",
indexPath);
if (indexPath) {
//
@@ -64,12 +106,23 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
// showDeleteButtonAtPoint hideDeleteButton
self.longPressIndexPath = indexPath;
}
} else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed) {
if (!self.tapGesture.enabled) {
self.tapGesture.enabled = YES;
NSLog(@"[KBAIMessageChatingVC] tapGesture re-enabled after longPress");
}
}
}
- (void)handleTapGesture:(UITapGestureRecognizer *)gesture {
//
if (self.deleteButton && !self.deleteButton.hidden) {
CGPoint pointInView = [gesture locationInView:self.tableView];
NSLog(@"[KBAIMessageChatingVC] tap state=%ld point=(%.1f, %.1f)",
(long)gesture.state, pointInView.x, pointInView.y);
CGPoint pointInButton = [gesture locationInView:self.deleteButton];
//
@@ -83,8 +136,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
- (void)showDeleteButtonAtPoint:(CGPoint)point {
//
[self hideDeleteButton];
// 使 completion
self.deleteButtonGeneration += 1;
NSLog(@"[KBAIMessageChatingVC] showDeleteButtonAtPoint=(%.1f, %.1f) generation=%ld",
point.x, point.y, (long)self.deleteButtonGeneration);
//
if (!self.deleteButton) {
@@ -105,7 +160,14 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
//
[self.view addSubview:self.deleteButton];
if (self.deleteButton.superview != self.view) {
[self.view addSubview:self.deleteButton];
} else {
[self.view bringSubviewToFront:self.deleteButton];
}
//
[self.deleteButton.layer removeAllAnimations];
//
CGSize buttonSize = CGSizeMake(110, 40);
@@ -138,6 +200,9 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}];
self.deleteButton.hidden = NO;
self.deleteButton.alpha = 1.0;
self.deleteButton.transform = CGAffineTransformIdentity;
NSLog(@"[KBAIMessageChatingVC] deleteButton shown");
//
self.deleteButton.transform = CGAffineTransformMakeScale(0.3, 0.3);
@@ -150,10 +215,20 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
- (void)hideDeleteButton {
if (self.deleteButton) {
NSInteger generation = self.deleteButtonGeneration;
NSLog(@"[KBAIMessageChatingVC] hideDeleteButton generation=%ld", (long)generation);
//
[self.deleteButton.layer removeAllAnimations];
[UIView animateWithDuration:0.15 animations:^{
self.deleteButton.alpha = 0;
self.deleteButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
//
if (generation != self.deleteButtonGeneration) {
NSLog(@"[KBAIMessageChatingVC] hide completion ignored (generation changed: %ld -> %ld)",
(long)generation, (long)self.deleteButtonGeneration);
return;
}
self.deleteButton.hidden = YES;
[self.deleteButton removeFromSuperview];
}];
@@ -166,26 +241,69 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
return;
}
// indexPath
NSIndexPath *indexPath = self.longPressIndexPath;
// indexPath hideDeleteButton
self.pendingDeleteIndexPath = self.longPressIndexPath;
[self hideDeleteButton];
[self showDeleteConfirmPopView];
}
#pragma mark -
- (void)showDeleteConfirmPopView {
if (self.deleteConfirmPopView) {
[self.deleteConfirmPopView dismiss];
}
CGFloat width = KB_SCREEN_WIDTH - 80;
KBAIChatDeleteConfirmView *content = [[KBAIChatDeleteConfirmView alloc] initWithFrame:CGRectMake(0, 0, width, 260)];
content.delegate = self;
LSTPopView *popView = [LSTPopView initWithCustomView:content
parentView:nil
popStyle:LSTPopStyleFade
dismissStyle:LSTDismissStyleFade];
popView.bgColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
popView.hemStyle = LSTHemStyleCenter;
popView.isClickBgDismiss = YES;
popView.isAvoidKeyboard = NO;
self.deleteConfirmPopView = popView;
[popView pop];
}
#pragma mark - KBAIChatDeleteConfirmViewDelegate
- (void)chatDeleteConfirmViewDidTapDelete:(KBAIChatDeleteConfirmView *)view {
[self.deleteConfirmPopView dismiss];
NSIndexPath *indexPath = self.pendingDeleteIndexPath;
self.pendingDeleteIndexPath = nil;
[self performDeleteAtIndexPath:indexPath];
}
- (void)chatDeleteConfirmViewDidTapCancel:(KBAIChatDeleteConfirmView *)view {
[self.deleteConfirmPopView dismiss];
self.pendingDeleteIndexPath = nil;
}
#pragma mark -
- (void)performDeleteAtIndexPath:(NSIndexPath *)indexPath {
if (!indexPath) {
return;
}
//
if (indexPath.row >= self.chattedList.count) {
NSLog(@"[KBAIMessageChatingVC] 错误索引越界row=%ld, count=%ld",
NSLog(@"[KBAIMessageChatingVC] 错误索引越界row=%ld, count=%ld",
(long)indexPath.row, (long)self.chattedList.count);
[self hideDeleteButton];
return;
}
KBChattedCompanionModel *model = self.chattedList[indexPath.row];
NSInteger companionId = model.companionId;
NSLog(@"[KBAIMessageChatingVC] 开始删除聊天记录companionId=%ld, name=%@",
NSLog(@"[KBAIMessageChatingVC] 开始删除聊天记录companionId=%ld, name=%@",
(long)companionId, model.name);
//
[self hideDeleteButton];
//
[KBHUD show];
@@ -214,7 +332,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
// 2. TableView
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath]
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
// 3.