TasksViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // TasksViewController.m
  3. // TumblrDownloader
  4. //
  5. // Created by HonorLee on 16/1/17.
  6. // Copyright © 2016年 HonorLee. All rights reserved.
  7. //
  8. #import "TasksViewController.h"
  9. #import "ImageHandler.h"
  10. @interface TasksViewController ()
  11. @property NSUserDefaults *userDefaults;
  12. @property UIRefreshControl *uirefresh;
  13. @property NSMutableArray *TaskData;
  14. @end
  15. @implementation TasksViewController
  16. //UIRefreshControl *uirefresh;
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. _TaskTableView.delegate = self;
  21. _TaskTableView.dataSource = self;
  22. // _TaskTableView sets
  23. _uirefresh = [[UIRefreshControl alloc]init];
  24. _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefresh", nil)];
  25. [_TaskTableView addSubview:_uirefresh];
  26. [_uirefresh addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
  27. _userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.honorlee.TDGroup"];
  28. }
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33. -(void)viewDidAppear:(BOOL)animated{
  34. [self pullToRefresh];
  35. }
  36. -(void)updateSourceData{
  37. [_userDefaults setObject:_TaskData forKey:@"tasks"];
  38. [_userDefaults synchronize];
  39. }
  40. #pragma Table Refresh
  41. -(void)pullToRefresh{
  42. NSLog(@"Start refresh");
  43. _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshing", nil)];
  44. [_uirefresh beginRefreshing];
  45. [self reloadCellData];
  46. [self refreshEnd];
  47. }
  48. -(void)refreshEnd{
  49. _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshEnd", nil)];
  50. [_uirefresh endRefreshing];
  51. }
  52. -(void)reloadCellData{
  53. [_userDefaults synchronize];
  54. _TaskData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
  55. [_TaskTableView reloadData];
  56. NSLog(@"Data loaded");
  57. }
  58. #pragma TableViewDelegate
  59. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  60. UITableViewCell *cell = [_TaskTableView dequeueReusableCellWithIdentifier:@"TaskCellStyle"];
  61. // cell.textLabel.
  62. NSMutableDictionary *tumblrData = [NSMutableDictionary dictionaryWithDictionary:_TaskData[indexPath.row]];
  63. NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
  64. NSString *newPath = [ImageHandler getAndSaveThumbImageFromURLandSetToImageView:[tumblrData objectForKey:@"imgURL"] saveToName:[tumblrData objectForKey:@"videoID"] setToImage:cell.imageView];
  65. // NSLog(@"%@",newPath);
  66. // dispatch_async(dispatch_get_main_queue(), ^{
  67. // NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
  68. // UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]] scale:1];
  69. // UIGraphicsBeginImageContext(CGSizeMake(80, 80));
  70. // [img drawInRect:CGRectMake(0, 0, 80, 80)];
  71. // UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
  72. // UIGraphicsEndImageContext();
  73. // [cell.imageView setImage:newImg];
  74. // NSString *jpgPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/thumb/%@.jpg",[tumblrData objectForKey:@"videoID"]]];
  75. // NSLog(@"%@",jpgPath);
  76. // [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:jpgPath atomically:YES];
  77. //// [cell.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]]]];
  78. // });
  79. cell.textLabel.text = [tumblrData objectForKey:@"videoID"];
  80. [tumblrData setObject:newPath forKey:@"cachePath"];
  81. _TaskData[indexPath.row] = tumblrData;
  82. [self updateSourceData];
  83. return cell;
  84. }
  85. //-tabl
  86. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  87. return [_TaskData count];
  88. }
  89. -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  90. return 1;
  91. }
  92. //-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
  93. //// /UITableViewRowActionStyle *style =
  94. // UITableViewRowAction *downloadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Download" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  95. //
  96. // }];
  97. // downloadAction.backgroundColor = [UIColor colorWithRed:166/255.0 green:226/255.0 blue:105/255.0 alpha:1];
  98. // UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  99. //
  100. // }];
  101. // return @[deleteAction,downloadAction];
  102. //}
  103. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  104. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  105. }
  106. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  107. [[_TaskTableView cellForRowAtIndexPath:indexPath] setTintColor:[UIColor redColor]];
  108. }
  109. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  110. return 60;
  111. }
  112. #pragma Action
  113. - (IBAction)EditTaskList:(id)sender {
  114. if([sender tag]==1){
  115. [_TaskTableView setEditing:NO];
  116. [sender setTitle:@"编辑"];
  117. [_AllEditBtn setTitle:@"全部开始"];
  118. [sender setTag:0];
  119. [_AllEditBtn setTag:1];
  120. }else{
  121. [_TaskTableView setEditing:YES];
  122. [sender setTitle:@"完成"];
  123. [_AllEditBtn setTitle:@"删除"];
  124. [sender setTag:1];
  125. [_AllEditBtn setTag:2];
  126. }
  127. }
  128. - (IBAction)EditAll:(id)sender {
  129. if([sender tag]==2){
  130. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否删除所有已选任务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  131. [alert show];
  132. }else{
  133. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"所有任务加入下载队列" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  134. [alert show];
  135. }
  136. }
  137. #pragma UIAlertView Delegate
  138. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  139. if([_AllEditBtn tag]==2){
  140. if(buttonIndex==1){
  141. // NSArray *selectedIndexPaths =
  142. [self deleteSelected:[_TaskTableView indexPathsForSelectedRows]];
  143. }
  144. }else{
  145. }
  146. }
  147. -(void)startAll{
  148. }
  149. -(void)deleteSelected:(NSArray *)indexPathsArray{
  150. NSMutableIndexSet *indexSets = [[NSMutableIndexSet alloc]init];
  151. NSFileManager *fs = [NSFileManager defaultManager];
  152. [indexPathsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  153. NSIndexPath *index = (NSIndexPath *)obj;
  154. [indexSets addIndex:index.row];
  155. NSDictionary *tumblrData = [NSDictionary dictionaryWithDictionary:_TaskData[index.row]];
  156. NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Library/Caches/%@.jpg",[tumblrData objectForKey:@"videoID"]]];
  157. if([fs fileExistsAtPath:filePath]){
  158. NSLog(@"Remove cache");
  159. NSError *err;
  160. [fs removeItemAtPath:filePath error:&err];
  161. NSLog(@"Remove error:%@",err);
  162. }
  163. }];
  164. [_TaskData removeObjectsAtIndexes:indexSets];
  165. // [_TaskTableView setContentOffset:CGPointMake(0, -200) animated:NO];
  166. #pragma reload
  167. [self updateSourceData];
  168. // [_uirefresh sendActionsForControlEvents:UIControlEventValueChanged];
  169. [self reloadCellData];
  170. }
  171. @end