JXHomeViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // JXHomeViewController.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-13.
  6. //
  7. #import "JXHomeViewController.h"
  8. #import "JXAPIService.h"
  9. #import "JXCacheManager.h"
  10. #import "JXDetailViewController.h"
  11. @interface JXHomeViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
  12. @property (nonatomic, strong) UICollectionView *dramaCollectionView;
  13. @property (nonatomic, strong) UIRefreshControl *refreshControl;
  14. @property (nonatomic, strong) NSArray *dramas;
  15. @property (nonatomic, assign) NSInteger currentPage;
  16. @end
  17. @implementation JXHomeViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. NSLog(@"🔴 🔴 🔴 JXHomeViewController viewDidLoad 被调用!🔴 🔴 🔴");
  21. self.view.backgroundColor = [UIColor blackColor];
  22. self.title = @"短剧";
  23. NSLog(@"🔴 设置背景和标题完成");
  24. self.currentPage = 1;
  25. NSLog(@"🔴 准备调用 setupUI...");
  26. [self setupUI];
  27. NSLog(@"🔴 setupUI 调用完成");
  28. NSLog(@"🔴 准备调用 loadData...");
  29. [self loadData];
  30. NSLog(@"🔴 loadData 调用完成");
  31. NSLog(@"🔴 🔴 🔴 JXHomeViewController viewDidLoad 完成!🔴 🔴 🔴");
  32. }
  33. - (void)setupUI {
  34. NSLog(@"🟠 setupUI 开始");
  35. // 创建全屏纵向滚动的CollectionView(每个短剧占满屏幕)
  36. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  37. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  38. layout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
  39. layout.minimumLineSpacing = 0;
  40. layout.minimumInteritemSpacing = 0;
  41. NSLog(@"🟠 Layout 创建完成,itemSize: %.0f x %.0f", layout.itemSize.width, layout.itemSize.height);
  42. self.dramaCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
  43. collectionViewLayout:layout];
  44. self.dramaCollectionView.backgroundColor = [UIColor blackColor];
  45. self.dramaCollectionView.delegate = self;
  46. self.dramaCollectionView.dataSource = self;
  47. self.dramaCollectionView.pagingEnabled = YES;
  48. self.dramaCollectionView.showsVerticalScrollIndicator = NO;
  49. NSLog(@"🟠 CollectionView 创建完成,frame: %@", NSStringFromCGRect(self.dramaCollectionView.frame));
  50. [self.dramaCollectionView registerClass:[UICollectionViewCell class]
  51. forCellWithReuseIdentifier:@"DramaCell"];
  52. NSLog(@"🟠 Cell类注册完成");
  53. // 添加下拉刷新
  54. self.refreshControl = [[UIRefreshControl alloc] init];
  55. [self.refreshControl addTarget:self
  56. action:@selector(handleRefresh)
  57. forControlEvents:UIControlEventValueChanged];
  58. [self.dramaCollectionView addSubview:self.refreshControl];
  59. NSLog(@"🟠 刷新控件添加完成");
  60. // 添加到视图
  61. [self.view addSubview:self.dramaCollectionView];
  62. NSLog(@"🟠 CollectionView 添加到主视图");
  63. // 设置约束
  64. self.dramaCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
  65. [NSLayoutConstraint activateConstraints:@[
  66. [self.dramaCollectionView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
  67. [self.dramaCollectionView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  68. [self.dramaCollectionView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  69. [self.dramaCollectionView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
  70. ]];
  71. NSLog(@"🟠 约束设置完成");
  72. NSLog(@"🟠 setupUI 完成!");
  73. }
  74. - (void)loadData {
  75. // 加载短剧列表
  76. NSLog(@"📥 loadData 被调用");
  77. [self loadDramaListWithPage:1];
  78. }
  79. - (void)loadDramaListWithPage:(NSInteger)page {
  80. NSLog(@"📥 loadDramaListWithPage:%ld 被调用", (long)page);
  81. [[JXAPIService sharedService] getDramaListWithCategoryId:nil
  82. page:page
  83. pageSize:20
  84. success:^(id responseObject) {
  85. NSDictionary *response = responseObject;
  86. NSLog(@"📥 API 响应: %@", response);
  87. if ([response[@"code"] intValue] == 200) {
  88. NSArray *dramasData = response[@"data"][@"items"];
  89. NSLog(@"📥 收到 %ld 条短剧数据", (long)dramasData.count);
  90. if (page == 1) {
  91. self.dramas = dramasData;
  92. } else {
  93. // 追加数据
  94. NSMutableArray *mutableDramas = [self.dramas mutableCopy];
  95. [mutableDramas addObjectsFromArray:dramasData];
  96. self.dramas = mutableDramas;
  97. }
  98. NSLog(@"📥 self.dramas 现在有 %ld 条数据", (long)self.dramas.count);
  99. [self.dramaCollectionView reloadData];
  100. NSLog(@"📥 CollectionView reloadData 已调用");
  101. // 缓存数据
  102. [[JXCacheManager sharedManager] saveDramaList:dramasData
  103. forCategory:@"all"
  104. page:page];
  105. }
  106. [self.refreshControl endRefreshing];
  107. } failure:^(NSError *error) {
  108. NSLog(@"❌ 加载短剧列表失败: %@", error.localizedDescription);
  109. [self.refreshControl endRefreshing];
  110. // 尝试从缓存加载
  111. if (page == 1) {
  112. NSArray *cached = [[JXCacheManager sharedManager] getDramaListForCategory:@"all"
  113. page:page];
  114. if (cached) {
  115. self.dramas = cached;
  116. [self.dramaCollectionView reloadData];
  117. }
  118. }
  119. }];
  120. }
  121. - (void)handleRefresh {
  122. self.currentPage = 1;
  123. [self loadData];
  124. }
  125. #pragma mark - UICollectionViewDataSource
  126. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  127. NSInteger count = self.dramas.count;
  128. NSLog(@"📊 numberOfItemsInSection 被调用,返回 %ld 条数据", (long)count);
  129. return count;
  130. }
  131. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  132. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DramaCell"
  133. forIndexPath:indexPath];
  134. // 清理之前的子视图
  135. for (UIView *view in cell.contentView.subviews) {
  136. [view removeFromSuperview];
  137. }
  138. NSDictionary *drama = self.dramas[indexPath.item];
  139. // 背景色为黑色
  140. cell.contentView.backgroundColor = [UIColor blackColor];
  141. // 背景封面图 - 全屏
  142. UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
  143. backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
  144. backgroundImageView.clipsToBounds = YES;
  145. // TODO: 加载drama[@"cover"]图片
  146. backgroundImageView.backgroundColor = [UIColor darkGrayColor];
  147. [cell.contentView addSubview:backgroundImageView];
  148. // 渐变遮罩层(从透明到黑色)
  149. UIView *maskView = [[UIView alloc] initWithFrame:cell.contentView.bounds];
  150. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  151. gradientLayer.frame = maskView.bounds;
  152. gradientLayer.colors = @[
  153. (id)[UIColor clearColor].CGColor,
  154. (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3].CGColor,
  155. (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7].CGColor
  156. ];
  157. gradientLayer.locations = @[@0, @0.5, @1.0];
  158. [maskView.layer addSublayer:gradientLayer];
  159. [cell.contentView addSubview:maskView];
  160. // 正中心的"查看全部"按钮
  161. UIButton *viewAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
  162. CGFloat centerButtonWidth = 150;
  163. CGFloat centerButtonHeight = 50;
  164. CGFloat centerX = cell.contentView.bounds.size.width / 2;
  165. CGFloat centerY = cell.contentView.bounds.size.height / 2;
  166. viewAllButton.frame = CGRectMake(centerX - centerButtonWidth/2, centerY - centerButtonHeight/2, centerButtonWidth, centerButtonHeight);
  167. [viewAllButton setTitle:@"查看全部" forState:UIControlStateNormal];
  168. viewAllButton.backgroundColor = [UIColor whiteColor];
  169. viewAllButton.layer.cornerRadius = centerButtonHeight / 2;
  170. viewAllButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  171. [viewAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  172. [viewAllButton addTarget:self action:@selector(onViewAllClicked:) forControlEvents:UIControlEventTouchUpInside];
  173. [cell.contentView addSubview:viewAllButton];
  174. NSLog(@"🎯 正中心的'查看全部'按钮 frame: %@", NSStringFromCGRect(viewAllButton.frame));
  175. // 底部信息区域 - 距底部 60dp(为底部页签留空间)
  176. CGFloat bottomInfoHeight = 200;
  177. CGFloat bottomAreaY = cell.contentView.bounds.size.height - bottomInfoHeight - 60;
  178. UIView *bottomInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, bottomAreaY, cell.contentView.bounds.size.width, bottomInfoHeight)];
  179. bottomInfoView.backgroundColor = [UIColor clearColor];
  180. [cell.contentView addSubview:bottomInfoView];
  181. NSLog(@"📱 Cell高度: %.0f, bottomAreaY: %.0f, bottomInfoView frame: %@",
  182. cell.contentView.bounds.size.height, bottomAreaY, NSStringFromCGRect(bottomInfoView.frame));
  183. // 短剧标题
  184. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, bottomInfoView.bounds.size.width - 32, 60)];
  185. titleLabel.text = drama[@"title"];
  186. titleLabel.font = [UIFont boldSystemFontOfSize:24];
  187. titleLabel.textColor = [UIColor whiteColor];
  188. titleLabel.numberOfLines = 2;
  189. [bottomInfoView addSubview:titleLabel];
  190. // 短剧描述
  191. UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 65, bottomInfoView.bounds.size.width - 32, 60)];
  192. descLabel.text = drama[@"description"] ?: @"";
  193. descLabel.font = [UIFont systemFontOfSize:14];
  194. descLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9];
  195. descLabel.numberOfLines = 3;
  196. [bottomInfoView addSubview:descLabel];
  197. // 作者信息
  198. UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 130, bottomInfoView.bounds.size.width - 32, 20)];
  199. authorLabel.text = drama[@"author_info"] ?: @"";
  200. authorLabel.font = [UIFont systemFontOfSize:12];
  201. authorLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
  202. [bottomInfoView addSubview:authorLabel];
  203. // "合集"按钮
  204. UIButton *collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
  205. collectionButton.frame = CGRectMake(16, 155, bottomInfoView.bounds.size.width - 32, 40);
  206. [collectionButton setTitle:@"合集 ⊞" forState:UIControlStateNormal];
  207. collectionButton.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
  208. collectionButton.layer.cornerRadius = 20;
  209. collectionButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  210. [collectionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  211. [collectionButton addTarget:self action:@selector(onCollectionClicked:) forControlEvents:UIControlEventTouchUpInside];
  212. [bottomInfoView addSubview:collectionButton];
  213. return cell;
  214. }
  215. #pragma mark - UICollectionViewDelegate
  216. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  217. // 不需要在这里处理,按钮回调会处理
  218. }
  219. #pragma mark - Button Actions
  220. - (void)onViewAllClicked:(UIButton *)sender {
  221. // 获取按钮在Collection View中的位置
  222. CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.dramaCollectionView];
  223. NSIndexPath *indexPath = [self.dramaCollectionView indexPathForItemAtPoint:buttonPosition];
  224. if (indexPath) {
  225. NSDictionary *drama = self.dramas[indexPath.item];
  226. NSString *dramaId = drama[@"jx_drama_id"];
  227. NSLog(@"🎬 点击查看全部,dramaId: %@", dramaId);
  228. JXDetailViewController *detailVC = [[JXDetailViewController alloc] initWithDramaId:dramaId];
  229. [self.navigationController pushViewController:detailVC animated:YES];
  230. }
  231. }
  232. - (void)onCollectionClicked:(UIButton *)sender {
  233. // 获取按钮在Collection View中的位置
  234. CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.dramaCollectionView];
  235. NSIndexPath *indexPath = [self.dramaCollectionView indexPathForItemAtPoint:buttonPosition];
  236. if (indexPath) {
  237. NSDictionary *drama = self.dramas[indexPath.item];
  238. NSLog(@"🎬 点击合集,drama: %@", drama);
  239. // 显示合集选择菜单
  240. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择合集"
  241. message:nil
  242. preferredStyle:UIAlertControllerStyleActionSheet];
  243. UIAlertAction *currentAction = [UIAlertAction actionWithTitle:drama[@"title"]
  244. style:UIAlertActionStyleDefault
  245. handler:^(UIAlertAction *action) {
  246. NSLog(@"选择合集: %@", drama[@"title"]);
  247. }];
  248. [alert addAction:currentAction];
  249. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
  250. style:UIAlertActionStyleCancel
  251. handler:nil];
  252. [alert addAction:cancelAction];
  253. [self presentViewController:alert animated:YES completion:nil];
  254. }
  255. }
  256. @end