100 lines
2.7 KiB
Objective-C
100 lines
2.7 KiB
Objective-C
//
|
||
// KBAIMessageChatingVC.m
|
||
// keyBoard
|
||
//
|
||
// Created by Mac on 2026/1/28.
|
||
//
|
||
|
||
#import "KBAIMessageChatingVC.h"
|
||
#import "AiVM.h"
|
||
#import "KBChattedCompanionModel.h"
|
||
#import "KBHUD.h"
|
||
|
||
@interface KBAIMessageChatingVC ()
|
||
|
||
@property (nonatomic, strong) AiVM *viewModel;
|
||
@property (nonatomic, strong) NSMutableArray<KBChattedCompanionModel *> *chattedList;
|
||
|
||
@end
|
||
|
||
@implementation KBAIMessageChatingVC
|
||
|
||
#pragma mark - Lifecycle
|
||
|
||
- (void)viewDidLoad {
|
||
self.listType = 1; // Chatting
|
||
[super viewDidLoad];
|
||
}
|
||
|
||
#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
|