JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCVideotapePersenter.h"
#import "LCVideotapeListCell.h"
#import "LCVideotapeListHeardView.h"
#import "LCDeviceVideotapePlayManager.h"
 
@interface LCVideotapePersenter ()
 
///进行分组后的云录像数组
@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *groupCloudVideos;
 
///进行分组后的本地录像数组
@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *groupLocalVideos;
 
///选中的云录像数组
@property (nonatomic, strong) NSMutableSet *selectedCloudVideoSet;
 
///选中的本地录像数组
@property (nonatomic, strong) NSMutableSet *selectedLocalVideoSet;
 
 
@end
 
@implementation LCVideotapePersenter
 
#pragma mark - 数据获取
 
- (void)refreshCloudVideoListWithDate:(NSDate *)date {
    [LCProgressHUD showHudOnLowerView:nil];
    weakSelf(self);
    if (date) {
        self.currentDate = date;
    } else {
        
    }
    
    [self willChangeValueForKey:@"cloudVideoArray"];
    [self.cloudVideoArray removeAllObjects];
    [self.groupCloudVideos removeAllObjects];
    [self didChangeValueForKey:@"cloudVideoArray"];
    
    [LCVideotapeInterface getCloudRecordsForDevice:self.videoManager.currentDevice.deviceId channelId:self.videoManager.currentChannelInfo.channelId day:self.currentDate From:-1 Count:30 success:^(NSMutableArray<LCCloudVideotapeInfo *> *_Nonnull videos) {
        [weakself willChangeValueForKey:@"cloudVideoArray"];
        [weakself.cloudVideoArray removeAllObjects];
        weakself.cloudVideoArray = videos;
        weakself.groupCloudVideos = [self groupVideoListWith:weakself.cloudVideoArray];
        [weakself.videoListPage.cloudVideoList lc_setEmyptImageName:@"common_pic_novideotape" andDescription:videos.count==0?@"video_module_none_record".lc_T:@""];
        [weakself didChangeValueForKey:@"cloudVideoArray"];
        
        [LCProgressHUD hideAllHuds:nil];
        [weakself.videoListPage.cloudVideoList.mj_header endRefreshing];
    } failure:^(LCError *_Nonnull error) {
        [LCProgressHUD hideAllHuds:nil];
        [LCProgressHUD showMsg:error.errorMessage];
        [weakself.videoListPage.cloudVideoList lc_setEmyptImageName:@"common_pic_novideotape" andDescription:error.errorMessage];
        [weakself.videoListPage.cloudVideoList.mj_header endRefreshing];
    }];
}
 
- (void)refreshLocalVideoListWithDate:(NSDate *)date {
    [LCProgressHUD showHudOnLowerView:nil];
    weakSelf(self);
    if (date) {
        self.currentDate = date;
    } else {
        
    }
    
    [self willChangeValueForKey:@"localVideoArray"];
    [self.localVideoArray removeAllObjects];
    [self.groupLocalVideos removeAllObjects];
    [self didChangeValueForKey:@"localVideoArray"];
    
    [LCVideotapeInterface queryLocalRecordsForDevice:self.videoManager.currentDevice.deviceId channelId:self.videoManager.currentChannelInfo.channelId day:self.currentDate From:1 To:30 success:^(NSMutableArray<LCLocalVideotapeInfo *> *_Nonnull videos) {
        [weakself willChangeValueForKey:@"localVideoArray"];
        [weakself.localVideoArray removeAllObjects];
        weakself.localVideoArray = videos;
        self.groupLocalVideos = [self groupVideoListWith:weakself.localVideoArray];
        [weakself.videoListPage.localVideoList lc_setEmyptImageName:@"common_pic_novideotape" andDescription:videos.count==0?@"video_module_none_record".lc_T:@""];
        [weakself didChangeValueForKey:@"localVideoArray"];
        
        [LCProgressHUD hideAllHuds:nil];
        [self.videoListPage.localVideoList.mj_header endRefreshing];
    } failure:^(LCError *_Nonnull error) {
        [LCProgressHUD hideAllHuds:nil];
        [LCProgressHUD showMsg:error.errorMessage];
        [self.videoListPage.localVideoList lc_setEmyptImageName:@"common_pic_novideotape" andDescription:error.errorMessage];
        [self.videoListPage.localVideoList.mj_header endRefreshing];
    }];
}
 
