// // VideoDownLoadListVC.m // jiaPei // // Created by 张嵘 on 2018/10/22. // Copyright © 2018 JCZ. All rights reserved. // #import "VideoDownLoadListVC.h" #import "VideoPlayViewController.h" static NSString *videoDownLoadListCellIdentifier = @"VideoDownLoadListCell"; @interface VideoDownLoadListVC () @property (nonatomic, strong) UISegmentedControl *segmentedControl; @property (nonatomic, strong) UIBarButtonItem *rightItemBtn; @property (nonatomic, strong) NSMutableArray *datas; @property (nonatomic, strong) NSMutableArray *dataArr; @end @implementation VideoDownLoadListVC #pragma mark - Life Circle - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self buildDatas]; if(self.datas){ self.tableView.editing = NO; [self.tableView reloadData]; } } - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - About UI - (void)initUI { [self initBase]; [self initTableView]; } - (void)initBase { self.view.backgroundColor = backGroundColor; [self configNavigationBarFirstVC]; self.navigationItem.titleView = self.segmentedControl; // 进度通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downLoadProgress:) name:RQDownloadProgressNotification object:nil]; // 状态改变通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downLoadStateChange:) name:RQDownloadStateChangeNotification object:nil]; } - (void)initTableView { if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else { self.automaticallyAdjustsScrollViewInsets = NO; } _tableView.rowHeight = UITableViewAutomaticDimension; _tableView.estimatedRowHeight = ((KScreenwidth / 4.0) * (2 / 3) + 16); UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, CGFLOAT_MIN)]; self.tableView.tableHeaderView = testView; } #pragma mark - Request Data - (void)buildDatas { [self getVideos]; // self.datas = [NSMutableArray arrayWithArray:[kRQDownloadManager downloadModels]]; } - (void)getVideos { if (![Util connectedToNetWork]) { showMsgUnconnect(); _datas = [NSMutableArray arrayWithArray:[kRQDownloadDataBaseManager getAllDownloadedData]]; [[[NSMutableArray arrayWithArray:[kRQDownloadDataBaseManager getAllDownloadedData]].rac_sequence.signal filter:^BOOL(RQDownloadModel *downloadModel) { return [downloadModel.subject integerValue] == self.videoListSegControlIndex; }] subscribeNext:^(RQDownloadModel *downloadModel) { [self.datas addObject:downloadModel]; } completed:^{ [_tableView reloadData]; }]; return; } NSMutableArray *aArray=[NSMutableArray array]; [aArray addPro:@"chapterId" Value:@""]; [aArray addPro:@"subject" Value:@""]; [aArray addPro:@"isPage" Value:@"0"]; [aArray addPro:@"pageSize" Value:@"0"]; [aArray addPro:@"currentPage" Value:@"0"]; NSString* method = @"getVideos"; ShowHUD(); [jiaPeiManager requestAnythingWithURL:method array:aArray data:nil completion:^(NSDictionary *dict) { RemoveHUD(); if (!dict) { ShowMsg(@"视频资源加载失败!"); return ; } if ([dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } NSArray *dicDataArr = dict[@"body"]; _datas = [NSMutableArray array]; _dataArr = [NSMutableArray array]; [[dicDataArr.rac_sequence.signal filter:^BOOL(NSDictionary *dic) { return [dic[@"subject"] integerValue] == self.videoListSegControlIndex + 1; }] subscribeNext:^(NSDictionary *dic) { [_dataArr addObject:dic]; RQDownloadModel *downloadModel = [[RQDownloadModel alloc] init]; downloadModel.videoId = dic[@"id"]; downloadModel.subject = dic[@"subject"]; downloadModel.title = dic[@"title"]; downloadModel.urlString = dic[@"videoPath"]; downloadModel.imageUrl = dic[@"image"]; downloadModel.content = dic[@"content"]; if ([kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString]) { [_datas addObject:[kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString]]; }else { [_datas addObject:downloadModel]; } } completed:^{ dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); }]; }]; } #pragma mark - Pravite Method #pragma mark - Public Method #pragma mark - Event response - (void)segmentAction:(UISegmentedControl *)Seg { self.videoListSegControlIndex = Seg.selectedSegmentIndex; [self buildDatas]; } #pragma mark - Delegate methods #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.datas.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [tableView registerNib:[UINib nibWithNibName:@"VideoDownLoadListCell" bundle:nil] forCellReuseIdentifier:@"VideoDownLoadListCell"]; VideoDownLoadListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoDownLoadListCell"]; if (!cell) { cell = [[VideoDownLoadListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"VideoDownLoadListCell"];; } if (self.datas[indexPath.row]) { cell.model = self.datas[indexPath.row]; } return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; RQDownloadModel *model = _datas[indexPath.row]; VideoPlayViewController *vc = [[VideoPlayViewController alloc] init]; vc.currentModel = model; [self navPushHideTabbarToVC:vc]; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [tableView beginUpdates]; RQDownloadModel *downloadModel = self.datas[indexPath.row]; downloadModel = [kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString]; [kRQDownloadManager deleteTaskAndCache:downloadModel]; [tableView endUpdates]; }]; return @[action]; } #pragma mark - RQDownloadNotification // 正在下载,进度回调 - (void)downLoadProgress:(NSNotification *)notification { RQDownloadModel *downloadModel = notification.object; [self.datas enumerateObjectsUsingBlock:^(RQDownloadModel *model, NSUInteger idx, BOOL * _Nonnull stop) { if ([model.urlString isEqualToString:downloadModel.urlString]) { // 主线程更新cell进度 dispatch_async(dispatch_get_main_queue(), ^{ VideoDownLoadListCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]]; [cell updateViewWithModel:downloadModel]; }); *stop = YES; } }]; } // 状态改变 - (void)downLoadStateChange:(NSNotification *)notification { RQDownloadModel *downloadModel = notification.object; [self.datas enumerateObjectsUsingBlock:^(RQDownloadModel *model, NSUInteger idx, BOOL * _Nonnull stop) { if ([model.urlString isEqualToString:downloadModel.urlString]) { // 更新数据源 self.datas[idx] = downloadModel; // 主线程刷新cell dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; }); *stop = YES; } }]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - Getters/Setters/Lazy Loading - (UISegmentedControl *)segmentedControl { if (!_segmentedControl) { _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"科目一",@"科目二",@"科目三",@"科目四"]]; _segmentedControl.frame = CGRectMake(0.0, 0.0, 100.0, 30.0); _segmentedControl.tintColor = [UIColor clearColor]; NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: defGreen}; [_segmentedControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性 NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: contentTextColor}; [_segmentedControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal]; [_segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; //添加委托方法 _segmentedControl.selectedSegmentIndex = _videoListSegControlIndex; } return _segmentedControl; } - (NSMutableArray *)datas { if (!_datas) { _datas = [NSMutableArray array]; } return _datas; } - (VideoListSegControlIndex)videoListSegControlIndex { if (!_videoListSegControlIndex) { _videoListSegControlIndex = VideoListSegControlIndex_ObjectOne; } return _videoListSegControlIndex; } @end