| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //
- // JXHomeViewController.m
- // AICity
- //
- // Created by TogetherWatch on 2025-10-13.
- //
- #import "JXHomeViewController.h"
- #import "JXAPIService.h"
- #import "JXCacheManager.h"
- #import "JXDetailViewController.h"
- @interface JXHomeViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
- @property (nonatomic, strong) UICollectionView *dramaCollectionView;
- @property (nonatomic, strong) UIRefreshControl *refreshControl;
- @property (nonatomic, strong) NSArray *dramas;
- @property (nonatomic, assign) NSInteger currentPage;
- @end
- @implementation JXHomeViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- NSLog(@"🔴 🔴 🔴 JXHomeViewController viewDidLoad 被调用!🔴 🔴 🔴");
-
- self.view.backgroundColor = [UIColor blackColor];
- self.title = @"短剧";
-
- NSLog(@"🔴 设置背景和标题完成");
-
- self.currentPage = 1;
-
- NSLog(@"🔴 准备调用 setupUI...");
- [self setupUI];
- NSLog(@"🔴 setupUI 调用完成");
-
- NSLog(@"🔴 准备调用 loadData...");
- [self loadData];
- NSLog(@"🔴 loadData 调用完成");
-
- NSLog(@"🔴 🔴 🔴 JXHomeViewController viewDidLoad 完成!🔴 🔴 🔴");
- }
- - (void)setupUI {
- NSLog(@"🟠 setupUI 开始");
-
- // 创建全屏纵向滚动的CollectionView(每个短剧占满屏幕)
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.scrollDirection = UICollectionViewScrollDirectionVertical;
- layout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
- layout.minimumLineSpacing = 0;
- layout.minimumInteritemSpacing = 0;
-
- NSLog(@"🟠 Layout 创建完成,itemSize: %.0f x %.0f", layout.itemSize.width, layout.itemSize.height);
-
- self.dramaCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
- collectionViewLayout:layout];
- self.dramaCollectionView.backgroundColor = [UIColor blackColor];
- self.dramaCollectionView.delegate = self;
- self.dramaCollectionView.dataSource = self;
- self.dramaCollectionView.pagingEnabled = YES;
- self.dramaCollectionView.showsVerticalScrollIndicator = NO;
-
- NSLog(@"🟠 CollectionView 创建完成,frame: %@", NSStringFromCGRect(self.dramaCollectionView.frame));
-
- [self.dramaCollectionView registerClass:[UICollectionViewCell class]
- forCellWithReuseIdentifier:@"DramaCell"];
-
- NSLog(@"🟠 Cell类注册完成");
-
- // 添加下拉刷新
- self.refreshControl = [[UIRefreshControl alloc] init];
- [self.refreshControl addTarget:self
- action:@selector(handleRefresh)
- forControlEvents:UIControlEventValueChanged];
- [self.dramaCollectionView addSubview:self.refreshControl];
-
- NSLog(@"🟠 刷新控件添加完成");
-
- // 添加到视图
- [self.view addSubview:self.dramaCollectionView];
-
- NSLog(@"🟠 CollectionView 添加到主视图");
-
- // 设置约束
- self.dramaCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
- [NSLayoutConstraint activateConstraints:@[
- [self.dramaCollectionView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
- [self.dramaCollectionView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
- [self.dramaCollectionView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
- [self.dramaCollectionView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
- ]];
-
- NSLog(@"🟠 约束设置完成");
- NSLog(@"🟠 setupUI 完成!");
- }
- - (void)loadData {
- // 加载短剧列表
- NSLog(@"📥 loadData 被调用");
- [self loadDramaListWithPage:1];
- }
- - (void)loadDramaListWithPage:(NSInteger)page {
- NSLog(@"📥 loadDramaListWithPage:%ld 被调用", (long)page);
- [[JXAPIService sharedService] getDramaListWithCategoryId:nil
- page:page
- pageSize:20
- success:^(id responseObject) {
- NSDictionary *response = responseObject;
- NSLog(@"📥 API 响应: %@", response);
- if ([response[@"code"] intValue] == 200) {
- NSArray *dramasData = response[@"data"][@"items"];
- NSLog(@"📥 收到 %ld 条短剧数据", (long)dramasData.count);
-
- if (page == 1) {
- self.dramas = dramasData;
- } else {
- // 追加数据
- NSMutableArray *mutableDramas = [self.dramas mutableCopy];
- [mutableDramas addObjectsFromArray:dramasData];
- self.dramas = mutableDramas;
- }
-
- NSLog(@"📥 self.dramas 现在有 %ld 条数据", (long)self.dramas.count);
- [self.dramaCollectionView reloadData];
- NSLog(@"📥 CollectionView reloadData 已调用");
-
- // 缓存数据
- [[JXCacheManager sharedManager] saveDramaList:dramasData
- forCategory:@"all"
- page:page];
- }
-
- [self.refreshControl endRefreshing];
- } failure:^(NSError *error) {
- NSLog(@"❌ 加载短剧列表失败: %@", error.localizedDescription);
- [self.refreshControl endRefreshing];
-
- // 尝试从缓存加载
- if (page == 1) {
- NSArray *cached = [[JXCacheManager sharedManager] getDramaListForCategory:@"all"
- page:page];
- if (cached) {
- self.dramas = cached;
- [self.dramaCollectionView reloadData];
- }
- }
- }];
- }
- - (void)handleRefresh {
- self.currentPage = 1;
- [self loadData];
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- NSInteger count = self.dramas.count;
- NSLog(@"📊 numberOfItemsInSection 被调用,返回 %ld 条数据", (long)count);
- return count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DramaCell"
- forIndexPath:indexPath];
-
- // 清理之前的子视图
- for (UIView *view in cell.contentView.subviews) {
- [view removeFromSuperview];
- }
-
- NSDictionary *drama = self.dramas[indexPath.item];
-
- // 背景色为黑色
- cell.contentView.backgroundColor = [UIColor blackColor];
-
- // 背景封面图 - 全屏
- UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
- backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
- backgroundImageView.clipsToBounds = YES;
- // TODO: 加载drama[@"cover"]图片
- backgroundImageView.backgroundColor = [UIColor darkGrayColor];
- [cell.contentView addSubview:backgroundImageView];
-
- // 渐变遮罩层(从透明到黑色)
- UIView *maskView = [[UIView alloc] initWithFrame:cell.contentView.bounds];
- CAGradientLayer *gradientLayer = [CAGradientLayer layer];
- gradientLayer.frame = maskView.bounds;
- gradientLayer.colors = @[
- (id)[UIColor clearColor].CGColor,
- (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3].CGColor,
- (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7].CGColor
- ];
- gradientLayer.locations = @[@0, @0.5, @1.0];
- [maskView.layer addSublayer:gradientLayer];
- [cell.contentView addSubview:maskView];
-
- // 正中心的"查看全部"按钮
- UIButton *viewAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
- CGFloat centerButtonWidth = 150;
- CGFloat centerButtonHeight = 50;
- CGFloat centerX = cell.contentView.bounds.size.width / 2;
- CGFloat centerY = cell.contentView.bounds.size.height / 2;
- viewAllButton.frame = CGRectMake(centerX - centerButtonWidth/2, centerY - centerButtonHeight/2, centerButtonWidth, centerButtonHeight);
- [viewAllButton setTitle:@"查看全部" forState:UIControlStateNormal];
- viewAllButton.backgroundColor = [UIColor whiteColor];
- viewAllButton.layer.cornerRadius = centerButtonHeight / 2;
- viewAllButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
- [viewAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [viewAllButton addTarget:self action:@selector(onViewAllClicked:) forControlEvents:UIControlEventTouchUpInside];
- [cell.contentView addSubview:viewAllButton];
-
- NSLog(@"🎯 正中心的'查看全部'按钮 frame: %@", NSStringFromCGRect(viewAllButton.frame));
-
- // 底部信息区域 - 距底部 60dp(为底部页签留空间)
- CGFloat bottomInfoHeight = 200;
- CGFloat bottomAreaY = cell.contentView.bounds.size.height - bottomInfoHeight - 60;
- UIView *bottomInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, bottomAreaY, cell.contentView.bounds.size.width, bottomInfoHeight)];
- bottomInfoView.backgroundColor = [UIColor clearColor];
- [cell.contentView addSubview:bottomInfoView];
-
- NSLog(@"📱 Cell高度: %.0f, bottomAreaY: %.0f, bottomInfoView frame: %@",
- cell.contentView.bounds.size.height, bottomAreaY, NSStringFromCGRect(bottomInfoView.frame));
-
- // 短剧标题
- UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, bottomInfoView.bounds.size.width - 32, 60)];
- titleLabel.text = drama[@"title"];
- titleLabel.font = [UIFont boldSystemFontOfSize:24];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.numberOfLines = 2;
- [bottomInfoView addSubview:titleLabel];
-
- // 短剧描述
- UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 65, bottomInfoView.bounds.size.width - 32, 60)];
- descLabel.text = drama[@"description"] ?: @"";
- descLabel.font = [UIFont systemFontOfSize:14];
- descLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9];
- descLabel.numberOfLines = 3;
- [bottomInfoView addSubview:descLabel];
-
- // 作者信息
- UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 130, bottomInfoView.bounds.size.width - 32, 20)];
- authorLabel.text = drama[@"author_info"] ?: @"";
- authorLabel.font = [UIFont systemFontOfSize:12];
- authorLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
- [bottomInfoView addSubview:authorLabel];
-
- // "合集"按钮
- UIButton *collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
- collectionButton.frame = CGRectMake(16, 155, bottomInfoView.bounds.size.width - 32, 40);
- [collectionButton setTitle:@"合集 ⊞" forState:UIControlStateNormal];
- collectionButton.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
- collectionButton.layer.cornerRadius = 20;
- collectionButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
- [collectionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [collectionButton addTarget:self action:@selector(onCollectionClicked:) forControlEvents:UIControlEventTouchUpInside];
- [bottomInfoView addSubview:collectionButton];
-
- return cell;
- }
- #pragma mark - UICollectionViewDelegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- // 不需要在这里处理,按钮回调会处理
- }
- #pragma mark - Button Actions
- - (void)onViewAllClicked:(UIButton *)sender {
- // 获取按钮在Collection View中的位置
- CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.dramaCollectionView];
- NSIndexPath *indexPath = [self.dramaCollectionView indexPathForItemAtPoint:buttonPosition];
-
- if (indexPath) {
- NSDictionary *drama = self.dramas[indexPath.item];
- NSString *dramaId = drama[@"jx_drama_id"];
- NSLog(@"🎬 点击查看全部,dramaId: %@", dramaId);
-
- JXDetailViewController *detailVC = [[JXDetailViewController alloc] initWithDramaId:dramaId];
- [self.navigationController pushViewController:detailVC animated:YES];
- }
- }
- - (void)onCollectionClicked:(UIButton *)sender {
- // 获取按钮在Collection View中的位置
- CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.dramaCollectionView];
- NSIndexPath *indexPath = [self.dramaCollectionView indexPathForItemAtPoint:buttonPosition];
-
- if (indexPath) {
- NSDictionary *drama = self.dramas[indexPath.item];
- NSLog(@"🎬 点击合集,drama: %@", drama);
-
- // 显示合集选择菜单
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择合集"
- message:nil
- preferredStyle:UIAlertControllerStyleActionSheet];
-
- UIAlertAction *currentAction = [UIAlertAction actionWithTitle:drama[@"title"]
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction *action) {
- NSLog(@"选择合集: %@", drama[@"title"]);
- }];
- [alert addAction:currentAction];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
- style:UIAlertActionStyleCancel
- handler:nil];
- [alert addAction:cancelAction];
-
- [self presentViewController:alert animated:YES completion:nil];
- }
- }
- @end
|