- (void)loadMoreCloudVideoListWithDate:(NSDate *)date {
    [LCProgressHUD showHudOnLowerView:nil];
    weakSelf(self);
    if (date) {
        self.currentDate = date;
    }
    [LCVideotapeInterface getCloudRecordsForDevice:self.videoManager.currentDevice.deviceId channelId:self.videoManager.currentChannelInfo.channelId day:self.currentDate From:[self.cloudVideoArray.lastObject.recordId integerValue] Count:30 success:^(NSMutableArray<LCCloudVideotapeInfo *> *_Nonnull videos) {
        [weakself willChangeValueForKey:@"cloudVideoArray"];
        [weakself.cloudVideoArray addObjectsFromArray:videos];
        self.groupCloudVideos = [self groupVideoListWith:weakself.cloudVideoArray];
        [weakself didChangeValueForKey:@"cloudVideoArray"];
        
        [LCProgressHUD hideAllHuds:nil];
        [self.videoListPage.cloudVideoList.mj_footer endRefreshing];
        if (videos.count < 30) {
            [self.videoListPage.cloudVideoList.mj_footer setState:MJRefreshStateNoMoreData];
        }
    } failure:^(LCError *_Nonnull error) {
        [LCProgressHUD hideAllHuds:nil];
        [LCProgressHUD showMsg:error.errorMessage];
        [self.videoListPage.cloudVideoList.mj_footer endRefreshing];
    }];
}
 
- (void)loadMoreLocalVideoListWithDate:(NSDate *)date {
    [LCProgressHUD showHudOnLowerView:nil];
    weakSelf(self);
    if (date) {
        self.currentDate = date;
    }
    [LCVideotapeInterface queryLocalRecordsForDevice:self.videoManager.currentDevice.deviceId channelId:self.videoManager.currentChannelInfo.channelId day:self.currentDate From:(int)(self.localVideoArray.count + 1) To:(int)(self.localVideoArray.count + 30) success:^(NSMutableArray<LCLocalVideotapeInfo *> *_Nonnull videos) {
        [weakself willChangeValueForKey:@"localVideoArray"];
        [weakself.localVideoArray addObjectsFromArray:videos];
        self.groupLocalVideos = [self groupVideoListWith:weakself.localVideoArray];
        [weakself didChangeValueForKey:@"localVideoArray"];
        
        [LCProgressHUD hideAllHuds:nil];
        [self.videoListPage.localVideoList.mj_footer endRefreshing];
        if (videos.count < 30) {
            [self.videoListPage.localVideoList.mj_footer setState:MJRefreshStateNoMoreData];
        }
    } failure:^(LCError *_Nonnull error) {
        [LCProgressHUD hideAllHuds:nil];
        [LCProgressHUD showMsg:error.errorMessage];
        [self.videoListPage.localVideoList.mj_footer endRefreshing];
    }];
}
 
- (NSMutableArray *)cloudVideoArray {
    if (!_cloudVideoArray) {
        _cloudVideoArray = [NSMutableArray array];
    }
    return _cloudVideoArray;
}
 
- (NSMutableArray *)localVideoArray {
    if (!_localVideoArray) {
        _localVideoArray = [NSMutableArray array];
    }
    return _localVideoArray;
}
 
- (LCDeviceVideoManager *)videoManager {
    if (!_videoManager) {
        _videoManager = [LCDeviceVideoManager manager];
    }
    return _videoManager;
}
 
- (NSDate *)currentDate {
    if (!_currentDate) {
        _currentDate = [NSDate new];
    }
    return _currentDate;
}
 
- (NSMutableArray<NSMutableArray *> *)groupCloudVideos {
    if (!_groupCloudVideos) {
        _groupCloudVideos = [NSMutableArray array];
    }
    return _groupCloudVideos;
}
 
- (NSMutableArray<NSMutableArray *> *)groupLocalVideos {
    if (!_groupLocalVideos) {
        _groupLocalVideos = [NSMutableArray array];
    }
    return _groupLocalVideos;
}
 
- (NSMutableSet *)selectedCloudVideoSet {
    if (!_selectedCloudVideoSet) {
        _selectedCloudVideoSet = [NSMutableSet set];
    }
    return _selectedCloudVideoSet;
}
 
