JXDetailViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // JXDetailViewController.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-13.
  6. // 短剧详情+播放器页面(对标Android的JuXingPlayerActivity)
  7. //
  8. #import "JXDetailViewController.h"
  9. #import "JXAPIService.h"
  10. #import "JXCacheManager.h"
  11. #import "JXDrama.h"
  12. #import "JXEpisode.h"
  13. #import "JXInteraction.h"
  14. #import "JXAVPlayer.h"
  15. #import "JXSuperPlayer.h"
  16. #import <AVFoundation/AVFoundation.h>
  17. #import "JXCollectionViewController.h"
  18. #import "JXCommentViewController.h"
  19. @interface JXDetailViewController () <JXSuperPlayerDelegate, UICollectionViewDelegate, UICollectionViewDataSource,JXCollectionViewControllerDelegate>
  20. // 播放器
  21. @property (nonatomic, strong) UIView *playerContainer;
  22. @property (nonatomic, strong) JXSuperPlayer *player;
  23. // UI组件
  24. @property (nonatomic, strong) UICollectionView *episodeCollectionView;
  25. @property (nonatomic, strong) UILabel *titleLabel;
  26. @property (nonatomic, strong) UILabel *descLabel;
  27. @property (nonatomic, strong) UILabel *authorLabel;
  28. @property (nonatomic, strong) UIButton *likeButton;
  29. @property (nonatomic, strong) UILabel *likeCountLabel;
  30. @property (nonatomic, strong) UIButton *favoriteButton;
  31. @property (nonatomic, strong) UILabel *favoriteCountLabel;
  32. @property (nonatomic, strong) UIButton *commentButton;
  33. @property (nonatomic, strong) UILabel *commentCountLabel;
  34. // 数据
  35. @property (nonatomic, strong) JXDrama *drama;
  36. @property (nonatomic, strong) NSArray<JXEpisode *> *episodes;
  37. @property (nonatomic, strong) JXInteraction *interaction;
  38. @property (nonatomic, assign) NSInteger currentEpisodeIndex;
  39. @end
  40. @implementation JXDetailViewController
  41. - (instancetype)initWithDramaId:(NSString *)dramaId {
  42. self = [super init];
  43. if (self) {
  44. _dramaId = [dramaId copy];
  45. _currentEpisodeIndex = 0;
  46. }
  47. return self;
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. self.view.backgroundColor = [UIColor blackColor];
  52. // 隐藏导航栏返回按钮
  53. self.navigationItem.hidesBackButton = YES;
  54. [self setupUI];
  55. [self loadData];
  56. }
  57. - (void)setupUI {
  58. CGFloat width = self.view.bounds.size.width;
  59. CGFloat height = self.view.bounds.size.height;
  60. // 创建垂直滚动的CollectionView(每集占满屏幕)
  61. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  62. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  63. layout.itemSize = CGSizeMake(width, height);
  64. layout.minimumLineSpacing = 0;
  65. layout.minimumInteritemSpacing = 0;
  66. self.episodeCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
  67. collectionViewLayout:layout];
  68. self.episodeCollectionView.backgroundColor = [UIColor blackColor];
  69. self.episodeCollectionView.delegate = self;
  70. self.episodeCollectionView.dataSource = self;
  71. self.episodeCollectionView.pagingEnabled = YES;
  72. self.episodeCollectionView.showsVerticalScrollIndicator = NO;
  73. [self.episodeCollectionView registerClass:[UICollectionViewCell class]
  74. forCellWithReuseIdentifier:@"EpisodeCell"];
  75. [self.view addSubview:self.episodeCollectionView];
  76. // 设置约束
  77. self.episodeCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
  78. [NSLayoutConstraint activateConstraints:@[
  79. [self.episodeCollectionView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
  80. [self.episodeCollectionView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  81. [self.episodeCollectionView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  82. [self.episodeCollectionView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
  83. ]];
  84. }
  85. - (void)loadData {
  86. if (!self.dramaId) {
  87. return;
  88. }
  89. // 从网络加载
  90. [[JXAPIService sharedService] getDramaDetailWithDramaId:self.dramaId success:^(id responseObject) {
  91. NSDictionary *response = responseObject;
  92. NSLog(@"📡 getDramaDetail响应代码: %@", response[@"code"]);
  93. NSLog(@"📡 getDramaDetail完整响应: %@", response);
  94. if ([response[@"code"] intValue] == 0) {
  95. NSDictionary *data = response[@"data"];
  96. NSLog(@"✅ 获取短剧数据成功");
  97. NSLog(@"📊 Drama数据: %@", data[@"drama"]);
  98. NSLog(@"📊 Episodes数量: %lu", (unsigned long)[data[@"episodes"] count]);
  99. [self updateUIWithData:data];
  100. // 自动滚动到第一集
  101. NSLog(@"⏯️ 自动滚动检查 - 剧集总数: %lu", (unsigned long)self.episodes.count);
  102. if (self.episodes.count > 0) {
  103. NSLog(@"⏯️ 滚动到第一集");
  104. [self.episodeCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.playIndex inSection:0]
  105. atScrollPosition:UICollectionViewScrollPositionCenteredVertically
  106. animated:NO];
  107. } else {
  108. NSLog(@"❌ 剧集列表为空,无法播放");
  109. }
  110. } else {
  111. NSLog(@"❌ API返回错误代码: %@", response[@"code"]);
  112. }
  113. } failure:^(NSError *error) {
  114. NSLog(@"❌ 加载短剧详情失败: %@", error.localizedDescription);
  115. }];
  116. }
  117. - (void)updateUIWithData:(NSDictionary *)data {
  118. // 解析数据
  119. self.drama = [[JXDrama alloc] initWithDictionary:data[@"drama"]];
  120. NSArray *episodesData = data[@"episodes"];
  121. NSMutableArray *episodesList = [NSMutableArray array];
  122. for (NSDictionary *episodeDict in episodesData) {
  123. JXEpisode *episode = [[JXEpisode alloc] initWithDictionary:episodeDict];
  124. [episodesList addObject:episode];
  125. }
  126. self.episodes = episodesList;
  127. self.interaction = [[JXInteraction alloc] initWithDictionary:data[@"interaction"]];
  128. [self.episodeCollectionView reloadData];
  129. }
  130. #pragma mark - UICollectionViewDataSource
  131. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  132. return self.episodes.count;
  133. }
  134. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  135. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EpisodeCell"
  136. forIndexPath:indexPath];
  137. // 清理之前的子视图
  138. for (UIView *view in cell.contentView.subviews) {
  139. [view removeFromSuperview];
  140. }
  141. cell.contentView.backgroundColor = [UIColor blackColor];
  142. CGFloat width = cell.contentView.bounds.size.width;
  143. CGFloat height = cell.contentView.bounds.size.height;
  144. // 播放器容器(占据上半部分)
  145. CGFloat playerHeight = height;
  146. UIView *playerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, playerHeight)];
  147. playerContainer.backgroundColor = [UIColor blackColor];
  148. [cell.contentView addSubview:playerContainer];
  149. [cell.contentView sendSubviewToBack:playerContainer];
  150. // 创建播放器
  151. JXSuperPlayer *player = [[JXSuperPlayer alloc] initWithContainerView:playerContainer];
  152. player.delegate = self;
  153. // 标题
  154. // 右侧互动按钮
  155. [self setupInteractionButtonsForCell:cell];
  156. UILabel * seeL = [[UILabel alloc] initWithFrame:CGRectMake(16, kScreenHeight - 14-safebottom, SCREEN_WIDTH - 32, 14)];
  157. seeL.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
  158. seeL.textColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1];
  159. [cell.contentView addSubview:seeL];
  160. seeL.text = [NSString stringWithFormat:@"%@次播放",@"30万"];
  161. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(seeL.frame) - 58, SCREEN_WIDTH, 38)];
  162. bottomView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.1];
  163. [cell.contentView addSubview:bottomView];
  164. UIImageView *sImgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10.5, 17, 17)];
  165. sImgView.image = [UIImage imageNamed:@"icon 2.1 拷贝"];
  166. [bottomView addSubview:sImgView];
  167. UILabel *hjL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sImgView.frame)+4, 0, 100, 38)];
  168. [bottomView addSubview:hjL];
  169. hjL.font = [UIFont boldSystemFontOfSize:18];
  170. hjL.textColor = UIColor.whiteColor;
  171. hjL.text = @"合集";
  172. UIImageView *mImgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-27, 13.5, 11, 11)];
  173. mImgView.image = [UIImage imageNamed:@"Frame 9366"];
  174. [bottomView addSubview:mImgView];
  175. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHJ:)];
  176. [bottomView addGestureRecognizer:tap];
  177. UILabel *descLabel = [[UILabel alloc] init];
  178. descLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  179. descLabel.textColor = [UIColor whiteColor];
  180. descLabel.numberOfLines = 2;
  181. [cell.contentView addSubview:descLabel];
  182. [cell.contentView bringSubviewToFront:descLabel];
  183. [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  184. make.bottom.equalTo(bottomView.mas_top).offset(-15);
  185. make.left.equalTo(cell.contentView).offset(16);
  186. make.width.mas_equalTo(SCREEN_WIDTH - 90);
  187. }];
  188. UILabel *titleLabel = [[UILabel alloc] init];
  189. titleLabel.font = [UIFont boldSystemFontOfSize:18];
  190. titleLabel.textColor = [UIColor whiteColor];
  191. titleLabel.numberOfLines = 2;
  192. [cell.contentView addSubview:titleLabel];
  193. [cell.contentView bringSubviewToFront:titleLabel];
  194. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  195. make.bottom.equalTo(descLabel.mas_top).offset(-12.5);
  196. make.left.equalTo(cell.contentView).offset(16);
  197. make.width.mas_equalTo(SCREEN_WIDTH - 90);
  198. }];
  199. // // 作者
  200. // UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, width - 32, 20)];
  201. // authorLabel.font = [UIFont systemFontOfSize:13];
  202. // authorLabel.textColor = [UIColor grayColor];
  203. // [cell.contentView addSubview:authorLabel];
  204. //
  205. // [cell.contentView bringSubviewToFront:authorLabel];
  206. // y += 30;
  207. // // 剧集信息区域
  208. // UIView *episodeInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, y, width, height - y)];
  209. // episodeInfoView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0];
  210. // [cell.contentView addSubview:episodeInfoView];
  211. JXEpisode *episode = self.episodes[indexPath.item];
  212. //
  213. // // 更新标签
  214. titleLabel.text = self.drama.title ?: @"";
  215. descLabel.text = episode.title ?: @"";
  216. // authorLabel.text = [NSString stringWithFormat:@"@%@", self.drama.authorInfo ?: @"未知"];
  217. //
  218. // // 剧集编号和标题
  219. // UILabel *episodeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, width - 32, 40)];
  220. // NSString *episodeTitle = [NSString stringWithFormat:@"第%ld集", (long)episode.episodeNumber];
  221. // if (episode.title && episode.title.length > 0) {
  222. // episodeTitle = [NSString stringWithFormat:@"%@ - %@", episodeTitle, episode.title];
  223. // }
  224. // episodeTitleLabel.text = episodeTitle;
  225. // episodeTitleLabel.font = [UIFont boldSystemFontOfSize:16];
  226. // episodeTitleLabel.textColor = [UIColor whiteColor];
  227. // episodeTitleLabel.numberOfLines = 2;
  228. // [episodeInfoView addSubview:episodeTitleLabel];
  229. // 播放视频
  230. [player playWithAppId:episode.appId fileId:episode.fileId psign:episode.psign];
  231. return cell;
  232. }
  233. - (void)showHJ:(UIGestureRecognizer *)gtr{
  234. JXEpisode *episode = self.episodes[self.currentEpisodeIndex];
  235. [self showCollectionDialogWithDramaId:[NSString stringWithFormat:@"%ld",episode.dramaId]];
  236. }
  237. - (void)showCollectionDialogWithDramaId:(NSString *)dramaId {
  238. JXCollectionViewController *collectionVC = [[JXCollectionViewController alloc] initWithDramaId:dramaId];
  239. collectionVC.delegate = self;
  240. // 设置弹窗样式(底部弹出)
  241. if (@available(iOS 15.0, *)) {
  242. UISheetPresentationController *sheet = collectionVC.sheetPresentationController;
  243. sheet.detents = @[
  244. [UISheetPresentationControllerDetent mediumDetent],
  245. [UISheetPresentationControllerDetent largeDetent]
  246. ];
  247. sheet.prefersGrabberVisible = YES;
  248. }
  249. [self presentViewController:collectionVC animated:YES completion:nil];
  250. }
  251. - (void)commentDidClicked{
  252. JXEpisode *episode = self.episodes[self.currentEpisodeIndex];
  253. JXCommentViewController *commentVC = [[JXCommentViewController alloc] initWithDramaId:[NSString stringWithFormat:@"%ld",episode.dramaId]];
  254. // 设置弹窗样式(底部弹出)
  255. if (@available(iOS 15.0, *)) {
  256. UISheetPresentationController *sheet = commentVC.sheetPresentationController;
  257. sheet.detents = @[
  258. [UISheetPresentationControllerDetent mediumDetent],
  259. [UISheetPresentationControllerDetent largeDetent]
  260. ];
  261. sheet.prefersGrabberVisible = YES;
  262. }
  263. [self presentViewController:commentVC animated:YES completion:^{
  264. NSLog(@"[JXShortDrama] 🗨️ commentVC present 完成!");
  265. }];
  266. }
  267. - (void)collectionViewControllerDidSelectEpisode:(NSString *)episodeId {
  268. NSLog(@"[JXShortDrama] 选择播放剧集: %@", episodeId);
  269. // TODO: 切换到选定的剧集播放
  270. // 可以找到对应的短剧,然后滚动到该位置并播放
  271. }
  272. #pragma mark - UICollectionViewDelegate
  273. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  274. // 停止不可见的播放器
  275. }
  276. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  277. // 获取当前可见的cell索引
  278. CGFloat pageWidth = scrollView.frame.size.width;
  279. CGFloat pageHeight = scrollView.frame.size.height;
  280. NSInteger currentPage = (NSInteger)(scrollView.contentOffset.y / pageHeight);
  281. if (currentPage != self.currentEpisodeIndex) {
  282. self.currentEpisodeIndex = currentPage;
  283. NSLog(@"📺 滚动到第 %ld 集", (long)currentPage + 1);
  284. }
  285. }
  286. #pragma mark - Helper Methods
  287. - (void)setupInteractionButtonsForCell:(UICollectionViewCell *)cell {
  288. CGFloat width = cell.contentView.bounds.size.width;
  289. UIView *container = [[UIView alloc] initWithFrame:CGRectMake(width - 48, kScreenHeight - safebottom - 190 - 90, 32, 190)];
  290. [cell.contentView addSubview:container];
  291. // 点赞
  292. UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  293. likeButton.frame = CGRectMake(0, 0, 32, 32);
  294. [likeButton setImage:[UIImage imageNamed:@"icon 1.1"] forState:UIControlStateNormal];
  295. [likeButton setImage:[UIImage imageNamed:@"icon 1.5"] forState:UIControlStateSelected];
  296. [container addSubview:likeButton];
  297. UILabel *likeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 32, 18)];
  298. likeCountLabel.textAlignment = NSTextAlignmentCenter;
  299. likeCountLabel.font = [UIFont systemFontOfSize:12];
  300. likeCountLabel.textColor = [UIColor whiteColor];
  301. likeCountLabel.text = [self formatCount:self.interaction.likeCount];
  302. [container addSubview:likeCountLabel];
  303. // 收藏
  304. UIButton *favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
  305. favoriteButton.frame = CGRectMake(0, CGRectGetMaxY(likeCountLabel.frame) + 20, 32, 32);
  306. [favoriteButton setImage:[UIImage imageNamed:@"icon 1.2"] forState:UIControlStateNormal];
  307. [favoriteButton setImage:[UIImage imageNamed:@"icon 1.5"] forState:UIControlStateSelected];
  308. [container addSubview:favoriteButton];
  309. UILabel *favoriteCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(favoriteButton.frame), 32, 18)];
  310. favoriteCountLabel.textAlignment = NSTextAlignmentCenter;
  311. favoriteCountLabel.font = [UIFont systemFontOfSize:12];
  312. favoriteCountLabel.textColor = [UIColor whiteColor];
  313. favoriteCountLabel.text = [self formatCount:self.interaction.favoriteCount];
  314. [container addSubview:favoriteCountLabel];
  315. // 评论
  316. UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
  317. commentButton.frame = CGRectMake(0, CGRectGetMaxY(favoriteCountLabel.frame) + 20, 32, 32);
  318. [commentButton setImage:[UIImage imageNamed:@"icon 1.3"] forState:UIControlStateNormal];
  319. [container addSubview:commentButton];
  320. [commentButton addTarget:self action:@selector(commentDidClicked) forControlEvents:UIControlEventTouchUpInside];
  321. UILabel *commentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(commentButton.frame) , 32, 18)];
  322. commentCountLabel.textAlignment = NSTextAlignmentCenter;
  323. commentCountLabel.font = [UIFont systemFontOfSize:12];
  324. commentCountLabel.textColor = [UIColor whiteColor];
  325. commentCountLabel.text = [self formatCount:self.interaction.commentCount];
  326. [container addSubview:commentCountLabel];
  327. }
  328. - (NSString *)formatCount:(long long)count {
  329. if (count >= 1000000) {
  330. return [NSString stringWithFormat:@"%.1fM", count / 1000000.0];
  331. } else if (count >= 1000) {
  332. return [NSString stringWithFormat:@"%.1fK", count / 1000.0];
  333. }
  334. return [NSString stringWithFormat:@"%lld", count];
  335. }
  336. - (void)dealloc {
  337. // 停止所有播放器
  338. }
  339. @end