// // liveListController.m // ThePaperHD // // Created by scar1900 on 15/3/10. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "liveListController.h" #import "liveTitleCell.h" #import "liveTopDateCell.h" #import "liveContTitleCell.h" #import "liveContentCell.h" #import "liveVideoCell.h" #import "liveImageCell.h" #import "TPImageScannerController.h" #import "liveContFootCell.h" #import "detailContentHomeController.h" #import "ImageContentController.h" #import "topicHomeController.h" #import "TPcontentWebController.h" #import "TPLiveHomeController.h" #define CONTENTWIDTH 1060/2 #define TABLEWIDTH 1330/2 @interface liveListController () @property(nonatomic, strong)liveInfoBO *liveInfoBO; @property(nonatomic, strong)NSMutableArray *dataList; @property(nonatomic, strong)NSMutableArray *heightOfCellInEachSection; //cell高度数组 @property(nonatomic, strong)NSMutableArray *sectionTitleHeightList; @property(nonatomic, strong)ALMoviePlayerController *moviePlayerController; @end @implementation liveListController @synthesize nodeId = _nodeId; @synthesize livePageBO = _livePageBO; @synthesize dataList = _dataList; @synthesize heightOfCellInEachSection,sectionTitleHeightList; @synthesize liveInfoBO; @synthesize moviePlayerController; @synthesize isVideoPlaying; @synthesize delegate; - (void)viewDidLoad { [super viewDidLoad]; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.clipsToBounds = YES; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.contentInset = UIEdgeInsetsZero; self.tableView.hidden = YES; [self configCanRefresh:YES canLoad:YES]; self.tableView.refreshControl.frame = CGRectMake(0, self.tableView.refreshControl.frame.origin.y, 1164/2, self.tableView.refreshControl.frame.size.height); [self setLoadFooterFrame:CGRectMake(0, CGRectGetHeight(self.tableView.bounds) ,TABLEWIDTH , 60)]; UIPinchGestureRecognizer *zoomGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchHandler:)]; zoomGesture.delaysTouchesBegan = YES; [self.tableView addGestureRecognizer:zoomGesture]; } -(void)setNodeId:(NSString *)Id{ _nodeId = Id; if (!self.viewModel) { self.viewModel = [liveViewModel new]; self.viewModel.delegate = self; self.viewModel.nodeId = self.nodeId; [self manualRefresh]; } } - (void)setLivePageBO:(liveDetailPageVO *)pageBO { _livePageBO = pageBO; self.heightOfCellInEachSection = [NSMutableArray array]; self.sectionTitleHeightList = [NSMutableArray array]; __weak typeof(self) weakSelf = self; [self.viewModel getTableDataSourceWithLivePageData:pageBO completion:^(NSMutableArray *dataSource, NSMutableArray *sectionTitleList, NSMutableArray *heightList, NSString *nextUrl) { weakSelf.nextUrl = nextUrl; weakSelf.liveInfoBO = setJsonDicToDataModel(pageBO.liveInfo, [liveInfoBO class]); weakSelf.heightOfCellInEachSection = [NSMutableArray arrayWithArray:heightList]; weakSelf.sectionTitleHeightList = [NSMutableArray arrayWithArray:sectionTitleList]; weakSelf.dataList = [NSMutableArray arrayWithArray:dataSource]; }]; } - (void)setDataList:(NSMutableArray *)list { self.tableView.hidden = NO; BOOL isListNULL = NO; if (!_dataList || _dataList.count == 0) { isListNULL = YES; } _dataList = list; [self.tableView reloadData]; if (isListNULL) { [CoreAnimationEffect animationEaseOut:self.view]; } } - (void)reloadTableWithFontChange { //【倒退】直播页:切换字号后必定闪退(bug:5673) self.livePageBO = self.livePageBO; [self.tableView reloadData]; [self.tableView setContentOffset:CGPointZero animated:YES]; } #pragma mark - refresh and load handler - (void)pullRefreshHandler { if (!self.nodeId) { TPLOG(@"数据有问题,无法获取新数据"); [self endRefresh]; return; } [self.viewModel remotePullMethod]; } - (void)pullLoadMoreHander { if (!self.nextUrl) { TPLOG(@"数据有问题,无法获取新数据"); [self loadSuccess]; return; } [self.viewModel remotePullLoadMethod:self.nextUrl]; } #pragma mark - tableView delegate #pragma mark UITableViewDelegate and UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataList?self.dataList.count:0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (self.dataList && self.dataList.count > 0) { id obj = self.dataList[section]; if ([obj isKindOfClass:[NSArray class]]) { NSArray *listArray = obj; return listArray.count; }else return 1; }else return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.heightOfCellInEachSection && self.heightOfCellInEachSection.count > 0) { id obj = self.heightOfCellInEachSection[indexPath.section]; if ([obj isKindOfClass:[NSArray class]]) { NSArray *listArray = obj; NSString *heightStr = listArray[indexPath.row]; return [heightStr floatValue]; }else { return [obj floatValue]; } }else return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { NSString *titleHeight = sectionTitleHeightList[section]; if (!isBlankString(titleHeight)) { return 30; }else return 0; } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section != 0) { CGFloat padding = 0; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { padding = 125; } UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 30)]; view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(padding, 0, TABLEWIDTH, 30)]; backView.backgroundColor = [UIColor colorWithHexString:LiveSectionHeaderColor]; [view addSubview:backView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, TABLEWIDTH-10, 30)]; label.font = appFont(15, NO); label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentLeft; [backView addSubview:label]; NSString *title = self.sectionTitleHeightList[section]; label.text = title; return view; }else return nil; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { id obj = self.dataList[indexPath.section]; if ([obj isKindOfClass:[liveInfoBO class]]) { return [self getLiveTitleCell:table cellForRowAtIndexPath:indexPath]; //直播标题 }else{ NSArray *list = obj; id indexObject = list[indexPath.row]; NSDictionary *dic = indexObject; NSString *type = dic[@"type"]; if ([type isEqualToString:@"topDate"]) { //置顶时间 return [self getTopDateCell:table cellForRowAtIndexPath:indexPath]; }else if ([type isEqualToString:@"topTitle"] || [type isEqualToString:@"contentTitle"]) { return [self getContTitleCell:table cellForRowAtIndexPath:indexPath]; //文章标题 }else if ([type isMatchedByRegex:@"contentText"]) { return [self getContTextCell:table cellForRowAtIndexPath:indexPath]; //正文文字 }else if ([type isMatchedByRegex:@"video"]) { return [self getVideoCell:table cellForRowAtIndexPath:indexPath]; //视频 }else if ([type isMatchedByRegex:@"image"]) { return [self getImageCell:table cellForRowAtIndexPath:indexPath]; //图片 }else { return [self getfootCell:table cellForRowAtIndexPath:indexPath]; //分享和查看更多 } } } - (liveTitleCell *)getLiveTitleCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveTitleCell"; liveTitleCell *cell = (liveTitleCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveTitleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } id obj = self.dataList[indexPath.section]; cell.liveInfo = obj; return cell; } - (liveTopDateCell *)getTopDateCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveTopDateCell"; liveTopDateCell *cell = (liveTopDateCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveTopDateCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; return cell; } - (liveContTitleCell *)getContTitleCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveContTitleCell"; liveContTitleCell *cell = (liveContTitleCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveContTitleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; if (indexPath.row == 0) { cell.isFirst = YES; }else cell.isFirst = NO; return cell; } - (liveContentCell *)getContTextCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveContentCell"; liveContentCell *cell = (liveContentCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; return cell; } - (liveVideoCell *)getVideoCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveVideoCell"; liveVideoCell *cell = (liveVideoCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveVideoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; cell.delegate = self; return cell; } - (liveImageCell *)getImageCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveImageCell"; liveImageCell *cell = (liveImageCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; cell.delegate = self; return cell; } - (liveContFootCell *)getfootCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"liveContFootCell"; liveContFootCell *cell = (liveContFootCell*)[table dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[liveContFootCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSArray *list = self.dataList[indexPath.section]; id obj = list[indexPath.row]; cell.dataDic = obj; cell.delegate = self; cell.indexPath = indexPath; if (indexPath.row == list.count-1) { cell.isLast = YES; }else cell.isLast = NO; return cell; } #pragma mark - cell delegate - (void)clickImage:(imageObjectBO *)imgBO imageList:(NSArray *)imageList{ TPLOG(@"click Image:%@",imgBO); NSMutableArray *imageBOArray = [NSMutableArray array]; [imageList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[imageObjectBO class]]) { imageObjectBO *imageBO = obj; if ([imageBO.tags isEqualToString:@"other"]) { [imageBOArray addObject:imageBO]; } } }]; __block NSInteger currentIndex; [imageBOArray enumerateObjectsUsingBlock:^(imageObjectBO* obj, NSUInteger idx, BOOL *stop) { if ([obj.name isEqualToString:imgBO.name]) { currentIndex = idx; } }]; TPImageScannerController *imageScanner = [[TPImageScannerController alloc]init]; imageScanner.type = contentImageScannerType; imageScanner.currentIndex = currentIndex; imageScanner.imageBOList = imageBOArray; imageScanner.type = contentImageScannerType; imageScanner.delegate = self; [self.navigationController pushViewController:imageScanner animated:NO]; [CoreAnimationEffect animationEaseIn:self.navigationController.view]; if (isNotIOS8) { imageScanner.currentIndex = currentIndex; } } - (void)goToDetailPage:(listContObjectVO *)listBO { pushContentWithListContentObject(self.navigationController, listBO); } - (void)shareEvent:(NSString *)shareText indexPath:(NSIndexPath *)indexPath offset:(CGFloat)offset{ CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath]; CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview].superview]; CGPoint sharePoint = CGPointMake(rectInSuperview.origin.x + offset, rectInSuperview.origin.y); TPLOG(@"x:%f y:%f",sharePoint.x,sharePoint.y); if ([self.delegate respondsToSelector:@selector(beginToShare:startPoint:)]) { [self.delegate beginToShare:shareText startPoint:sharePoint]; } if ([self.delegate respondsToSelector:@selector(shareIndexPath:)]) { [self.delegate shareIndexPath:indexPath]; } } #pragma mark - video play - (void)clickToPlayVideo:(CGRect)videoFrame indexPath:(NSIndexPath *)indexPath videoBO:(videoObjectBO *)videoBO{ [self.viewModel adVideoInView:self.view videoFrame:videoFrame videoData:videoBO]; } - (void)playFinish:(NSNotification*)notification { [self stopAndRemoveVideo]; self.isVideoPlaying = NO; } - (void)stopAndRemoveVideo { [self.viewModel stopAndRemoveVideo]; } - (void)moviePlayerWillMoveFromWindow { } - (void)enterFullScreen:(BOOL)isEnter { if (!isEnter) { [self stopAndRemoveVideo]; } } - (void)dealloc { [self stopAndRemoveVideo]; self.viewModel.delegate = nil; self.viewModel = nil; self.tableView.delegate = nil; self.tableView.dataSource = nil; } - (void)closeHandler { [self stopAndRemoveVideo]; } #pragma mark - image scanner delegate - (void)imageContentClick:(imageObjectBO *)imageBO { [self.navigationController popViewControllerAnimated:NO]; [CoreAnimationEffect animationEaseOut:self.navigationController.view]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - pinchhandler - (void)pinchHandler:(UIPinchGestureRecognizer*)zoom { UIGestureRecognizerState state = zoom.state; if (state == UIGestureRecognizerStateBegan) { }else if (state == UIGestureRecognizerStateChanged) { // TPLOG(@"%f",zoom.scale); }else if (state == UIGestureRecognizerStateEnded) { // TPLOG(@"end scale:%f",zoom.scale); if (zoom.scale > 1) { //放大 if ([TPUserDefault instance].contFontSize == fontSizeExBig) { return; } if ([TPUserDefault instance].contFontSize == fontSizeExSmall) { [TPUserDefault instance].contFontSize = fontSizeSmall; }else if ([TPUserDefault instance].contFontSize == fontSizeSmall) { [TPUserDefault instance].contFontSize = fontSizeMiddle; }else if ([TPUserDefault instance].contFontSize == fontSizeMiddle) { [TPUserDefault instance].contFontSize = fontSizeBig; }else if([TPUserDefault instance].contFontSize == fontSizeBig){ [TPUserDefault instance].contFontSize = fontSizeExBig; } [self reloadTableWithFontChange]; }else if (zoom.scale<1){ if ([TPUserDefault instance].contFontSize == fontSizeExSmall) { return; } if ([TPUserDefault instance].contFontSize == fontSizeExBig) { [TPUserDefault instance].contFontSize = fontSizeBig; }else if ([TPUserDefault instance].contFontSize == fontSizeBig) { [TPUserDefault instance].contFontSize = fontSizeMiddle; }else if ([TPUserDefault instance].contFontSize == fontSizeMiddle) { [TPUserDefault instance].contFontSize = fontSizeSmall; }else if([TPUserDefault instance].contFontSize == fontSizeSmall){ [TPUserDefault instance].contFontSize = fontSizeExSmall; } [self reloadTableWithFontChange]; } } } #pragma mark - viewModel delegate - (void)getLiveDetailPageData:(liveDetailPageVO *)pageBO { self.livePageBO = pageBO; self.tableView.refreshControl.originalContentInsectY = 0; [self endRefresh]; } - (void)getLiveInfo:(liveInfoBO *)infoBO { self.liveInfoBO = infoBO; if ([self.delegate respondsToSelector:@selector(listRefreshed:)]) { [self.delegate listRefreshed:self.liveInfoBO]; } } - (void)returnMoreInfoData:(id)responseData { __weak typeof(self) weakSelf = self; [self.viewModel getMoreFootData:self.dataList responseData:responseData heightList:heightOfCellInEachSection titleList:sectionTitleHeightList completion:^(NSMutableArray *dataSource, NSMutableArray *sectionTitleList, NSMutableArray *heightList, NSString *nextUrl) { weakSelf.heightOfCellInEachSection = [NSMutableArray arrayWithArray:heightList]; weakSelf.sectionTitleHeightList = [NSMutableArray arrayWithArray:sectionTitleList]; weakSelf.dataList = [NSMutableArray arrayWithArray:dataSource]; [self loadSuccess]; weakSelf.nextUrl = nextUrl; }]; } -(void)hiddenStatusBar:(BOOL)hidden{ if ([self.delegate respondsToSelector:@selector(statusBarHidden:)]) { [self.delegate statusBarHidden:hidden]; } } - (void)remoteFail { [self endRefresh]; [self loadSuccess]; } -(void)refreshPage{ if ([self.delegate respondsToSelector:@selector(needRefrshPage)]) { [self.delegate needRefrshPage]; } } @end