// // JXHistoryViewController.m // AICity // // Created by TogetherWatch on 2025-10-20. // #import "JXHistoryViewController.h" #import "JXAPIService.h" #import "JXDrama.h" // Cell复用标识符 static NSString * const kHistoryCellIdentifier = @"HistoryCell"; @interface JXHistoryViewController () #pragma mark - UI组件 @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIRefreshControl *refreshControl; @property (nonatomic, strong) UIView *emptyView; @property (nonatomic, strong) UIBarButtonItem *clearAllButton; #pragma mark - 数据 @property (nonatomic, strong) NSMutableArray *historyList; #pragma mark - 状态 @property (nonatomic, assign) BOOL isLoading; @end @implementation JXHistoryViewController #pragma mark - 生命周期 - (instancetype)init { self = [super init]; if (self) { _historyList = [NSMutableArray array]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"播放历史"; [self setupUI]; [self loadHistory]; } #pragma mark - UI设置 - (void)setupUI { // 清空按钮 self.clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(handleClearAll)]; self.navigationItem.rightBarButtonItem = self.clearAllButton; // 列表 self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHistoryCellIdentifier]; [self.view addSubview:self.tableView]; // 下拉刷新 self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged]; [self.tableView addSubview:self.refreshControl]; // 空状态 [self setupEmptyView]; // 约束 self.tableView.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.tableView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor], [self.tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], [self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor] ]]; } - (void)setupEmptyView { self.emptyView = [[UIView alloc] init]; self.emptyView.hidden = YES; [self.view addSubview:self.emptyView]; UILabel *emptyLabel = [[UILabel alloc] init]; emptyLabel.text = @"暂无播放历史\n快去看短剧吧~"; emptyLabel.textColor = [UIColor grayColor]; emptyLabel.font = [UIFont systemFontOfSize:14]; emptyLabel.textAlignment = NSTextAlignmentCenter; emptyLabel.numberOfLines = 0; [self.emptyView addSubview:emptyLabel]; self.emptyView.translatesAutoresizingMaskIntoConstraints = NO; emptyLabel.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.emptyView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], [self.emptyView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor], [emptyLabel.centerXAnchor constraintEqualToAnchor:self.emptyView.centerXAnchor], [emptyLabel.centerYAnchor constraintEqualToAnchor:self.emptyView.centerYAnchor] ]]; } #pragma mark - 数据加载 - (void)loadHistory { if (self.isLoading) { return; } self.isLoading = YES; // TODO: 调用API获取播放历史 // 暂时使用示例数据 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.isLoading = NO; [self.refreshControl endRefreshing]; // 示例数据 self.historyList = [@[] mutableCopy]; [self.tableView reloadData]; self.emptyView.hidden = (self.historyList.count > 0); self.clearAllButton.enabled = (self.historyList.count > 0); }); } - (void)handleRefresh { [self loadHistory]; } - (void)handleClearAll { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清空历史" message:@"确定要清空全部播放历史吗?" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { [self clearAllHistory]; }]]; [self presentViewController:alert animated:YES completion:nil]; } - (void)clearAllHistory { // TODO: 调用API清空历史 [self.historyList removeAllObjects]; [self.tableView reloadData]; self.emptyView.hidden = NO; self.clearAllButton.enabled = NO; } - (void)deleteHistoryAtIndex:(NSInteger)index { if (index < 0 || index >= self.historyList.count) { return; } // TODO: 调用API删除单条历史 [self.historyList removeObjectAtIndex:index]; [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; self.emptyView.hidden = (self.historyList.count > 0); self.clearAllButton.enabled = (self.historyList.count > 0); } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.historyList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHistoryCellIdentifier forIndexPath:indexPath]; JXDrama *drama = self.historyList[indexPath.row]; cell.textLabel.text = drama.title; cell.detailTextLabel.text = @"观看至第X集"; // TODO: 显示播放进度 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; JXDrama *drama = self.historyList[indexPath.row]; NSLog(@"[JXHistory] 继续播放: %@", drama.title); // TODO: 跳转到播放页,从上次播放位置继续 } - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction *action, UIView *sourceView, void (^completionHandler)(BOOL)) { [self deleteHistoryAtIndex:indexPath.row]; completionHandler(YES); }]; UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]]; return configuration; } @end