CNWitnessSearchDetailController.m
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// 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