VideoDownLoadListVC.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // VideoDownLoadListVC.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2018/10/22.
  6. // Copyright © 2018 JCZ. All rights reserved.
  7. //
  8. #import "VideoDownLoadListVC.h"
  9. #import "VideoPlayViewController.h"
  10. static NSString *videoDownLoadListCellIdentifier = @"VideoDownLoadListCell";
  11. @interface VideoDownLoadListVC () <UITableViewDataSource, UITableViewDelegate>
  12. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  13. @property (nonatomic, strong) UIBarButtonItem *rightItemBtn;
  14. @property (nonatomic, strong) NSMutableArray *datas;
  15. @property (nonatomic, strong) NSMutableArray *dataArr;
  16. @end
  17. @implementation VideoDownLoadListVC
  18. #pragma mark - Life Circle
  19. - (void)viewWillAppear:(BOOL)animated{
  20. [super viewWillAppear:animated];
  21. [self buildDatas];
  22. if(self.datas){
  23. self.tableView.editing = NO;
  24. [self.tableView reloadData];
  25. }
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self initUI];
  30. }
  31. - (void)didReceiveMemoryWarning {
  32. [super didReceiveMemoryWarning];
  33. }
  34. #pragma mark - About UI
  35. - (void)initUI {
  36. [self initBase];
  37. [self initTableView];
  38. }
  39. - (void)initBase {
  40. self.view.backgroundColor = backGroundColor;
  41. [self configNavigationBarFirstVC];
  42. self.navigationItem.titleView = self.segmentedControl;
  43. // 进度通知
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downLoadProgress:) name:RQDownloadProgressNotification object:nil];
  45. // 状态改变通知
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downLoadStateChange:) name:RQDownloadStateChangeNotification object:nil];
  47. }
  48. - (void)initTableView {
  49. if (@available(iOS 11.0, *)) {
  50. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  51. }else {
  52. self.automaticallyAdjustsScrollViewInsets = NO;
  53. }
  54. _tableView.rowHeight = UITableViewAutomaticDimension;
  55. _tableView.estimatedRowHeight = ((KScreenwidth / 4.0) * (2 / 3) + 16);
  56. UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, CGFLOAT_MIN)];
  57. self.tableView.tableHeaderView = testView;
  58. }
  59. #pragma mark - Request Data
  60. - (void)buildDatas {
  61. [self getVideos];
  62. // self.datas = [NSMutableArray arrayWithArray:[kRQDownloadManager downloadModels]];
  63. }
  64. - (void)getVideos {
  65. if (![Util connectedToNetWork]) {
  66. showMsgUnconnect();
  67. _datas = [NSMutableArray arrayWithArray:[kRQDownloadDataBaseManager getAllDownloadedData]];
  68. [[[NSMutableArray arrayWithArray:[kRQDownloadDataBaseManager getAllDownloadedData]].rac_sequence.signal filter:^BOOL(RQDownloadModel *downloadModel) {
  69. return [downloadModel.subject integerValue] == self.videoListSegControlIndex;
  70. }] subscribeNext:^(RQDownloadModel *downloadModel) {
  71. [self.datas addObject:downloadModel];
  72. } completed:^{
  73. [_tableView reloadData];
  74. }];
  75. return;
  76. }
  77. NSMutableArray *aArray=[NSMutableArray array];
  78. [aArray addPro:@"chapterId" Value:@""];
  79. [aArray addPro:@"subject" Value:@""];
  80. [aArray addPro:@"isPage" Value:@"0"];
  81. [aArray addPro:@"pageSize" Value:@"0"];
  82. [aArray addPro:@"currentPage" Value:@"0"];
  83. NSString* method = @"getVideos";
  84. ShowHUD();
  85. [jiaPeiManager requestAnythingWithURL:method array:aArray data:nil completion:^(NSDictionary *dict) {
  86. RemoveHUD();
  87. if (!dict) {
  88. ShowMsg(@"视频资源加载失败!");
  89. return ;
  90. }
  91. if ([dict[@"code"] isEqualToString:@"1"]) {
  92. ShowMsg(dict[@"body"]);
  93. return;
  94. }
  95. NSArray *dicDataArr = dict[@"body"];
  96. _datas = [NSMutableArray array];
  97. _dataArr = [NSMutableArray array];
  98. [[dicDataArr.rac_sequence.signal filter:^BOOL(NSDictionary *dic) {
  99. return [dic[@"subject"] integerValue] == self.videoListSegControlIndex + 1;
  100. }] subscribeNext:^(NSDictionary *dic) {
  101. [_dataArr addObject:dic];
  102. RQDownloadModel *downloadModel = [[RQDownloadModel alloc] init];
  103. downloadModel.videoId = dic[@"id"];
  104. downloadModel.subject = dic[@"subject"];
  105. downloadModel.title = dic[@"title"];
  106. downloadModel.urlString = dic[@"videoPath"];
  107. downloadModel.imageUrl = dic[@"image"];
  108. downloadModel.content = dic[@"content"];
  109. if ([kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString]) {
  110. [_datas addObject:[kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString]];
  111. }else {
  112. [_datas addObject:downloadModel];
  113. }
  114. } completed:^{
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. [_tableView reloadData];
  117. });
  118. }];
  119. }];
  120. }
  121. #pragma mark - Pravite Method
  122. #pragma mark - Public Method
  123. #pragma mark - Event response
  124. - (void)segmentAction:(UISegmentedControl *)Seg {
  125. self.videoListSegControlIndex = Seg.selectedSegmentIndex;
  126. [self buildDatas];
  127. }
  128. #pragma mark - Delegate methods
  129. #pragma mark - UITableViewDataSource
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. return self.datas.count;
  132. }
  133. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. [tableView registerNib:[UINib nibWithNibName:@"VideoDownLoadListCell" bundle:nil] forCellReuseIdentifier:@"VideoDownLoadListCell"];
  135. VideoDownLoadListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoDownLoadListCell"];
  136. if (!cell) {
  137. cell = [[VideoDownLoadListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"VideoDownLoadListCell"];;
  138. }
  139. if (self.datas[indexPath.row]) {
  140. cell.model = self.datas[indexPath.row];
  141. }
  142. return cell;
  143. }
  144. #pragma mark - UITableViewDelegate
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  147. RQDownloadModel *model = _datas[indexPath.row];
  148. VideoPlayViewController *vc = [[VideoPlayViewController alloc] init];
  149. vc.currentModel = model;
  150. [self navPushHideTabbarToVC:vc];
  151. }
  152. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  153. return YES;
  154. }
  155. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  156. return UITableViewCellEditingStyleDelete;
  157. }
  158. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  160. [tableView beginUpdates];
  161. RQDownloadModel *downloadModel = self.datas[indexPath.row];
  162. downloadModel = [kRQDownloadDataBaseManager getModelWithUrl:downloadModel.urlString];
  163. [kRQDownloadManager deleteTaskAndCache:downloadModel];
  164. [tableView endUpdates];
  165. }];
  166. return @[action];
  167. }
  168. #pragma mark - RQDownloadNotification
  169. // 正在下载,进度回调
  170. - (void)downLoadProgress:(NSNotification *)notification
  171. {
  172. RQDownloadModel *downloadModel = notification.object;
  173. [self.datas enumerateObjectsUsingBlock:^(RQDownloadModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
  174. if ([model.urlString isEqualToString:downloadModel.urlString]) {
  175. // 主线程更新cell进度
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. VideoDownLoadListCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
  178. [cell updateViewWithModel:downloadModel];
  179. });
  180. *stop = YES;
  181. }
  182. }];
  183. }
  184. // 状态改变
  185. - (void)downLoadStateChange:(NSNotification *)notification
  186. {
  187. RQDownloadModel *downloadModel = notification.object;
  188. [self.datas enumerateObjectsUsingBlock:^(RQDownloadModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
  189. if ([model.urlString isEqualToString:downloadModel.urlString]) {
  190. // 更新数据源
  191. self.datas[idx] = downloadModel;
  192. // 主线程刷新cell
  193. dispatch_async(dispatch_get_main_queue(), ^{
  194. [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  195. });
  196. *stop = YES;
  197. }
  198. }];
  199. }
  200. - (void)dealloc
  201. {
  202. [[NSNotificationCenter defaultCenter] removeObserver:self];
  203. }
  204. #pragma mark - Getters/Setters/Lazy Loading
  205. - (UISegmentedControl *)segmentedControl {
  206. if (!_segmentedControl) {
  207. _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"科目一",@"科目二",@"科目三",@"科目四"]];
  208. _segmentedControl.frame = CGRectMake(0.0, 0.0, 100.0, 30.0);
  209. _segmentedControl.tintColor = [UIColor clearColor];
  210. NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
  211. NSForegroundColorAttributeName: defGreen};
  212. [_segmentedControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
  213. NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
  214. NSForegroundColorAttributeName: contentTextColor};
  215. [_segmentedControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
  216. [_segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; //添加委托方法
  217. _segmentedControl.selectedSegmentIndex = _videoListSegControlIndex;
  218. }
  219. return _segmentedControl;
  220. }
  221. - (NSMutableArray *)datas {
  222. if (!_datas) {
  223. _datas = [NSMutableArray array];
  224. }
  225. return _datas;
  226. }
  227. - (VideoListSegControlIndex)videoListSegControlIndex {
  228. if (!_videoListSegControlIndex) {
  229. _videoListSegControlIndex = VideoListSegControlIndex_ObjectOne;
  230. }
  231. return _videoListSegControlIndex;
  232. }
  233. @end