LVCChannelPagingView.m 12.3 KB
//
//  LVCChannelPagingView.m
//  LiveVideoCloud
//
//  Created by cnliveJunBo on 17/6/19.
//  Copyright © 2017年 cnlive. All rights reserved.
//

#import "LVCChannelPagingView.h"
#import "LVCMainHeaderCollectionReusableView.h"
#import "LVCSectionHeaderCollectionReusableView.h"
#import "LVCMainHorizontalCollectionViewCell.h"
#import "JBSearchView.h"

static NSString *mainHeaderId = @"mainHeader";
static NSString *SectionHeaderId = @"SectionHeader";
static NSString *MainHorizontalCellId = @"MainHorizontalCell";

@interface LVCChannelPagingView() <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
    
    BOOL _hasSearchBox;  //是否显示搜索框
    NSArray *_searchColumnArray;
    NSMutableArray *_banners;
    NSMutableArray *_marquees;
    NSMutableArray *_channels;
    LVCMainHeaderCollectionReusableView *_mainHeader;
    CGFloat _collectionOffsetY;
}

@property (nonatomic, strong)UICollectionView *collectionView;
//@property (nonatomic, strong)UIView *searchBackground;
@property (nonatomic, strong)JBSearchView *searchView;
//@property (nonatomic, strong)UIButton *searchBtn;
@property (nonatomic, strong)UIView *searchLine;
@property (nonatomic, strong)UILabel *searchHotLab;

@end

@implementation LVCChannelPagingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        _banners = [[NSMutableArray alloc] init];
        _marquees = [[NSMutableArray alloc] init];
        _channels = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)setModel:(LVCHomePageListModel *)model {
    
    if (_model != model) {
        _model = model;
    }
    [self requestAppindex];
}

#pragma mark - 请求数据
- (void)requestAppindex {
    
    NSString *uid;
    if ([UserTools isLogin]) {
        uid = [UserInformationModel manager].uid;
    } else {
        uid = [NSString stringWithFormat:@"%u", arc4random()%3];
    }
    NSString *tm = [NSString stringWithFormat:@"%u_%@", arc4random()%10, uid];
    
    NSDictionary *parameter = @{@"cid": self.model.cid ? self.model.cid : @"",
                                @"tm": tm};
    
    NSString *url;
    if ([self.model.cid isEqualToString:@"tj/"]) {
        url = Appindexpage;
    } else {
        url = Appindex;
    }
    
    [[AFHTTPRequestOperationManager manager] GET:url parameters:[self addParameters:parameter] success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        responseObject = [responseObject validatedDictionary];
        
        _hasSearchBox = [responseObject[@"searchCategory"] isEqualToString:@"false"] ? NO : YES;
        
        self.searchHotLab.text = responseObject[@"placeHolder"];
        
        //是否显示搜索旁边的分类
        _searchColumnArray = responseObject[@"searchColumn"];
        
        if ([Tools isArray:_searchColumnArray]) {
            
            // 搜索旁的分类
        } else {
            
//            self.searchBtn.width = KScreenWidth - 20;
        }
        
        if (responseObject[@"banner"] && [responseObject[@"banner"] isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in responseObject[@"banner"]) {
                LVCBannerAndMarqueeModel *model = [LVCBannerAndMarqueeModel modelWithDic:dic];
                if (model) {
                    [_banners addObject:model];
                }
            }
        }
        if (responseObject[@"marquee"] && [responseObject[@"marquee"] isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in responseObject[@"marquee"]) {
                LVCBannerAndMarqueeModel *model = [LVCBannerAndMarqueeModel modelWithDic:dic];
                if (model) {
                    [_marquees addObject:model];
                }
            }
        }
        if (responseObject[@"channels"] && [responseObject[@"channels"] isKindOfClass:[NSArray class]]) {
            
            for (NSDictionary *dic in responseObject[@"channels"]) {
                LVCChannelDetailModel *model = [LVCChannelDetailModel modelWithDic:dic];
                NSMutableArray *programs = [[NSMutableArray alloc] init];
                
                if (dic[@"programs"] && [dic[@"programs"] isKindOfClass:[NSArray class]]) {
                    for (NSDictionary *program in dic[@"programs"]) {
                        id programModel;
                        if ([model.channelType isEqualToString:@"M"]) {
                            programModel = [LVCBqWqXgModel modelWithDic:program];
                        } else {
                            programModel = [LVCBannerAndMarqueeModel modelWithDic:program];
                        }
                        if (programModel) {
                            [programs addObject:programModel];
                        }
                    }
                }
                model.programs = [[NSArray alloc] initWithArray:programs];
                if (model && [Tools isArray:model.programs]) {
                    [_channels addObject:model];
                }
            }
        }
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

#pragma mark - UICollectionViewDelegate !!!!!!!!!!!!!!!!!!!!!!!!!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (_hasSearchBox) {
        
        if (scrollView == self.collectionView) {
            
            if (scrollView.contentOffset.y <= 0) {

                self.searchView.y = 0;
                if (_channels.count > 0) {
                    [_mainHeader addSubview:self.searchView];
                } else {
                    [self.collectionView addSubview:self.searchView];
                }
                _collectionOffsetY = scrollView.contentOffset.y;
            } else {
                
                if (scrollView.contentOffset.y >= 45) {
                    if (self.searchView.superview != self) {
                        self.searchView.y = -45;
                        [self.searchView addSubview:self.searchLine];
                        [self addSubview:self.searchView];
                    }
                }
                if (scrollView.contentOffset.y-_collectionOffsetY > 0) {
                    _collectionOffsetY = scrollView.contentOffset.y;
                    [UIView animateWithDuration:0.2 animations:^{
                        self.searchView.y = -45;
                    }];
                } else {
                    _collectionOffsetY = scrollView.contentOffset.y;
                    [UIView animateWithDuration:0.2 animations:^{
                        self.searchView.y = 0;
                    }];
                }
            }
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

#pragma mark - UICollectionViewDataSource !!!!!!!!!!!!!!!!!!!!!!!!!
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LVCMainHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MainHorizontalCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        _mainHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:mainHeaderId forIndexPath:indexPath];
        _mainHeader.backgroundColor = [UIColor redColor];
        if (!self.searchView.superview) {
            [_mainHeader addSubview:self.searchView];
        }
        _mainHeader.banners = _banners;
        return _mainHeader;
    } else {
        LVCSectionHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionHeaderId forIndexPath:indexPath];
        view.backgroundColor = [UIColor yellowColor];
        
        LVCChannelDetailModel *model = [[LVCChannelDetailModel alloc] init];
        model.title = [NSString stringWithFormat:@"这是第%ld组",(long)indexPath.section];
        view.model = model;
        
        return view;
    }
    return nil;
}

