1
This commit is contained in:
@@ -9,8 +9,12 @@
|
||||
#import "AiVM.h"
|
||||
#import "KBChattedCompanionModel.h"
|
||||
#import "KBHUD.h"
|
||||
#import "KBAIChatMessageCacheManager.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
|
||||
/// 聊天会话被重置的通知
|
||||
static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidResetNotification";
|
||||
|
||||
@interface KBAIMessageChatingVC ()
|
||||
|
||||
@property (nonatomic, strong) AiVM *viewModel;
|
||||
@@ -54,10 +58,11 @@
|
||||
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
|
||||
|
||||
if (indexPath) {
|
||||
self.longPressIndexPath = indexPath;
|
||||
|
||||
// 在手指位置显示删除按钮
|
||||
[self showDeleteButtonAtPoint:point];
|
||||
|
||||
// 在 showDeleteButtonAtPoint 之后再设置,避免被 hideDeleteButton 清空
|
||||
self.longPressIndexPath = indexPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,12 +70,14 @@
|
||||
- (void)handleTapGesture:(UITapGestureRecognizer *)gesture {
|
||||
// 点击其他地方隐藏删除按钮
|
||||
if (self.deleteButton && !self.deleteButton.hidden) {
|
||||
CGPoint point = [gesture locationInView:self.view];
|
||||
CGPoint buttonPoint = [gesture locationInView:self.deleteButton];
|
||||
CGPoint pointInButton = [gesture locationInView:self.deleteButton];
|
||||
|
||||
// 如果点击的不是删除按钮,则隐藏
|
||||
if (!CGRectContainsPoint(self.deleteButton.bounds, buttonPoint)) {
|
||||
if (!CGRectContainsPoint(self.deleteButton.bounds, pointInButton)) {
|
||||
NSLog(@"[KBAIMessageChatingVC] 点击了删除按钮外部,隐藏按钮");
|
||||
[self hideDeleteButton];
|
||||
} else {
|
||||
NSLog(@"[KBAIMessageChatingVC] 点击了删除按钮内部,不隐藏");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,13 +162,75 @@
|
||||
}
|
||||
|
||||
- (void)deleteButtonTapped {
|
||||
if (self.longPressIndexPath) {
|
||||
// 隐藏按钮
|
||||
[self hideDeleteButton];
|
||||
|
||||
// 执行删除
|
||||
[self deleteItemAtIndexPath:self.longPressIndexPath];
|
||||
if (!self.longPressIndexPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存 indexPath,因为后面会清空
|
||||
NSIndexPath *indexPath = self.longPressIndexPath;
|
||||
|
||||
// 获取要删除的数据
|
||||
if (indexPath.row >= self.chattedList.count) {
|
||||
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=%@",
|
||||
(long)companionId, model.name);
|
||||
|
||||
// 隐藏删除按钮
|
||||
[self hideDeleteButton];
|
||||
|
||||
// 显示加载提示
|
||||
[KBHUD show];
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
|
||||
// 调用清空聊天会话的 API
|
||||
[self.viewModel resetChatSessionWithCompanionId:companionId
|
||||
completion:^(KBChatSessionResetResponse * _Nullable response, NSError * _Nullable error) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[KBHUD dismiss];
|
||||
|
||||
if (error) {
|
||||
NSLog(@"[KBAIMessageChatingVC] 删除失败:%@", error.localizedDescription);
|
||||
[KBHUD showError:@"删除失败,请重试"];
|
||||
return;
|
||||
}
|
||||
|
||||
NSLog(@"[KBAIMessageChatingVC] ✅ API 调用成功,开始清理本地数据");
|
||||
|
||||
// 1. 删除本地列表数据
|
||||
if (indexPath.row < weakSelf.chattedList.count) {
|
||||
[weakSelf.chattedList removeObjectAtIndex:indexPath.row];
|
||||
}
|
||||
if (indexPath.row < weakSelf.dataArray.count) {
|
||||
[weakSelf.dataArray removeObjectAtIndex:indexPath.row];
|
||||
}
|
||||
|
||||
// 2. 更新 TableView(带动画)
|
||||
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath]
|
||||
withRowAnimation:UITableViewRowAnimationLeft];
|
||||
|
||||
// 3. ✅ 清除缓存管理器中的聊天记录(关键!)
|
||||
[[KBAIChatMessageCacheManager shared] clearMessagesForCompanionId:companionId];
|
||||
NSLog(@"[KBAIMessageChatingVC] ✅ 已清除缓存:companionId=%ld", (long)companionId);
|
||||
|
||||
// 4. 发送通知,通知其他页面(主页)刷新
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:KBChatSessionDidResetNotification
|
||||
object:nil
|
||||
userInfo:@{@"companionId": @(companionId)}];
|
||||
NSLog(@"[KBAIMessageChatingVC] ✅ 已发送重置通知:companionId=%ld", (long)companionId);
|
||||
|
||||
// 5. 显示成功提示
|
||||
[KBHUD showSuccess:@"已删除"];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - 2:数据加载
|
||||
@@ -200,24 +269,6 @@
|
||||
}];
|
||||
}
|
||||
|
||||
#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 {
|
||||
|
||||
Reference in New Issue
Block a user