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

#import "IVTHotAndFollowLiveVC.h"

#import "WebViewVC.h"
#import "BaseNavigationController.h"

@interface IVTHotAndFollowLiveVC()<UITableViewDelegate,UITableViewDataSource>
{
    UILabel *noDataLabel;
}
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation IVTHotAndFollowLiveVC

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[IVTAccountTool getUserId] intValue] > 0) {
        [self requestHotLiveListFunc];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initMainViews];
    
    [self requestLoopBannerFunc];
    
    noDataLabel = [[UILabel alloc]init];
    noDataLabel.font = [UIFont systemFontOfSize:13];
    noDataLabel.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
    noDataLabel.text = @"没有数据";
    noDataLabel.hidden = YES;
    noDataLabel.font = [UIFont systemFontOfSize:16];
    noDataLabel.text = @"空空如也~~";
    noDataLabel.textAlignment = 1;
    [self.view addSubview:noDataLabel];
    
}

#pragma mark - 初始化tableview
-(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(refreshRequest)];
}

- (void)refreshRequest {
    [self requestLoopBannerFunc];
    [self requestHotLiveListFunc];
}

#pragma mark - 加载轮播图
- (void)requestLoopBannerFunc
{
    self.dataArray = nil;
    if ([_strType isEqualToString:@"hot"]) {
        [[IVTNetworkAPIClient sharedClient] getLoopBannerList:nil success:^(NSDictionary *successData) {
            NSInteger code = [[successData objectForKey:@"code"] integerValue];
            if (code == 0) {
                UIView *head  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
                head.backgroundColor  =[UIColor clearColor];
                _tableV.tableHeaderView = head;
                NSMutableArray *arr = [successData valueForKeyPath:@"list"];
                ImageScrollView *imgScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
                [head addSubview:imgScrollView];
                imgScrollView.pics = arr;
                if (imgScrollView.pics.count == 1) {
                    [imgScrollView.pageControl setHidden:YES];
                }else{
                    LRLog(@"轮播图个数大于1");
                }
                //点击事件
                [imgScrollView returnIndex:^(NSInteger index) {
                    LRLog(@"点击了第%zi张", index);
                    NSString *urlStr = arr[index][@"conUrl"];
                    if (urlStr) {
                        WebViewVC *vc = [[WebViewVC alloc] init];
                        vc.webUrl = urlStr;
                        vc.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:vc animated:YES];
                    }

                }];
                //刷新(必需的步骤)
                dispatch_async(dispatch_get_main_queue(), ^{
                    [imgScrollView reloadView];
                });

            }else{
                _tableV.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, 0);
            }
        } error:^(NSDictionary *successData) {
            
        } failure:^(NSError *failureError) {

        }];
    }
}

#pragma mark - 数据源

- (void)requestHotLiveListFunc
{
    __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"];
    
    if ([_strType isEqualToString:@"hot"]) {
        [[IVTNetworkAPIClient sharedClient] getRequestHotVideoList: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) {
                    
                    IVTHotAndFollowVideoModel *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:@"网络异常"];
        }];

    }else{
        [[IVTNetworkAPIClient sharedClient] getRequestFollowerVideoList: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) {
                    IVTHotAndFollowVideoModel *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 - tableview delegate/datasouce

#pragma mark - ********************** Delegate Event **********************
- (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;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IVTMeLiveListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeLiveListCell" forIndexPath:indexPath];
    cell.delegate = self;
    if (self.dataArray.count > 0) {
        IVTHotAndFollowVideoModel *playModel = [self.dataArray objectAtIndex:indexPath.row];
        cell.playerModel = playModel;
    }
    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];
            }];
        }
    }
}



@end