- (NSMutableSet *)selectedLocalVideoSet {
    if (!_selectedLocalVideoSet) {
        _selectedLocalVideoSet = [NSMutableSet set];
    }
    return _selectedLocalVideoSet;
}
 
//将获取的数据进行分组。结果为按照时间倒序排列的二维数组
- (NSMutableArray *)groupVideoListWith:(NSMutableArray *)videos {
    // 线将数组按照倒叙排列
//    NSArray *sortedArray = [videos sortedArrayUsingComparator:^NSComparisonResult (id _Nonnull obj1, id _Nonnull obj2) {
//        return [[obj2 valueForKey:@"beginDate"] compare:[obj1 valueForKey:@"beginDate"]];
//    }];
    NSArray *sortedArray = videos;
    NSMutableArray *result = [NSMutableArray array];
    if ([videos.firstObject isKindOfClass:NSClassFromString(@"LCCloudVideotapeInfo")]) {
        //云录像倒序
 
        for (int a = 23; a >= 0; a--) {
            NSMutableArray *tempAry = [NSMutableArray array];
            for (int b = 0; b < sortedArray.count; b++) {
                id temp = sortedArray[b];
                NSDate *beginTime = [temp valueForKey:@"beginDate"];
                if (beginTime.hour == a) {
                    [tempAry addObject:temp];
                }
            }
            if (tempAry.count != 0) {
                [result addObject:tempAry];
            }
        }
    } else {
        //本地录像正序
        for (int a = 0; a < 24; a++) {
            NSMutableArray *tempAry = [NSMutableArray array];
            for (int b = 0; b < sortedArray.count; b++) {
                id temp = sortedArray[b];
                NSDate *beginTime = [temp valueForKey:@"beginDate"];
                if (beginTime.hour == a) {
                    [tempAry addObject:temp];
                }
            }
            if (tempAry.count != 0) {
                [result addObject:tempAry];
            }
        }
    }
    //便利数组
 
    return result;
}
 
#pragma mark - scrollView代理相关,滚动到哪个页面,页面自动刷新(如果该页面数据为空)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isKindOfClass:[UICollectionView class]]) {
        return;
    }
    if (scrollView.contentOffset.x == 0) {
        //滚动到云视频
        self.isCloudMode = YES;
        [self.videoListPage.segment setSelectIndex:0];
        [self refreshCloudVideoListWithDate:nil];
    } else {
        self.isCloudMode = NO;
        [self.videoListPage.segment setSelectIndex:1];
        [self refreshLocalVideoListWithDate:nil];
    }
}
#pragma mark - 删除数据
- (void)deleteCloudViewotape {
    weakSelf(self);
    if (self.selectedCloudVideoSet.count == 0) {
        
        [LCProgressHUD hideAllHuds:nil];
        //重新生成数据
        weakself.groupCloudVideos = [self groupVideoListWith:self.cloudVideoArray];
        [self.videoListPage.cloudVideoList reloadData];
        return;
    }
    
    if ([MBProgressHUD HUDForView:[LCProgressHUD keyWindow]] == nil) {
        [LCProgressHUD showHudOnView:nil];
    }
    
    
    NSArray<LCCloudVideotapeInfo *> * items = [self.selectedCloudVideoSet allObjects];
    [LCVideotapeInterface deleteCloudRecords:items.firstObject.recordRegionId success:^{
        [weakself.selectedCloudVideoSet removeObject:items.firstObject];
        //[self.videoListPage.cloudVideoList deleteItemsAtIndexPaths:@[items.firstObject.index]];
        
        [weakself willChangeValueForKey:@"cloudVideoArray"];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:weakself.cloudVideoArray];
        [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[LCCloudVideotapeInfo class]]) {
                if ([((LCCloudVideotapeInfo *)obj).recordId isEqualToString:items.firstObject.recordId]) {
                    [tempArr removeObject:obj];
                }
            }
            *stop = NO;
        }];
        weakself.cloudVideoArray = tempArr;
        [weakself didChangeValueForKey:@"cloudVideoArray"];
        
        [weakself deleteCloudViewotape];
    } failure:^(LCError * _Nonnull error) {
        [LCProgressHUD hideAllHuds:nil];
        [LCProgressHUD showMsg:error.errorMessage];
    }];
    
}
 
