CNWitnessSearchDetailController.m 11.6 KB
//
//  CNWitnessSearchDetailController.m
//  CNLiveVideoClassifyKit
//
//  Created by CNLive on 2022/6/16.
//

#import "CNWitnessSearchDetailController.h"
#import "CNWitnessSearchPresent.h"
#import "CNWitnessSearchVideoCell.h"
#import "CNWitnessSearchPersonCell.h"
#import "CNWitnessSearchModel.h"
#import "CNCollectionViewLayout.h"
#import "CNLiveListManager.h"

static NSString *const CNWitnessSearchVideoCellID = @"CNWitnessSearchVideoCell";
static NSString *const CNWitnessSearchPersonCellID = @"CNWitnessSearchPersonCell";
@interface CNWitnessSearchDetailController ()<UICollectionViewDelegate,UICollectionViewDataSource,CNWaterFallLayoutDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, assign)CNWitnessSearchDetailType type;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign)NSInteger page;//请求页数
@property (nonatomic, strong)NSString *ts;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)NSMutableArray *personIDArray;
@property (nonatomic, strong)NSMutableArray *videoIDArray;
@end

@implementation CNWitnessSearchDetailController
- (void)requestData{
    if (![CNLiveNetworking isNetworking] && self.dataSource.count == 0) {
        self.collectionView.mj_footer.hidden = YES;
        [self showEmptyViewWithImage:[UIImage loadBundleImage:@"dd_wlcw"] text:@"网络开小差了,刷新试试" detailText:@"" buttonTitle:@"     刷新     " buttonAction:@selector(requestData)];
        [self setCNAPIErrorEmptyView];
        return;
    }
        weakself
    [QMUITips showLoadingInView:self.view];
    [CNWitnessSearchPresent requestSearchVideoListWithTS:self.ts searchType:self.type + 1 keyword:self.searchStr page:self.page responseBlock:^(NSArray *dataArray, BOOL isLastPage, NSString *error) {
        [QMUITips hideAllTips];
        [weakSelf.collectionView.mj_footer endRefreshing];
            if (error) {
                if (weakSelf.page == 1) {
                    weakSelf.collectionView.mj_footer.hidden = YES;
                    [weakSelf showEmptyViewWithImage:[UIImage loadBundleImage:@"wufalianjie"] text:@"接口开小差了,试试重新请求" detailText:@"" buttonTitle:@"     重试     " buttonAction:@selector(requestData)];
                    [weakSelf setCNAPIErrorEmptyView];
                    return;
                }
                [QMUITips showWithText:error inView:self.view hideAfterDelay:1.5];
                return;
            }
            if (weakSelf.page == 1 && dataArray == 0) {
                weakSelf.collectionView.mj_footer.hidden = YES;
                [weakSelf showEmptyViewWithImage:[UIImage loadBundleImage:@"zw_zt"] text:@"未搜索到相关内容哦" detailText:@"" buttonTitle:NULL buttonAction:NULL];
                [weakSelf setCNAPIErrorEmptyView];
                return;
            }
        weakSelf.collectionView.mj_footer.hidden = NO;
        if (isLastPage) {
            [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.emptyView removeFromSuperview];
        [self.dataSource addObjectsFromArray:dataArray];
        if (weakSelf.type == CNWitnessSearchDetailTypeVideo) {
            [dataArray enumerateObjectsUsingBlock:^(CNWitnessSearchDetailModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
                [weakSelf.videoIDArray addObject:model.pid];
            }];
        }else{
            [dataArray enumerateObjectsUsingBlock:^(CNPersonMessageModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
                [weakSelf.personIDArray addObject:model.id];
            }];
        }
       
        [self.collectionView reloadData];
        }];
}
- (void)dealloc
{
    if (self.type == CNWitnessSearchDetailTypeVideo) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }else{
        [[CNLiveListManager shareListManager] removeObserver:self forKeyPath:@"personMessage"];
    }
    NSLog(@"CNWitnessSearchDetailController++++++销毁了");
}
- (instancetype)initWithPageType:(CNWitnessSearchDetailType)type
{
    if (self = [super init]) {
        self.type = type;
        self.page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.left.equalTo(self.view);
    }];
    if (self.type == CNWitnessSearchDetailTypeVideo) {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureEvent:)];
        pan.delegate = self;
        [self.view addGestureRecognizer:pan];
    }
    if (self.type == CNWitnessSearchDetailTypeVideo) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoChangeWithParam:) name:@"WjjWitnessLikeStatusNotification" object:nil];
    }else{
        [[CNLiveListManager shareListManager] addObserver:self forKeyPath:@"personMessage" options:NSKeyValueObservingOptionNew context:NULL];
    }
   
}
- (void)panGestureEvent:(UIPanGestureRecognizer *)gesture{
    if (self.panGestureBlock) {
        self.panGestureBlock(gesture);
    }
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}
#pragma KVO||Notification
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"personMessage"]) {
        NSDictionary *dic = change[NSKeyValueChangeNewKey];
        CNPersonMessageModel *model = [CNPersonMessageModel mj_objectWithKeyValues:dic];
        if ([self.personIDArray containsObject:model.id]) {
            NSInteger index = [self.personIDArray indexOfObject:model.id];
            [self.dataSource replaceObjectAtIndex:index withObject:model];
            [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
        }
    }
}
- (void)videoChangeWithParam:(NSNotification *)noti{
    NSString *pid = [noti.userInfo objectForKey:@"contentId"];
    if ([self.videoIDArray containsObject:pid]) {
        NSInteger index = [self.videoIDArray indexOfObject:pid];
        NSString *selected = [noti.userInfo objectForKey:@"selected"];
        NSString *likesNumber = [noti.userInfo objectForKey:@"likesNum"];
        CNWitnessSearchDetailModel *model = self.dataSource[index];
        model.tool.liked = [selected isEqualToString:@"true"];
        model.tool.likesNum = likesNumber.integerValue;
        [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
    }
}
#pragma mark - UIGestureRecognizerDelegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == CNWitnessSearchDetailTypeVideo) {
        NSArray *dicArray = [NSDictionary mj_keyValuesArrayWithObjectArray:self.dataSource];
        [CNLiveClassifyJumpBridge openVideoPlayerVCWithCurrentIndex:[NSString stringWithFormat:@"%ld",indexPath.row] dataList:dicArray.copy];
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == CNWitnessSearchDetailTypeVideo) {
        CNWitnessSearchVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNWitnessSearchVideoCellID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    }else{
        CNWitnessSearchPersonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CNWitnessSearchPersonCellID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
        weakself
        cell.selectPersonBlock = ^(NSDictionary * _Nonnull dic) {
            [weakSelf openPersonMessage:dic];
        };
        return cell;
    }
}
- (void)openPersonMessage:(NSDictionary *)param{
    [CNLiveClassifyJumpBridge openPersonMessage:param];
}
#pragma mark - CNWaterFallLayoutDelegate
//通过代理得到每个item 的一个高度
- (CGFloat)collectionViewLayout:(CNCollectionViewLayout*)layout heightAtIndexPath:(NSIndexPath*)indexPath{
    CNWitnessSearchDetailModel *model = self.dataSource[indexPath.row];
           CGFloat oneHeight = [@"北" heightForFont:UIFontMake(15) width:(kScreenWidth-33)/2-20];
           CGFloat totalHeight = [model.title heightForFont:UIFontMake(15) width:(kScreenWidth-33)/2-20];
           if (totalHeight > oneHeight *2) {
               totalHeight = oneHeight *2;
           }
    if (model.isHorizontalScreen == 1) {
               return 192 + totalHeight;
           }
           else{
               return 290 + totalHeight;
           }
}
#pragma mark - Setting
- (void)setSearchStr:(NSString *)searchStr{
    if (![_searchStr isEqualToString:searchStr]) {
        _searchStr = searchStr;
        [self.dataSource removeAllObjects];
        if (self.type == CNWitnessSearchDetailTypeVideo) {
            [self.videoIDArray removeAllObjects];
        }else{
            [self.personIDArray removeAllObjects];
        }
        
        [self.collectionView reloadData];
        self.page = 1;
        [self requestData];
    }
}
#pragma mark - Getting
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        if (self.type == CNWitnessSearchDetailTypeVideo) {
            CNCollectionViewLayout *layout = [[CNCollectionViewLayout alloc] init];
            layout.delegate = self;
            layout.colCount = 2;
            layout.colSpace = 9;
            layout.lineSpace = 9;
            layout.sectionInsets = UIEdgeInsetsMake(12, 12, 0, 12);
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
            _collectionView.backgroundColor = UIColorHex(#F6F7F9);
        }else{
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.itemSize = CGSizeMake(kScreenWidth - 24, 80);
            layout.minimumLineSpacing = 0;
            layout.minimumInteritemSpacing = 0;
            layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
            _collectionView.backgroundColor = UIColorWhite;
        }
        
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[CNWitnessSearchVideoCell class] forCellWithReuseIdentifier:CNWitnessSearchVideoCellID];
        [_collectionView registerClass:[CNWitnessSearchPersonCell class] forCellWithReuseIdentifier:CNWitnessSearchPersonCellID];
        weakself
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.page ++;
            [weakSelf requestData];
        }];
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (NSMutableArray *)personIDArray{
    if (!_personIDArray) {
        _personIDArray = [NSMutableArray array];
    }
    return _personIDArray;
}
- (NSMutableArray *)videoIDArray{
    if (!_videoIDArray) {
        _videoIDArray = [NSMutableArray array];
    }
    return _videoIDArray;
}
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView{
    return self.view;
}

@end