TasksViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. [self reloadCellData];
  45. [self refreshEnd];
  46. }
  47. -(void)refreshEnd{
  48. [_uirefresh endRefreshing];
  49. _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshEnd", nil)];
  50. }
  51. -(void)reloadCellData{
  52. [_userDefaults synchronize];
  53. _TaskData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
  54. [_TaskTableView reloadData];
  55. NSLog(@"Data loaded");
  56. }
  57. #pragma TableViewDelegate
  58. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  59. UITableViewCell *cell = [_TaskTableView dequeueReusableCellWithIdentifier:@"TaskCellStyle"];
  60. // cell.textLabel.
  61. NSMutableDictionary *tumblrData = [NSMutableDictionary dictionaryWithDictionary:_TaskData[indexPath.row]];
  62. NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
  63. NSString *newPath = [ImageHandler getAndSaveThumbImageFromURLandSetToImageView:[tumblrData objectForKey:@"imgURL"] saveToName:[tumblrData objectForKey:@"videoID"] setToImage:cell.imageView];
  64. // NSLog(@"%@",newPath);
  65. // dispatch_async(dispatch_get_main_queue(), ^{
  66. // NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
  67. // UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]] scale:1];
  68. // UIGraphicsBeginImageContext(CGSizeMake(80, 80));
  69. // [img drawInRect:CGRectMake(0, 0, 80, 80)];
  70. // UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
  71. // UIGraphicsEndImageContext();
  72. // [cell.imageView setImage:newImg];
  73. // NSString *jpgPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/thumb/%@.jpg",[tumblrData objectForKey:@"videoID"]]];
  74. // NSLog(@"%@",jpgPath);
  75. // [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:jpgPath atomically:YES];
  76. //// [cell.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]]]];
  77. // });
  78. cell.textLabel.text = [tumblrData objectForKey:@"videoID"];
  79. [tumblrData setObject:newPath forKey:@"cachePath"];
  80. _TaskData[indexPath.row] = tumblrData;
  81. [self updateSourceData];
  82. return cell;
  83. }
  84. //-tabl
  85. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  86. return [_TaskData count];
  87. }
  88. -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  89. return 1;
  90. }
  91. -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
  92. // /UITableViewRowActionStyle *style =
  93. UITableViewRowAction *downloadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Download" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  94. }];
  95. downloadAction.backgroundColor = [UIColor colorWithRed:166/255.0 green:226/255.0 blue:105/255.0 alpha:1];
  96. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  97. }];
  98. return @[deleteAction,downloadAction];
  99. }
  100. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  101. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  102. }
  103. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  104. [[_TaskTableView cellForRowAtIndexPath:indexPath] setTintColor:[UIColor redColor]];
  105. }
  106. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  107. return 80;
  108. }
  109. #pragma Action
  110. - (IBAction)EditTaskList:(id)sender {
  111. if([sender tag]==1){
  112. [_TaskTableView setEditing:NO];
  113. [sender setTitle:@"编辑"];
  114. [_AllEditBtn setTitle:@"全部开始"];
  115. [sender setTag:0];
  116. [_AllEditBtn setTag:1];
  117. }else{
  118. [_TaskTableView setEditing:YES];
  119. [sender setTitle:@"完成"];
  120. [_AllEditBtn setTitle:@"删除"];
  121. [sender setTag:1];
  122. [_AllEditBtn setTag:2];
  123. }
  124. }
  125. - (IBAction)EditAll:(id)sender {
  126. if([sender tag]==2){
  127. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否删除所有已选任务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  128. [alert show];
  129. }else{
  130. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"所有任务加入下载队列" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  131. [alert show];
  132. }
  133. }
  134. #pragma UIAlertView Delegate
  135. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  136. if([_AllEditBtn tag]==2){
  137. if(buttonIndex==1){
  138. // NSArray *selectedIndexPaths =
  139. [self deleteSelected:[_TaskTableView indexPathsForSelectedRows]];
  140. }
  141. }else{
  142. }
  143. }
  144. -(void)startAll{
  145. }
  146. -(void)deleteSelected:(NSArray *)indexPathsArray{
  147. NSMutableIndexSet *indexSets = [[NSMutableIndexSet alloc]init];
  148. [indexPathsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  149. NSIndexPath *index = (NSIndexPath *)obj;
  150. [indexSets addIndex:index.row];
  151. }];
  152. [_TaskData removeObjectsAtIndexes:indexSets];
  153. [self updateSourceData];
  154. [self reloadCellData];
  155. }
  156. @end