#pragma mark - collection代理相关
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView.tag == 1000) {
        //云录像
        return self.groupCloudVideos[section].count;
    } else {
        //本地录像
        return self.groupLocalVideos[section].count;
    }
}
 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (collectionView.tag == 1000) {
        //云录像
        return self.groupCloudVideos.count;
    } else {
        //本地录像
        return self.groupLocalVideos.count;
    }
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCVideotapeListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCVideotapeListCell" forIndexPath:indexPath];
    
    if (collectionView.tag == 1000) {
        //云录像
        LCCloudVideotapeInfo *info = self.groupCloudVideos[indexPath.section][indexPath.item];
        info.index = indexPath;
        cell.model = info;
    } else {
        //本地录像
        LCLocalVideotapeInfo *info = self.groupLocalVideos[indexPath.section][indexPath.item];
        cell.model = info;
    }
    
    cell.selectImg.hidden = ![self isCellChecked:indexPath];
    
    [cell.KVOController observe:self keyPath:@"isSelectAll" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *, id> *_Nonnull change) {
        if ([change[@"new"] boolValue]) {
            cell.selectImg.hidden = NO;
            if (collectionView.tag == 1000) {
                [self.selectedCloudVideoSet addObject:cell.model];
            } else {
                [self.selectedLocalVideoSet addObject:cell.model];
            }
        } else {
            cell.selectImg.hidden = YES;
            if (collectionView.tag == 1000) {
                [self.selectedCloudVideoSet removeAllObjects];
            } else {
                [self.selectedLocalVideoSet removeAllObjects];
            }
        }
    }];
 
    [cell.KVOController observe:self keyPath:@"isEdit" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *, id> *_Nonnull change) {
        if (![change[@"new"] boolValue]) {
            cell.selectImg.hidden = YES;
        }
    }];
 
    return cell;
}
 
 
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    LCVideotapeListHeardView *heardView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LCVideotapeListHeardView" forIndexPath:indexPath];
    heardView.backgroundColor = [UIColor dhcolor_c54];
    heardView.index = indexPath.section;
    if (collectionView.tag == 1000) {
        LCCloudVideotapeInfo *info = self.groupCloudVideos[indexPath.section][0];
        heardView.time = [NSString stringWithFormat:@"%02ld:00", info.beginDate.hour];
    } else {
        LCLocalVideotapeInfo *info = self.groupLocalVideos[indexPath.section][0];
        heardView.time = [NSString stringWithFormat:@"%02ld:00", info.beginDate.hour];
    }
    return heardView;
}
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        CGSize size = { SCREEN_WIDTH, 27 };
        return size;
    } else {
        CGSize size = { SCREEN_WIDTH, 32 };
        return size;
    }
}
 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCVideotapeListCell *cell = (LCVideotapeListCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (self.isEdit) {
        //编辑模式状态
        cell.selectImg.hidden = !cell.selectImg.hidden;
        if (collectionView.tag == 1000) {
            if (cell.selectImg.hidden) {
                [self.selectedCloudVideoSet removeObject:cell.model];
            } else {
                [self.selectedCloudVideoSet addObject:cell.model];
            }
        } else {
            if (cell.selectImg.hidden) {
                [self.selectedLocalVideoSet removeObject:cell.model];
            } else {
                [self.selectedLocalVideoSet addObject:cell.model];
            }
        }
    } else {
        if (collectionView.tag == 1000) {
            [LCDeviceVideotapePlayManager manager].cloudVideotapeInfo = (LCCloudVideotapeInfo *)cell.model;
        } else {
            [LCDeviceVideotapePlayManager manager].localVideotapeInfo = (LCLocalVideotapeInfo *)cell.model;
        }
        [self.videoListPage.navigationController pushToVideotapePlay];
    }
}
 
- (BOOL)isCellChecked:(NSIndexPath *)indexPath {
    if (self.selectedCloudVideoSet.count == 0) {
        return NO;
    }
    for (LCCloudVideotapeInfo *info in self.selectedCloudVideoSet) {
        if ([info.index compare:indexPath] == NSOrderedSame) {
            return YES;
        }
    }
    return NO;
}
 
- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
}
 
@end