123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //
- // 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 () <UITableViewDataSource, UITableViewDelegate>
- @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
|