IVTStarLiveVC.m 9.08 KB
//
//  IVTStarLiveVC.m
//  IVTMeLiveTwo
//
//  Created by xrg on 16/8/24.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTStarLiveVC.h"

#import "BaseNavigationController.h"

@interface IVTStarLiveVC()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    IVTHotAndFollowVideoModel *playModel;
    UILabel *noDataLabel;

}
@property (nonatomic, strong) NSMutableArray *dataArray;

@end



@implementation IVTStarLiveVC

- (void)viewDidLoad
{
    [self requestStarLiveListFunc];
    [self initMainViews];

    noDataLabel = [[UILabel alloc]init];
    noDataLabel.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
    noDataLabel.font = [UIFont systemFontOfSize:16];
    noDataLabel.text = @"空空如也~~";
    noDataLabel.textAlignment = 1;
    noDataLabel.hidden = YES;
    [self.view addSubview:noDataLabel];
}
#pragma mark - 数据源数组初始化
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark - ********************** Init All Views **********************
-(void)initMainViews{
    if (!_tableV) {
        _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 140) style:UITableViewStylePlain];
        _tableV.backgroundColor = [UIColor IVTBackgroundColor];
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsHorizontalScrollIndicator = NO;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [_tableV registerClass:[IVTMeLiveListCell class] forCellReuseIdentifier:@"MeLiveListCell"];
        [self.view addSubview:_tableV];
    }
    self.tableV.mj_header= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestStarLiveListFunc)];
}

#pragma mark - 网络请求
- (void)requestStarLiveListFunc
{
    __weak __typeof(self)vc = self;

    NSMutableDictionary *paramDict = [[NSMutableDictionary alloc]init];
    [paramDict setObject:[IVTAccountTool getUserId] forKey:@"username"];
    [paramDict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
    [paramDict setObject:@"1"  forKey:@"page"];
    [paramDict setObject:@"20" forKey:@"size"];
    
    [[IVTNetworkAPIClient sharedClient] getRequestStarVideoList:paramDict success:^(NSDictionary *successData) {
        [vc.tableV.mj_header endRefreshing];
        
        NSArray *listArray = [successData objectForKey:@"data"];
        if (listArray.count > 0) {
            [vc.dataArray removeAllObjects];

            for (NSMutableDictionary *liveDict  in listArray) {
                
                playModel = [[IVTHotAndFollowVideoModel alloc] initWithDict:liveDict];
                
                [vc.dataArray addObject:playModel];
            }
        }

        if ([vc.dataArray count] > 0) {
            noDataLabel.hidden = YES;
        } else {
            noDataLabel.hidden = NO;
        }
        
        [_tableV reloadData];
    } error:^(NSDictionary *successData) {
       
        [vc.tableV.mj_header endRefreshing];

        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
    } failure:^(NSError *failureError) {
        
        [vc.tableV.mj_header endRefreshing];
        [HUDUtil showError:@"网络异常"];
    }];
}


#pragma mark - UITableview的数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenWidth + 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 7.0f;
    
}

#pragma 定义tableview上的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IVTMeLiveListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeLiveListCell"];
    static NSString * cellIdentifier = @"cell";
    
    if (indexPath.row == 0) {
        
        cell.delegate = self;
        if (!cell) {
            cell = [[IVTMeLiveListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        playModel = self.dataArray[0];
        
        cell.playerModel = playModel;
        
    }else if(indexPath.row == 1){
        
        if (!cell) {
            cell = [[IVTMeLiveListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        playModel = self.dataArray[1];
        
        cell.playerModel = playModel;
        
    }else if (indexPath.row == 2){
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        _userLiveScrollView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 370) collectionViewLayout:flowLayout];
        _userLiveScrollView.backgroundColor = [UIColor whiteColor];
        _userLiveScrollView.dataSource=self;
        _userLiveScrollView.delegate=self;
        _userLiveScrollView.backgroundColor = [UIColor IVTBackgroundColor];
        //注册Cell,必须要有
        [_userLiveScrollView registerClass:[IVTMeLiveListCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [cell.contentView addSubview:_userLiveScrollView];
    }
    return cell;
}




#pragma mark - UIcollection的数据源
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 50;
    //    return self.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"UICollectionViewCell";
    IVTNewCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    for (int i = 0; i <=indexPath.row; i ++) {
        if (indexPath.row == i) {
            cell.userHeadImageView.image = [UIImage imageNamed:@"直播-1"];
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count > 0) {
        [SVProgressHUD show];

        IVTKSYLiveShowViewController *liveVC = [[IVTKSYLiveShowViewController alloc]init];
        IVTHotAndFollowVideoModel * playerModel = self.dataArray[indexPath.row];
        
        liveVC.roomJID = [[XMPPManager sharedInstance] getRoomJID:playerModel.chatRoomJid];
        liveVC.liveShowURL = [NSURL URLWithString:playerModel.url];
        liveVC.liveId = playerModel.userid;
        liveVC.userIDTolive = playerModel.userIDToLive;
        liveVC.zhuBoAvatarUrl = playerModel.portrait;
        liveVC.personNumber = playerModel.online_users;
        [liveVC setHidesBottomBarWhenPushed:YES];
        if ([[NSString stringWithFormat:@"%@",playerModel.userIDToLive] isEqualToString:[NSString stringWithFormat:@"%@",[IVTAccountTool getUserId]]]) {
            [SVProgressHUD showErrorWithStatus:@"该直播间无法进入,请稍后再试"];
        }else{
            
            BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:liveVC];
            [self.tabBarController presentViewController:nav animated:YES completion:^{
                [SVProgressHUD dismiss];
            }];
            
        }
    }

}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width-24) / 3.0;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat itemHeight;
    if(screenHeight == 568.0){
        itemHeight = 80;
        
    }else  if(screenHeight == 667.0){
        itemHeight = 89;
        
    }else{
        itemHeight = 100;
    }
    
    return CGSizeMake(itemWidth, itemHeight);
}




//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3, 12, 3, 3);
}

//上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

//这个方法是两个之间的间距(同一行cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}



@end