JXHistoryViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // JXHistoryViewController.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-20.
  6. //
  7. #import "JXHistoryViewController.h"
  8. #import "JXAPIService.h"
  9. #import "JXDrama.h"
  10. // Cell复用标识符
  11. static NSString * const kHistoryCellIdentifier = @"HistoryCell";
  12. @interface JXHistoryViewController () <UITableViewDelegate, UITableViewDataSource>
  13. #pragma mark - UI组件
  14. @property (nonatomic, strong) UITableView *tableView;
  15. @property (nonatomic, strong) UIRefreshControl *refreshControl;
  16. @property (nonatomic, strong) UIView *emptyView;
  17. @property (nonatomic, strong) UIBarButtonItem *clearAllButton;
  18. #pragma mark - 数据
  19. @property (nonatomic, strong) NSMutableArray<JXDrama *> *historyList;
  20. #pragma mark - 状态
  21. @property (nonatomic, assign) BOOL isLoading;
  22. @end
  23. @implementation JXHistoryViewController
  24. #pragma mark - 生命周期
  25. - (instancetype)init {
  26. self = [super init];
  27. if (self) {
  28. _historyList = [NSMutableArray array];
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.view.backgroundColor = [UIColor whiteColor];
  35. self.title = @"播放历史";
  36. [self setupUI];
  37. [self loadHistory];
  38. }
  39. #pragma mark - UI设置
  40. - (void)setupUI {
  41. // 清空按钮
  42. self.clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"清空"
  43. style:UIBarButtonItemStylePlain
  44. target:self
  45. action:@selector(handleClearAll)];
  46. self.navigationItem.rightBarButtonItem = self.clearAllButton;
  47. // 列表
  48. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  49. self.tableView.delegate = self;
  50. self.tableView.dataSource = self;
  51. self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  52. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHistoryCellIdentifier];
  53. [self.view addSubview:self.tableView];
  54. // 下拉刷新
  55. self.refreshControl = [[UIRefreshControl alloc] init];
  56. [self.refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  57. [self.tableView addSubview:self.refreshControl];
  58. // 空状态
  59. [self setupEmptyView];
  60. // 约束
  61. self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
  62. [NSLayoutConstraint activateConstraints:@[
  63. [self.tableView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
  64. [self.tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  65. [self.tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  66. [self.tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
  67. ]];
  68. }
  69. - (void)setupEmptyView {
  70. self.emptyView = [[UIView alloc] init];
  71. self.emptyView.hidden = YES;
  72. [self.view addSubview:self.emptyView];
  73. UILabel *emptyLabel = [[UILabel alloc] init];
  74. emptyLabel.text = @"暂无播放历史\n快去看短剧吧~";
  75. emptyLabel.textColor = [UIColor grayColor];
  76. emptyLabel.font = [UIFont systemFontOfSize:14];
  77. emptyLabel.textAlignment = NSTextAlignmentCenter;
  78. emptyLabel.numberOfLines = 0;
  79. [self.emptyView addSubview:emptyLabel];
  80. self.emptyView.translatesAutoresizingMaskIntoConstraints = NO;
  81. emptyLabel.translatesAutoresizingMaskIntoConstraints = NO;
  82. [NSLayoutConstraint activateConstraints:@[
  83. [self.emptyView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  84. [self.emptyView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
  85. [emptyLabel.centerXAnchor constraintEqualToAnchor:self.emptyView.centerXAnchor],
  86. [emptyLabel.centerYAnchor constraintEqualToAnchor:self.emptyView.centerYAnchor]
  87. ]];
  88. }
  89. #pragma mark - 数据加载
  90. - (void)loadHistory {
  91. if (self.isLoading) {
  92. return;
  93. }
  94. self.isLoading = YES;
  95. // TODO: 调用API获取播放历史
  96. // 暂时使用示例数据
  97. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  98. self.isLoading = NO;
  99. [self.refreshControl endRefreshing];
  100. // 示例数据
  101. self.historyList = [@[] mutableCopy];
  102. [self.tableView reloadData];
  103. self.emptyView.hidden = (self.historyList.count > 0);
  104. self.clearAllButton.enabled = (self.historyList.count > 0);
  105. });
  106. }
  107. - (void)handleRefresh {
  108. [self loadHistory];
  109. }
  110. - (void)handleClearAll {
  111. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清空历史"
  112. message:@"确定要清空全部播放历史吗?"
  113. preferredStyle:UIAlertControllerStyleAlert];
  114. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
  115. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  116. [self clearAllHistory];
  117. }]];
  118. [self presentViewController:alert animated:YES completion:nil];
  119. }
  120. - (void)clearAllHistory {
  121. // TODO: 调用API清空历史
  122. [self.historyList removeAllObjects];
  123. [self.tableView reloadData];
  124. self.emptyView.hidden = NO;
  125. self.clearAllButton.enabled = NO;
  126. }
  127. - (void)deleteHistoryAtIndex:(NSInteger)index {
  128. if (index < 0 || index >= self.historyList.count) {
  129. return;
  130. }
  131. // TODO: 调用API删除单条历史
  132. [self.historyList removeObjectAtIndex:index];
  133. [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  134. self.emptyView.hidden = (self.historyList.count > 0);
  135. self.clearAllButton.enabled = (self.historyList.count > 0);
  136. }
  137. #pragma mark - UITableViewDataSource
  138. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  139. return self.historyList.count;
  140. }
  141. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  142. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHistoryCellIdentifier forIndexPath:indexPath];
  143. JXDrama *drama = self.historyList[indexPath.row];
  144. cell.textLabel.text = drama.title;
  145. cell.detailTextLabel.text = @"观看至第X集"; // TODO: 显示播放进度
  146. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  147. return cell;
  148. }
  149. #pragma mark - UITableViewDelegate
  150. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  151. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  152. JXDrama *drama = self.historyList[indexPath.row];
  153. NSLog(@"[JXHistory] 继续播放: %@", drama.title);
  154. // TODO: 跳转到播放页,从上次播放位置继续
  155. }
  156. - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
  157. UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
  158. title:@"删除"
  159. handler:^(UIContextualAction *action, UIView *sourceView, void (^completionHandler)(BOOL)) {
  160. [self deleteHistoryAtIndex:indexPath.row];
  161. completionHandler(YES);
  162. }];
  163. UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
  164. return configuration;
  165. }
  166. @end