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

#import "IVTNewLiveVC.h"

#import "IVTKSYLiveShowViewController.h"
#import "BaseNavigationController.h"

@class XMPPJID;
@interface IVTNewLiveVC()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UILabel * noDataLabel;
}

@end


@implementation IVTNewLiveVC


#pragma mark - 数据源数组初始化
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad
{
    [self creatCollection];
    [self requestNewLiveListFunc];
    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 - 创建scrollView
- (void)creatCollection
{

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _userLiveScrollView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:flowLayout];
    _userLiveScrollView.dataSource=self;
    _userLiveScrollView.delegate=self;
    _userLiveScrollView.backgroundColor = [UIColor IVTBackgroundColor];
    //注册Cell,必须要有
    [_userLiveScrollView registerClass:[IVTNewCollectionCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.view addSubview:_userLiveScrollView];
    self.userLiveScrollView.mj_header= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestNewLiveListFunc)];
}

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

    NSMutableDictionary *paramDict = [[NSMutableDictionary alloc]init];
    [paramDict setObject:[IVTAccountTool getUserName] forKey:@"username"];
    [paramDict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
    [paramDict setObject:[IVTAccountTool getUserId] forKey:@"userid"];
    [paramDict setObject:@"1"  forKey:@"page"];
    [paramDict setObject:@"20" forKey:@"size"];
    
    [[IVTNetworkAPIClient sharedClient] getRequestNewVideoList:paramDict success:^(NSDictionary *successData) {
        [vc.userLiveScrollView.mj_header endRefreshing];
        [vc.dataArray removeAllObjects];
        NSArray *arr = [successData objectForKey:@"data"];
        if (arr.count > 0) {
            [vc.dataArray removeAllObjects];
            for (NSDictionary * liveDict in arr) {
                IVTNewVideoModel* newVideoModel = [[IVTNewVideoModel alloc] initWithDict:liveDict];
                [vc.dataArray addObject:newVideoModel];
            }
        }
        if ([vc.dataArray count] > 0) {
            noDataLabel.hidden = YES;
        } else {
            noDataLabel.hidden = NO;
        }
        [_userLiveScrollView reloadData];

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

}

#pragma mark - UIcollection的数据源
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.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];
    IVTNewVideoModel *newVideoModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.VideoNewModel = newVideoModel;
    
    return cell;
}

#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 = itemWidth;
        
    }else  if(screenHeight == 667.0){
        itemHeight = itemWidth;
        
    }else{
        itemHeight = itemWidth;
    }
    
    return CGSizeMake(itemWidth, itemHeight);
    //    return CGSizeMake(120, 90);
}


//定义每个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;
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [SVProgressHUD show];

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

}


@end