// // HBHomeViewController.m // HiBit // // Created by HI_LOSER on 2024/6/7. // #import "HBHomeViewController.h" //#import "QJShereModalView.h" //#import "QJVideoEpisodesModalView.h" #import "GKBallLoadingView.h" #import "GKLikeView.h" #import "GKDYVideoScrollView.h" #import "GKDYVideoCell.h" #import "GKDYPlayerManager.h" #import "GKDYVideoPortraitCell.h" #import "GKDYVideoLandscapeCell.h" @interface HBHomeViewController () //,QJVideoEpisodesModalViewDelegate> @property (nonatomic, strong) GKDYPlayerManager * manager; @property (assign, nonatomic) BOOL isDetails; @property (assign, nonatomic) NSInteger sid; //@property (nonatomic, strong) QJVideoEpisodesModalView * episodesModalView; @end @implementation HBHomeViewController - (instancetype)initWithVideoId:(NSInteger)sid { self = [super init]; if (self) { self.isDetails = YES; self.sid = sid; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.hbd_barAlpha = 0.0; self.edgesForExtendedLayout = UIRectEdgeNone; [self initUI]; [self setupRefresh]; [self requestData]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.manager.player.viewControllerDisappear = YES; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.manager.player.viewControllerDisappear = NO; } - (void)dealloc { NSLog(@"playerVC dealloc"); } - (void)initUI { [self.view addSubview:self.manager.scrollView]; [self.manager.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; } - (void)setupRefresh { if (self.isDetails) return; // 详情也 不需要加载更多 @weakify(self); self.manager.scrollView.mj_footer = [HBRefreshAutoNormalFooter footerWithRefreshingBlock:^{ @strongify(self); [self requestMore]; }]; } - (void)requestData { GKBallLoadingView *loadingView = [GKBallLoadingView loadingViewInView:self.view]; [loadingView startLoading]; self.manager.page = 1; @weakify(loadingView); [self requestData:^{ @strongify(loadingView); [loadingView stopLoading]; [loadingView removeFromSuperview]; }]; } - (void)requestMore { self.manager.page++; [self requestData:nil]; } - (void)refreshData:(void (^)(void))completion { self.manager.page = 1; [self requestData:completion]; } - (void)requestData:(nullable void (^)(void))completion { HBWeakSelf if (self.isDetails) { [HBNetWorkingUtility videos_fetchVideoDetailsWithSid:self.sid complateBlock:^(BOOL success, id _Nonnull data, NSString * _Nonnull message) { if (completion) completion(); if (success) { GKDYVideoModel * model = [GKDYVideoModel mj_objectWithKeyValues:data[@"shortVideoInfo"]]; NSArray * videoInfoModels = [GKDUVideoInfoModel mj_objectArrayWithKeyValuesArray:data[@"episode_list"]]; GKDUVideoInfoModel * formModel = [GKDUVideoInfoModel mj_objectWithKeyValues:data[@"video_info"]]; for (GKDUVideoInfoModel * videoInfoModel in videoInfoModels) { GKDYVideoModel * tempModel = [model copy]; tempModel.videoInfo = videoInfoModel; [weakSelf.manager.dataSources addObject:tempModel]; } // weakSelf.episodesModalView.models = weakSelf.manager.dataSources; // weakSelf.episodesModalView.indexModel = formModel; [weakSelf.manager.scrollView reloadData]; [weakSelf.manager.scrollView scrollToPageWithIndex:(formModel.episode -1)]; } }]; } else { [HBNetWorkingUtility discover_fetchRecommandsWithPage:self.manager.page size:20 complateBlock:^(BOOL success, id _Nonnull data, NSString * _Nonnull message) { if (completion) completion(); if (success) { NSArray * models = [GKDYVideoModel mj_objectArrayWithKeyValuesArray:data[@"list"]]; if (weakSelf.manager.page == 1) { [weakSelf.manager.dataSources removeAllObjects]; } [weakSelf.manager.dataSources addObjectsFromArray:models]; [weakSelf.manager.scrollView reloadData]; } [weakSelf.manager.scrollView.mj_footer endRefreshingWithNoMoreData]; }]; } } - (GKDYVideoModel *)model { return self.manager.currentCell.model; } #pragma mark - GKDYPlayerManagerDelegate - (void)scrollViewShouldLoadMore { [self requestMore]; } - (void)scrollViewDidPanDistance:(CGFloat)distance isEnd:(BOOL)isEnd { if ([self.delegate respondsToSelector:@selector(playerVC:didDragDistance:isEnd:)]) { [self.delegate playerVC:self didDragDistance:distance isEnd:isEnd]; } } - (void)cellZoomBegan:(GKDYVideoModel *)model { if ([self.delegate respondsToSelector:@selector(playerVC:cellZoomBegan:)]) { [self.delegate playerVC:self cellZoomBegan:model]; } } - (void)cellZoomEnded:(GKDYVideoModel *)model isFullscreen:(BOOL)isFullscreen { if ([self.delegate respondsToSelector:@selector(playerVC:cellZoomEnded:isFullscreen:)]) { [self.delegate playerVC:self cellZoomEnded:model isFullscreen:isFullscreen]; } } - (void)celldidClickShare:(GKDYVideoModel *)model cell:(GKDYVideoPortraitCell *)cell { // [[QJShereModalView new] presentInView:nil]; } - (void)celldidClickFavorite:(GKDYVideoModel *)model cell:(GKDYVideoPortraitCell *)cell { if (self.isDetails) return; if (model.is_collect) { [HBNetWorkingUtility customer_favoriteWithVideoId:model.cid complateBlock:nil]; } else { [HBNetWorkingUtility customer_unFavoriteWithVideoId:model.cid complateBlock:nil]; } } - (void)celldidClickEpiscode:(GKDYVideoModel *)model cell:(GKDYVideoPortraitCell *)cell { if (!self.isDetails) { HBHomeViewController * controller = [[HBHomeViewController alloc]initWithVideoId:model.cid]; [self.navigationController pushViewController:controller animated:YES]; } else { // self.episodesModalView.indexModel = self.manager.currentCell.model.videoInfo; // [self.episodesModalView presentInView:nil]; } } -(void)videoEpisodesModalViewDidSelectItemWithModel:(GKDUVideoInfoModel *)model { if (self.isDetails) { NSInteger row = 0; for (int index = 0; index < self.manager.dataSources.count; index ++ ) { GKDYVideoModel * tempModel = self.manager.dataSources[index]; if (model.vid == tempModel.videoInfo.vid) { row = index; break;; } } [self.manager.scrollView scrollToPageWithIndex:row]; } } - (void)cellSwitchWithModel:(GKDYVideoModel *)model { if (!self.isDetails) return; [HBNetWorkingUtility videos_uploadPlayHistoryWithSid:model.cid vId:model.videoInfo.vid complateBlock:^(BOOL success, id _Nonnull data, NSString * _Nonnull message) { }]; } #pragma mark - 懒加载 - (GKDYPlayerManager *)manager { if (!_manager) { _manager = [[GKDYPlayerManager alloc] init]; _manager.delegate = self; } return _manager; } //- (QJVideoEpisodesModalView *)episodesModalView{ // if (!_episodesModalView) { // _episodesModalView = [QJVideoEpisodesModalView new]; // _episodesModalView.delegate = self; // } // return _episodesModalView; //} @end