#pragma mark - UICollectionViewDelegateFlowLayout !!!!!!!!!!!!!!!!!!!!!!!!!
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(KScreenWidth*0.4, 80);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return UIEdgeInsetsMake(5, 10, 5, 4);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(KScreenWidth, 300);
    } else {
        return CGSizeMake(KScreenWidth, 47);
    }
}

#pragma mark - GetMethod !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64 - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[LVCMainHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:mainHeaderId];
         
        [_collectionView registerClass:[LVCSectionHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionHeaderId];
        
        [_collectionView registerClass:[LVCMainHorizontalCollectionViewCell class] forCellWithReuseIdentifier:MainHorizontalCellId];
    }
    return _collectionView;
}

- (JBSearchView *)searchView {
    
    if (!_searchView) {
        _searchView = [[JBSearchView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 45)];
        _searchView.backgroundColor = [UIColor backgroundColor];
        
        NSMutableArray *arrayM = [[NSMutableArray alloc] init];
        NSDictionary *dict1 = @{@"name":@"liveShow"};
        [arrayM addObject:dict1];
        NSDictionary *dict2 = @{@"name":@"全部"};
        [arrayM addObject:dict2];
        _searchView.sortArray = arrayM;
        
    }
    return _searchView;
}

- (UIView *)searchLine {
    
    if (!_searchLine) {
        _searchLine = [[UIView alloc] initWithFrame:CGRectMake(0, 44.5, KScreenWidth, 0.5)];
        _searchLine.backgroundColor = [UIColor navigationLineColor];
    }
    return _searchLine;
}

//- (UILabel *)searchHotLab {
//    
//    if (!_searchHotLab) {
//        _searchHotLab = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, self.searchBtn.width-45, self.searchBtn.height)];
//        _searchHotLab.textColor = [UIColor grayColorCNLive];
//        _searchHotLab.font = [UIFont systemFontOfSize:14];
//    }
//    return _searchHotLab;
//}

// ------------------------------------------分割线---------------------------------------------------
- (NSDictionary *)addParameters:(NSDictionary *)dict {
    
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return dict;
    }
    if (!dict) {
        return @{@"appid": JBAppId, @"plat": @"i", @"version": JBAppVersion};
    } else {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:dict];
        [dic addEntriesFromDictionary:@{@"appid": JBAppId, @"plat": @"i", @"version": JBAppVersion}];
        return [[NSDictionary alloc] initWithDictionary:dic];
    }
}

@end